跳到主要内容
知仓学习社ZHICANG

security

Enforce Elixir/Phoenix security — auth, OAuth, sessions, CSRF, XSS, SQL

读凭据严重 1 · 高危 0oliver-kriska/claude-elixir-phoenix

它会碰到什么

扫了多少8 个文本文件,32 KB
它会碰到什么读凭据
命中总数1 处
命中统计严重 1 · 高 0 · 中 0 · 低 0
逐条看命中(1 条严重或高危)
  • 严重 references/advanced-patterns.md:265cred-paths
    - [ ] .env in .gitignore

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Elixir/Phoenix Security Reference

> Ash projects: AshAuthentication has its own strategy/token patterns — use the ash-framework skill. CSRF, XSS, and secret management patterns below still apply.

Quick reference for security patterns in Elixir/Phoenix.

Iron Laws — Never Violate These

  1. VALIDATE AT BOUNDARIES — Never trust client input. All data through changesets
  2. NEVER INTERPOLATE USER INPUT — Use Ecto's ^ operator, never string interpolation
  3. NO String.to_atom WITH USER INPUT — Atom exhaustion DoS. Use to_existing_atom/1
  4. AUTHORIZE EVERYWHERE — Check in contexts AND re-validate in LiveView events
  5. ESCAPE BY DEFAULT — Never use raw/1 with untrusted content
  6. SECRETS NEVER IN CODE — All secrets in runtime.exs from env vars
  7. LIVEVIEW EVENT PARAMS ARE UNTRUSTED — Users can alter forms, hooks, and every phx-value-* in DevTools. Validate and authorize against server-side state before acting

Quick Patterns

Timing-Safe Authentication

def authenticate(email, password) do
  user = Repo.get_by(User, email: email)

  cond do
    user && Argon2.verify_pass(password, user.hashed_password) ->
      {:ok, user}
    user ->
      {:error, :invalid_credentials}
    true ->
      Argon2.no_user_verify()  # Timing attack prevention
      {:error, :invalid_credentials}
  end
end

LiveView Authorization (CRITICAL)

# `id` is client input even when it came from phx-value-id.
# RE-AUTHORIZE IN EVERY EVENT HANDLER
def handle_event("delete", %{"id" => id}, socket) do
  post = Blog.get_post!(id)

  # Don't trust that mount authorized this action!
  with :ok <- Bodyguard.permit(Blog, :delete_post, socket.assigns.current_user, post) do
    Blog.delete_post(post)
    {:noreply, stream_delete(socket, :posts, post)}
  else
    _ -> {:noreply, put_flash(socket, :error, "Unauthorized")}
  end
end

Rendered LiveView events can expose IDs in HTML and websocket payloads. That is

not automatically a vulnerability: treat IDs as public identifiers, never as

proof of access. Use opaque references only when the identifier itself must not

be disclosed, and still perform server-side authorization.

SQL Injection Prevention

# ✅ SAFE: Parameterized queries
from(u in User, where: u.name == ^user_input)

# ❌ VULNERABLE: String interpolation
from(u in User, where: fragment("name = '#{user_input}'"))

Quick Decisions

What to validate?

  • All user input → Ecto changesets
  • File uploads → Extension + magic bytes + size
  • PathsPath.safe_relative/2 for traversal
  • AtomsString.to_existing_atom/1 only

What to escape?

  • HTML output → Auto-escaped by default (<%= %>)
  • User HTML → HtmlSanitizeEx with scrubber
  • Neverraw/1 with untrusted content

Anti-patterns

| Wrong | Right |

|-------|-------|

| "SELECT * FROM users WHERE name = '#{name}'" | from(u in User, where: u.name == ^name) |

| String.to_atom(user_input) | String.to_existing_atom(user_input) |

| <%= raw @user_comment %> | <%= @user_comment %> |

| Hardcoded secrets in config | runtime.exs from env vars |

| Auth only in mount | Re-auth in every handle_event |

| Trusting phx-value-* or hidden IDs | Load server-side state and authorize it |

References

For detailed patterns, see:

  • references/authentication.md - phx.gen.auth, MFA, sessions
  • references/authorization.md - Bodyguard, scopes, LiveView auth
  • references/input-validation.md - Changesets, file uploads, paths
  • references/security-headers.md - CSP, CSRF, rate limiting, headers
  • references/oauth-linking.md - OAuth account linking, token management
  • references/rate-limiting.md - Composite key strategies, Hammer patterns
  • references/advanced-patterns.md - SSRF prevention, secrets management, supply chain

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。

它属于哪个仓库

星标★ 553
本站分层T2
该仓技能数322
原文件路径targets/pi/skills/security/SKILL.md

同一个仓库里的其他技能

看这个仓库的全部 322 个技能

同名技能的其他版本

有 6 个不同仓库或目录里都有叫 security 的技能。它们内容并不相同,别混用: