Skip to content

fix(audit-engine): list refused external links; refuse before the link cache and bulk checker #770 - #773

Merged
nc9 merged 7 commits into
mainfrom
fix/external-refused-links-770
Oct 14, 2026
Merged

nc9 merged 7 commits into
mainfrom
fix/external-refused-links-770

Conversation

@nc9

@nc9 nc9 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #770 (items 1, 2 and 4; item 3 landed in #767). Also carries a small #758 follow-up (below).

What changed

These follow up #767, which put the external link check behind the egress guard.

  1. Refused links are listed, and never read as checked. A link whose address the policy refuses (a private, link-local or cloud metadata address on a public site's page) is never requested. Before this PR it was stored unresolved and left out of every report line.
    • checkCollectedExternalLinks now records these links in stats.externalLinkCheck as refused (a count) and refusedLinks. The sample uses the same builder as the not-requested links: the 50 most-linked, each with 10 source pages and its pageCount, URLs masked, and masked duplicates merged.
    • links/broken-external-links lists them in a new info check, refused-external-links. They are never reported as working or broken. Message template: N external link(s) point at a private, link-local or cloud metadata address, so they were not requested and their status was not checked.
    • Links refused on the URL alone are left out before selection, like the not-requested links. They take no slot under max_links and are not counted as checked (refusedByUrl), and the over-limit denominator leaves them out. Refused links are never stored as rows, but their appearances are written.
    • When every external link was left out or refused, the rule emits only those info lines and no "could not be checked" skip, which would hold the rule's issues open.
    • Both rule-data builders now drop any external row this run did not write: a row whose checkedAt is older than the crawl's start. Incremental reuse copies a reused page's link rows from earlier crawls with their old checkedAt, and this run's external phase stamps every row it writes. So a link refused (before or after its DNS lookup), left out, or not reached under the limit can no longer report an old verdict.
  2. Refused before the cache and the bulk checker. checkExternalLinks used to read the link cache and call the bulk checker before the guard. The CLI's link cache is global, so a verdict that a private site's audit cached for http://192.168.1.1/... could be served into a public site's report, and the URL sent to the cloud.
    • A new refusedWithoutLookup in @squirrelscan/utils/public-egress settles what the URL alone decides before either is asked: the scheme, allowHost, a refused IP literal, a metadata hostname, and a non-public name the policy does not allow.
    • A public name still waits for the request's own vetting. false is not an approval.
  3. Docs list the headers the check sends, including Accept-Encoding.

Also folded in, from #771's review (#758): crawl/sitemap-lastmod-churn's "too small to judge" message now counts year and month lastmods. A sitemap with only a few of them used to read Only 0 sitemap URL(s) carry a lastmod. A test is added.

Remaining, out of scope here: the link cache is not keyed by policy, and a private site's non-public links still go to the cloud bulk check (#775).

Acceptance criteria

  • A link refused by the egress policy is reported in links/broken-external-links as not requested, with a reason and a masked sample, and is never counted as working or as checked. It is never stored as a row. A link refused on its URL alone is left out before selection, so it takes no slot under max_links (refusedByUrl). When every external link was left out or refused, only those info lines appear: no "could not be checked" skip.
  • Links refusable without DNS are refused before the link cache is read and before the bulk checker is called. A test shows that a cached 200 for a private address is not served into a public site's audit, and that the bulk checker is not asked.
  • (fix(audit-engine): external link check never requests action or token links, nor redirect hops into them (repo#2759) #767) A strict default policy applies when none is given.
  • The docs list exactly the headers the check sends.

Verification

  • packages/utils: public-egress.test.ts passes 123 tests, 3 of them new for refusedWithoutLookup: refusals on a public site, public names left to the request, and a private site's allowances.
  • packages/audit-engine: the new external-link-refused-770.test.ts passes 2 tests, covering the cache and bulk checker not being asked, and the stats sample (masked, counted, no rows stored). Both fail against main. The 2759 re-audit test now also seeds old verdicts for a metadata-address link and for a link the run never checks, and simulates the refused-after-lookup shape (a row older than the run beside an appearance the run wrote). On v1 and v2 it fails without the copied-row filter. The neighbour tests pass: external-link-egress-2759, external-links-not-requested-2759, bulk-checker-unanswered-753, fetch-deadline, undecoded-probe-fetches, link-checks-not-run-e2e and streaming-pre-rules-golden.
  • packages/rules: external-links-not-requested-2759.test.ts and link-checks-not-run.test.ts pass 29 tests. New cases: a refused link listed but not counted, a limit not reached because of it, and every link left out or refused with no not-checked skip.
  • apps/cli: dead-links-unanswered-753 and cloud-spend pass 12 tests.
  • tsgo --noEmit is clean in utils, core-contracts, rules and audit-engine. oxlint shows nothing new, the new test file is oxfmt-clean, and detect-secrets is clean.
  • docs/: tangly check --strict passes.

Agent skill

No change needed.

nc9 added 6 commits October 15, 2026 02:48
…k cache and bulk checker #770

Follow-ups to #767's egress guard on the external link check:
1. A link whose address the policy refuses (a private, link-local or
   metadata address on a public site's page) was stored unresolved and left
   out of every report line. checkCollectedExternalLinks now records them
   in stats.externalLinkCheck (refused, refusedLinks: masked sample with
   pages, the same builder as the not-requested sample), and
   links/broken-external-links lists them in a refused-external-links info
   check, never as working or broken.
2. checkExternalLinks consulted the link cache and the bulk checker before
   the guard, so a verdict a private site's audit cached for the same URL
   could be served into a public site's report, and the URL sent to the
   cloud. A new refusedWithoutLookup (@squirrelscan/utils/public-egress)
   settles what the URL alone decides (scheme, allowHost, refused IP
   literal, metadata name, non-public name the policy does not allow)
   before either is asked.
4. Docs list the headers the check sends, Accept-Encoding included.

Signed-off-by: Nik Cubrilovic <[email protected]>
…; the rule never reads them as not checked #770

From review:
- All links refused made the rule say "None of the N external link(s) could
  be checked", a not-checked skip that holds the rule's cloud issues open.
  Refused links are no longer stored as rows (they were never requested),
  and when every external link was left out or refused only those info
  lines are emitted.
- Links refused on the URL alone are now left out before selection, like
  the not-requested ones: they take no slot under max_links and are not
  counted as checked (stats.externalLinkCheck.refusedByUrl; the over-limit
  denominator leaves them out). Rule-data builders drop stale rows the
  policy would refuse, through a shared externalLinkEgress(baseUrl).
- The second stats write logs on failure; docs comment and CHANGELOG
  wording (link-local and metadata refused on every audit).

Signed-off-by: Nik Cubrilovic <[email protected]>
… not write #770

From review: skipping the row write for a refused link let a row copied
from an earlier crawl (incremental reuse copies a reused page's link rows,
not their appearances) survive with its old verdict, for a name refused
after lookup. Both builders now drop external rows with no appearance in
this crawl: only this run's external phase writes appearances, so a copied
row is a verdict this audit never made. That also retires stale verdicts
for links over the limit. Refused links keep their appearances. The
re-audit test adds a metadata-address row and a row for a link the run
never checks; without the filter it fails on v1 and v2. Docs and
CHANGELOG say which refusals take no slot.

Signed-off-by: Nik Cubrilovic <[email protected]>
…ot rows without appearances #770

From review: keeping appearances for a link refused after its DNS lookup
made the zero-appearances test pass for a row copied from an earlier crawl,
so that row's old verdict was reported again (and copied forward on every
re-audit). The builders now drop external rows whose checkedAt is older
than this crawl's start: incremental reuse copies rows with their old
checkedAt, and this run's external phase stamps every row it writes. The
re-audit test simulates the refused-after-lookup shape (a row older than
the run beside an appearance it wrote) and seeds its stale rows a minute
old (same-millisecond seeds made it flaky). Appearance batch failures now
log a warning.

Signed-off-by: Nik Cubrilovic <[email protected]>
…758

From #771's review: with a few coarse lastmods beside a news sitemap (fewer
than 20 in all), neither the news-only nor the too-coarse skip applies, and
the generic one said "Only 0 sitemap URL(s) carry a lastmod". It now counts
the coarse values, which do carry a lastmod. Test added.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/external-refused-links-770 branch from b7e199b to 408d527 Compare October 14, 2026 18:06
@nc9
nc9 marked this pull request as ready for review October 14, 2026 18:06
@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #773. I read the diff and refusedWithoutLookup. I did not run the test suite or check CI.

  • (medium) packages/utils/src/public-egress.ts:359 refusedWithoutLookup returns true for unparseable URLs and for non-http(s) schemes. Those links would be counted in refused-external-links and described as pointing at a private, link-local or metadata address, which is wrong for them. Confirm that external link collection only ever yields http(s) URLs. If it can yield others, return false for them here, or keep them out of the refused count.
  • (medium) packages/audit-engine/src/adapter.ts (v1 and v2 builders): the new checkedAt < crawl.startedAt filter drops any external row not written by this run. That is the stated intent, and the incremental-crawl test covers it. Two cases are worth checking. One is a resumed crawl, where startedAt may predate the rows written before the resume. The other is any path that does not stamp checkedAt with Date.now(), for example cloud or bulk-checker results; a missing checkedAt now counts as 0 and the row is dropped. A test for the resume case would help.
  • (low) adapter.ts: the stale-row filter calls externalLinkEgress() once per builder. Each call creates a fresh memoizeResolveHost, which the filter never uses. A lighter policy object for URL-only checks would avoid the allocation and make clear that no DNS is involved.
  • (low) adapter.ts: refusedHrefs combines refusedByUrl with the results entries marked refused, and results already includes the URL-refused ones. The checked: hrefs.length and refused values in stats are consistent, but refusedByUrl is only an internal breakdown. Check that nothing double-counts these hrefs, since results comes from checkExternalLinks, which now also marks URL-refused links.
  • (nit) external-checker.ts doc comment: the "Accept-Encoding it decodes" edit is appended in the middle of a sentence and the line runs long. It also looks unrelated to this PR.
  • (nit) The changelog entry bundles the crawl/sitemap-lastmod-drift and -churn read a bare-year or year-month lastmod as one exact day #758 and External link check: refused links vanish from the report, and the cache and bulk checker run before the egress check #770 notes in one section. That is fine, but the External link check: refused links vanish from the report, and the cache and bulk checker run before the egress check #770 entry is very long.

Tests cover the cache and bulk-checker bypass, the stats and sample masking, and the stale-row case in v1 and v2. The code reads cleanly and the security direction is right. A refused link can no longer be answered from the cache or a remote service, and nothing is stored for it. I found no blocking issues.

…rtedAt #770

Review follow-up. refusedWithoutLookup also refuses other schemes and
unparseable URLs; pin that collection never yields them, so a URL-refused
link is always an address refusal. Pin that a resume keeps the crawl's
startedAt, which the checkedAt filter depends on. Rewrap a doc line.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9

nc9 commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Triage of the review on 408d527. The follow-up is f4179b3: tests plus one doc rewrap, no behaviour change.

(medium) refusedWithoutLookup is true for unparseable URLs and non-http(s) schemes. Correct, and intended for the helper. As a pre-filter in front of a cache or a remote service it has to fail closed: the guarded send refuses those URLs too. It can't produce a wrong refused line, though, because collection never yields them. The external link phase collects only through absorbExternalLinkOccurrences, which uses extractLinks. extractLinks drops every href with an explicit scheme other than http(s) (hasNonCrawlableUrlScheme) and every href that does not resolve with new URL. Relative and protocol-relative hrefs resolve against the page's http(s) URL. The stale-row filter in the builders reads link rows from the same source. New test in external-link-refused-770.test.ts: a page with ftp, data, javascript, mailto, tel, file and an unparseable href collects only the two http(s) links, and only the private address is URL-refused.

(medium) the checkedAt < crawl.startedAt filter.

  • Resume: resumeFromStorage sets only status: "running", so startedAt stays the original run's start. Rows written before the interruption have checkedAt at or after it and are kept. restartCrawl resets startedAt, but it also calls clearCrawlData, so no earlier row survives it. New assertion in the existing resume test (crawler-stop-hard-cap.test.ts): a stopped crawl resumed to completion keeps its startedAt.
  • Rows without a fresh checkedAt: external link rows come from two writers only. One is the external link phase, which writes every result with checkedAt: Date.now(), whether it was checked locally, served from the link cache or answered by the bulk checker. The other is the incremental crawl's copy of a reused page's rows, which is what the filter exists for. A missing checkedAt (0) can only come from that copy path.

(low) externalLinkEgress() per builder. Leaving it. That is two allocations per audit, and building the URL-only check from the same constructor keeps its private-network decision identical to the live policy's.

(low) refusedHrefs double count. No overlap. hrefs is selected from requestable, which excludes refusedByUrl, so results never contains a URL-refused link. checkExternalLinks filters them too, but it is never handed one on this path. The stats test checks refused against the distinct count.

(nit) the Accept-Encoding doc line. It belongs here: since #767 every hop goes through fetchUndecoded, which adds Accept-Encoding, so "no headers but User-Agent and Accept" became wrong. Rewrapped.

(nit) long CHANGELOG entry. Leaving it as is.

@claude

claude Bot commented Oct 14, 2026

Copy link
Copy Markdown

Review of #773. I read the diff only and did not run the tests or check CI.

No critical or high findings. The refusal now happens before the cache and the bulk checker, refused links never get a row, and the stale-row filter is covered by tests, including the resume case that keeps startedAt.

  • (low) packages/audit-engine/src/adapter.ts (both rule-data builders): (link.checkedAt ?? 0) < (crawl?.startedAt ?? 0) silently drops any row with a missing checkedAt. That is probably right, but it makes the check depend on crawl.startedAt never being rewritten. The resume test pins this for the SQLite crawler. A short comment or test for other storage backends, if there are any, would guard it.
  • (low) packages/audit-engine/src/adapter.ts, externalLinkEgress: both builders call it once per run just to use refusedWithoutLookup, and each call builds a memoizeResolveHost resolver that is never used. Passing a lightweight policy there would be cleaner.
  • (low) packages/rules/src/links/broken-external-links.ts: found subtracts refused, which includes links refused after DNS lookup. eligible subtracts only refusedByUrl. This looks intentional, because links refused after lookup were selected under the limit. A comment saying so would help the next reader. A test mixing both kinds of refusal under max_links would also help.
  • (nit) The refused-external-links message always says "private, link-local or cloud metadata address". That is accurate today, and the test guarding scheme and parse failures is good. If refusedWithoutLookup gains other reasons, the text will drift.
  • (nit) The pub#770 and #770 comment prefixes are inconsistent with the surrounding code.

@nc9
nc9 merged commit 01cfca6 into main Oct 14, 2026
15 checks passed
@nc9
nc9 deleted the fix/external-refused-links-770 branch October 14, 2026 19:12
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.

External link check: refused links vanish from the report, and the cache and bulk checker run before the egress check

1 participant