Skip to content

security.py: SSRF-guarded connection classes have no direct test coverage; any-blocked vs first-clean A-record behavior undocumented #5220

Open

Description

@troal

Context

We ran a multi-reviewer audit of the repo (v0.9.109) with particular focus on graphify/security.py. Overall verdict: the SSRF design is excellent — resolve-once/connect-to-validated-IP connection classes (lines ~161–266) kill DNS rebinding by construction without monkey-patching global getaddrinfo, NAT64 WKP unwrapping and the CGN range handling are details most homegrown checks miss, and per-hop redirect re-validation is correct. Two gaps, though:

1. The SSRF core has no direct test coverage

tests/test_security.py never exercises the network-facing core. All network tests mock _build_opener (test_security.py ~110–144), so none of the following are covered:

  • cloud-metadata hostname and link-local address blocks
  • resolution to loopback / RFC1918 / CGN / IPv6 ULA addresses
  • _resolve_and_validate behavior on mixed clean/blocked A-record answers
  • redirect-to-internal (a public URL 302-ing to loopback or a cloud-metadata host)
  • the guarded HTTPS connection class preserving SNI/cert validation (server_hostname, line ~229)

Suggestion: local-socket-based tests (bind a loopback listener + a DNS-answer fixture) exercising _build_opener end-to-end, plus a redirect-chain test.

2. Any-blocked vs first-clean A-record inconsistency

validate_url rejects a URL if any resolved record is blocked (line ~148), but _resolve_and_validate connects to the first clean record and only fails if all are blocked (lines ~181–192). A dual-homed DNS answer therefore passes pre-flight validation one way and connect-time validation the other. Connect-time being authoritative is defensible, but the discrepancy is undocumented — worth either aligning or adding a comment stating which layer is authoritative.

Minor

  • sanitize_label (451–462) deliberately does not HTML-escape and pushes escaping to callers; sanitize_metadata (499–518, escape-by-default) is the safer contract. Consider making an escape-by-default variant the primary API for new callers.

Happy to upstream any of the test additions if useful — the audit otherwise found the module unusually well-hardened for this class of tool.

Activity

  1. github-actions commented on Nov 15, 2026

    @github-actions

    Thanks for opening this issue, @troal. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions