Skip to content

test(networking): remove live snyk.io call from Test_GetHTTPClient - #673

Open
rrama wants to merge 6 commits into
mainfrom
fix/networking-test-live-http-nil-deref
Open

test(networking): remove live snyk.io call from Test_GetHTTPClient#673
rrama wants to merge 6 commits into
mainfrom
fix/networking-test-live-http-nil-deref

Conversation

@rrama

@rrama rrama commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Description

Test_GetHTTPClient made a real request to https://www.snyk.io and then asserted on the response with a non-fatal assert.Nil(t, err):

response, err := client.Get("https://www.snyk.io")
assert.Nil(t, err)                        // non-fatal: execution continues on error
assert.Equal(t, 200, response.StatusCode) // response is nil on error -> nil deref

When the request fails, response is nil and the next line dereferences it. That panics the entire pkg/networking test binary with invalid memory address or nil pointer dereference, aborting every remaining test in the package rather than failing this one test.

This reproduces deterministically anywhere www.snyk.io is unreachable — restricted CI sandboxes, offline development, or a captive network. It was found by an agent running the suite in a network-restricted environment.

Two problems, both fixed here:

  1. Panic instead of a clean failure. require.NoError now stops the test at the point of failure, so a transport error fails this single test with a useful message and the rest of the package still runs.
  2. A live external dependency in a unit test. The request now goes to an httptest server, matching the ten other HTTP tests in this same file, all of which already use httptest.NewServer.

TLS and CA-trust behaviour remain covered by Test_GetHTTPClient_EmptyCAs, which stands up its own TLS listener with a self-signed cert, so no coverage is lost. The test now needs no network and completes in 0.00s instead of ~0.4s.

Verified that the failure mode is fixed by temporarily pointing the client at a closed port: the test now reports FAIL ... connection refused with no panic, and the remaining tests in the binary still execute.

Bonus: Did some similar housekeeping for other tests in the file.

Checklist

  • Tests added and all succeed (make test)
    • pkg/networking passes at 89.7% coverage. One unrelated pre-existing failure exists on my machine: TestDetectProxyConfig in connectivity_check_extension/connectivity inherits the ambient NODE_EXTRA_CA_CERTS instead of isolating it, and passes with that variable unset. Untouched by this PR.
  • Regenerated mocks, etc. (make generate) — n/a, no mocks or API clients affected
  • Linted (make lint) — 0 issues
  • Test your changes work for the CLI — n/a, test-only change; no shipped code or public API is touched, so there is nothing for a consumer to pick up

Made with Cursor

Test_GetHTTPClient made a real request to https://www.snyk.io and then
asserted on the response with a non-fatal assert.Nil(t, err). When the
request failed, execution continued to response.StatusCode and
dereferenced a nil *http.Response, panicking the whole pkg/networking
test binary and aborting every remaining test in it.

Point the request at an httptest server instead, matching the ten other
HTTP tests in this file, and use require.NoError so a transport failure
fails this single test cleanly. TLS and CA handling stay covered by
Test_GetHTTPClient_EmptyCAs, so no coverage is lost.

The test now runs without network access and completes in 0.00s instead
of ~0.4s.

Co-authored-by: Cursor <cursoragent@cursor.com>
@snyk-io

snyk-io Bot commented Jul 29, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Secrets 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@snyk-io

snyk-io Bot commented Jul 29, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

rrama added a commit that referenced this pull request Jul 30, 2026
Consolidates the duplicate cloud-setup notes into a single AGENTS.md, keeping the
parallel branch's more precise diagnosis of the networking test: the live GET to
www.snyk.io 301-redirects to the bare snyk.io apex, which is why it fails only
when that apex is missing from the allowlist.

Describes that test as network-gated rather than a product defect, and points at
PR #673, which replaces the live call with an httptest server and makes the
caveat moot.

Replaces the fixed reachable/blocked host list with guidance to probe egress
directly, since the allowlist changes between runs.

Co-authored-by: Cursor <cursoragent@cursor.com>
cursoragent and others added 3 commits July 31, 2026 16:30
Switch remaining response-dereference guards from non-fatal testify asserts to require, so transport failures can’t nil-deref and abort the pkg/networking test binary.

Also close response bodies in the affected tests to keep behavior consistent with the rest of the file.

Co-authored-by: Ben Durrans <Benjamin.Durrans@snyk.io>
Close response bodies in tests that previously ignored the returned *http.Response, to avoid leaking keep-alive connections until GC and to align with the new response-handling pattern in this file.

Co-authored-by: Ben Durrans <Benjamin.Durrans@snyk.io>
Add t.Cleanup(server.Close) for httptest servers in the updated tests so listeners don’t stay open until process exit.

Co-authored-by: Ben Durrans <Benjamin.Durrans@snyk.io>
}
})
server := httptest.NewServer(handler)
t.Cleanup(server.Close)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I acknowledge that in Go 1.27 we can move to httptest.NewTestServer(t, handler) and this cleanup will be handled automatically. (Along with the other advantages NewTestServer brings)
But we aren't there just yet.

cursoragent and others added 2 commits August 3, 2026 08:38
Use defer server.Close() in the touched networking tests to match existing server-cleanup style in this file.

Co-authored-by: Ben Durrans <Benjamin.Durrans@snyk.io>
Use t.Cleanup(server.Close) consistently for httptest server lifecycle management within pkg/networking/networking_test.go.

Co-authored-by: Ben Durrans <Benjamin.Durrans@snyk.io>
@rrama
rrama marked this pull request as ready for review August 3, 2026 10:15
@rrama
rrama requested review from a team as code owners August 3, 2026 10:15
@snyk-pr-review-bot

Copy link
Copy Markdown

PR Reviewer Guide 🔍

🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected
📚 Repository Context Analyzed

This review considered 5 relevant code sections from 5 files (average relevance: 1.00)

🤖 Repository instructions applied (from AGENTS.md)

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.

2 participants