Skip to content

test(security): add direct tests for SSRF-guarded connection classes - #5336

Open
debojyoti10CC wants to merge 1 commit into
Graphify-Labs:v8from
debojyoti10CC:fix/ssrf-guard-tests
Open

debojyoti10CC wants to merge 1 commit into
Graphify-Labs:v8from
debojyoti10CC:fix/ssrf-guard-tests

Conversation

@debojyoti10CC

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #5220 (point 1: test coverage). The SSRF-guarded connection classes in graphify/security.py were only ever exercised through mocked _build_opener, so I added direct tests. They patch socket.getaddrinfo / socket.create_connection, so there is no real network and no flakiness.

Covered: _resolve_and_validate blocking loopback, RFC1918, link-local metadata, CGN and IPv6 ULA/loopback; the guarded HTTP and HTTPS connections connecting only to the validated IP and refusing blocked ones without opening a socket; the HTTPS connection keeping server_hostname as the original host (SNI/cert validation); metadata hostname blocks; and the redirect handler rejecting loopback, metadata and file:// targets while allowing a public one.

For point 2 of the issue I did not change behavior. One test pins the current mixed-answer behavior: _resolve_and_validate is decided by the first record (blocked first raises, clean first is used), while validate_url rejects if any record is blocked. Whether to align them is a maintainer call, so I left it out of this PR.

Type of change

  • Tests or CI

Verification & Invariants

Invariant: a connection never reaches a private/reserved address, and TLS still validates against the original hostname even though we connect to a pinned IP. The new tests assert both directly. Sanity check: changing server_hostname=self.host to the IP in the HTTPS connection makes test_https_connection_keeps_sni_hostname_and_pins_ip fail. No production code, generated skill files or persisted state changed.

  • Read CONTRIBUTING.md
  • Isolated boundary tests added (tests only, no fix needed)
  • Limitation documented: any-blocked vs first-record behavior is pinned, not changed

How was this tested?

python -m pytest tests/test_security.py -p no:hypothesispytest  -> 102 passed
python -m ruff check .                                           -> All checks passed
python -m pyright tests/test_security.py                         -> 0 errors

-p no:hypothesispytest was needed only because a Windows policy blocks hypothesis's native DLL on my machine. A wider -k "security or fetch or ingest" run hit 2 collection errors from that same environment, so I did not run the full suite.

Graphify-specific checklist

  • No skill fragments touched
  • Extraction untouched
  • No API keys or graph data included
  • AI authorship disclosed via Co-Authored-By trailer in the commit

Covers _resolve_and_validate, guarded HTTP/HTTPS connections (pinned IP,
SNI preserved), metadata/link-local blocks and redirect re-validation.

Refs Graphify-Labs#5220

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @debojyoti10CC. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify code review View in Graphify

Review completed · 934f805ab468

Tip

Merge confidence: 5/5 · Looks safe to merge · 🟠 High impact
No issues reported

The change adds direct tests for URL validation, DNS resolution, pinned HTTP(S) connections, and redirect handling.

Full summary

The change adds direct tests for URL validation, DNS resolution, pinned HTTP(S) connections, and redirect handling. One test documents that a guarded connection uses the first DNS answer it can validate.

Code graph and static checks · 91 functions depend on this change

Impact & health

Impact — 91 functions depend on the 91 functions this change touches.

Health — grade A; no new coupling hotspots.

Formal checks — 91 functions this change can affect weren't formally checked in this review. Formal checks are informational only.

Gate & verification

✅ PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 91 function(s) in the blast radius were not formally verified this run

Test selection

1 of 593 test file(s) selected (0%) via static blast radius.

  • tests/test_security.py — impact, changed-test

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

@debojyoti10CC

Copy link
Copy Markdown
Contributor Author

Bumping this in case it got buried. It is still ready for review, and I am happy to make any changes you would like. Thanks!

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant