Your link previewer passed its SSRF review because every fetch went through assertPublicUrl.
Months later a pull request moves one handler to axios and hands req.query.url straight to
axios.get. The tests pass because none of them sends an internal address, so the endpoint can
reach the cloud metadata service again.1
What you get
You will end up with Semgrep taint rules in three languages, fixtures that fail when a rule forgets a client, and a CI job that blocks only the findings a change adds. This is for you if an SSRF fix has to stay fixed.
Short answer
Write a Semgrep rule in taint mode. Request fields such as req.query are its sources, the URL
argument of every HTTP client your code uses is a sink, and your validator, such as
assertPublicUrl, is the sanitizer. Keep a deliberately vulnerable fixture with ruleid
comments beside it, run semgrep --test in CI, and scan pull requests with
--baseline-commit, so only the findings a change adds fail the build.
You will need
Semgrep 1.187.0 from PyPI, which declares Python 3.10 or later, Node 22 for the sample’s scripts, and a CI job you can add a step to. Go runs one test, which compiles the Go fixture. The validator the rules trust comes from defend a URL-fetching endpoint against SSRF. A rule checks that a call passes through the validator, never that the validator is correct.
Approaches compared
| Approach | When it fits | What it costs you | When to pick something else |
|---|---|---|---|
| CodeQL taint queries | Taint must be followed across functions and files, in open source code or under Advanced Security | A database build per language, QL or a data extension per sanitizer, and slower global data flow | Private code with no Advanced Security license, or a rule needed the same afternoon |
| Custom Semgrep rules | Per-repository rules in YAML, written and tested in the pull request that changes the code | The open source engine follows taint inside one function, so a wrapper of your own hides its sink | Flow through your own helpers matters, and Semgrep Pro is not available |
| Snyk Code | You already run Snyk and want its built-in SSRF rule to trust your validator | A Snyk account, an Enterprise plan for Rule Extensions, and Snyk’s sink list rather than yours | You must decide the sources and sinks yourself, or have no Snyk Enterprise plan |
CodeQL adds a sanitizer as an isBarrier predicate in a QL configuration, or as a
barrierModel row in a
data extension,
whose list of kinds includes request-forgery. Its
data flow overview
calls global flow “typically more time and energy intensive” than local flow.
Snyk Code registers your validator as a flows_through_sanitizer through
Rule Extensions,
which its documentation reserves for Enterprise customers, and its CLI needs a
Snyk account.
Snyk’s documentation index lists custom rules for IaC only, and
the address of its former Snyk Code custom rules page redirects to an IaC page. Neither shows a
way to add a client to Snyk Code’s Ssrf rule.
Write the taint rule: sources, a sanitizer, and every sink
A taint rule reports data that travels from a source to a sink without passing through a sanitizer. Here the sources are the request fields, the sanitizer is the validator, and the sinks are the URL arguments of the HTTP clients.
rules:
- id: ssrf-request-data-to-http-client-ts
mode: taint
languages: [typescript, javascript]
severity: ERROR
message: >-
Request data reaches the URL of an outbound HTTP call without passing
through assertPublicUrl(). Validate it first, or call safeFetch().
metadata:
cwe: "CWE-918: Server-Side Request Forgery (SSRF)"
pattern-sources:
- patterns:
- pattern-either:
- pattern: $REQ.query
- pattern: $REQ.body
- pattern: $REQ.params
- pattern: $REQ.headers
- metavariable-regex:
metavariable: $REQ
regex: ^(req|request)$
pattern-sanitizers:
- pattern: assertPublicUrl(...)
pattern-sinks:
- patterns:
- pattern-either:
- pattern: fetch($URL, ...)
- pattern: axios($URL, ...)
- pattern: got($URL, ...)
- patterns:
- pattern: $CLIENT.$METHOD($URL, ...)
- metavariable-regex:
metavariable: $CLIENT
regex: ^(axios|got|http|https|undici)$
- metavariable-regex:
metavariable: $METHOD
regex: ^(get|post|put|patch|delete|head|request|stream)$
- focus-metavariable: $URL
Each operator does one job. metavariable-regex holds $REQ to the names req and request,
and $CLIENT to the libraries you name. Semgrep resolves import { request } from 'undici', so
undici in that list also covers a bare request(target) call, and a test holds it to that. The
focus-metavariable line makes the URL
argument the sink. Without it, axios.post('https://audit.example.com/v1/events', req.body)
counts as a finding, and a test in the sample removes the line to prove it.
The Python and Go rules in the same file have the same shape. Go’s sources need typed metavariables, written with a colon as the pattern syntax shows:
pattern-sources:
- pattern-either:
- pattern: "($R : *http.Request).URL.Query().Get(...)"
- pattern: "($R : *http.Request).FormValue(...)"
- pattern: "($R : *http.Request).PostFormValue(...)"
- pattern: "($R : *http.Request).Header.Get(...)"
pattern-sanitizers:
- pattern: AssertPublicURL(...)
Start from patterns, and see where they stop
A plain pattern rule is quicker to read, and it shows the operators before taint mode adds to
them. This one flags fetch or a named client unless the URL is a literal or the result of
assertPublicUrl in the same expression:
patterns:
- pattern-either:
- pattern: fetch($URL, ...)
- patterns:
- pattern: $CLIENT.$METHOD($URL, ...)
- metavariable-regex:
metavariable: $CLIENT
regex: ^(axios|got|http|https|undici)$
- metavariable-regex:
metavariable: $METHOD
regex: ^(get|post|put|patch|delete|head|request|stream)$
- pattern-not: $CALL("...", ...)
- pattern-not: $CALL(assertPublicUrl(...), ...)
- pattern-not: $CALL(await assertPublicUrl(...), ...)
Its test file records the two calls it cannot get right. A todook comment marks a line that
matches and should not, which semgrep --test
leaves out of its verdict:
export async function previewInTwoSteps(input: string) {
const target = await assertPublicUrl(input)
// The pattern rule cannot see that target was checked one line up.
// todook: outbound-call-with-unchecked-url
await axios.get(target)
}
export async function previewOfConfig(config: { url: string }) {
// The pattern rule cannot tell a configured URL from request data either.
// todook: outbound-call-with-unchecked-url
await fetch(config.url)
}
A pattern sees one expression, so a URL checked one line earlier still looks raw, and so does a URL from configuration. Taint mode follows the value through the function instead.
Name every client the code base uses
The fixtures hold twenty unsafe calls, at least one for every client the rules name, and eight safe ones across the three languages. This script scans them with the full rule and with a copy that knows one client per language:
node compare.mjs
fixture line expected fetch-only full call
ssrf-taint.go:27 report reported reported resp, err := http.Get(r.URL.Query().Get("ur...
ssrf-taint.go:39 report - reported req, err := http.NewRequestWithContext(r.Co...
ssrf-taint.go:56 report - reported resp, err := client.Get(r.Header.Get("X-Cal...
ssrf-taint.go:67 report - reported resp, err := http.Head(r.URL.Query().Get("u...
ssrf-taint.go:78 report - reported resp, err := http.Post(r.PostFormValue("tar...
ssrf-taint.go:89 report - reported req, err := http.NewRequest(http.MethodGet,...
ssrf-taint.go:105 report - reported resp, err := http.DefaultClient.Get(r.URL.Q...
ssrf-taint.go:117 report - reported resp, err := client.Get(r.URL.Query().Get("...
ssrf-taint.go:133 silent - - resp, err := http.Get(target)
ssrf-taint.py:18 report reported reported return requests.get(request.args["url"], ti...
ssrf-taint.py:25 report - reported return httpx.get(target, timeout=5).text
ssrf-taint.py:32 report - reported with urllib.request.urlopen(target, timeout...
ssrf-taint.py:40 silent - - return httpx.get(target, timeout=5).text
ssrf-taint.py:46 silent - - return requests.get("https://rates.example....
ssrf-taint.ts:18 report reported reported const page = await fetch(req.query.url)
ssrf-taint.ts:25 report - reported const page = await axios.get(target)
ssrf-taint.ts:32 report - reported res.send(await got(url).text())
ssrf-taint.ts:38 report - reported const { body } = await request(target)
ssrf-taint.ts:44 report - reported https.get(`https://${req.params.host}/statu...
ssrf-taint.ts:49 report - reported http.get(`http://${req.params.host}/status`...
ssrf-taint.ts:54 report - reported const page = await axios(req.query.url)
ssrf-taint.ts:60 report - reported await got.post(req.body.callbackUrl)
ssrf-taint.ts:66 report - reported await undici.request(req.body.callbackUrl, ...
ssrf-taint.ts:74 miss - - return fetch(target)
ssrf-taint.ts:84 silent - - const page = await axios.get(target)
ssrf-taint.ts:90 silent - - const page = await safeFetch(req.query.url)
ssrf-taint.ts:96 silent - - await axios.post('https://audit.example.com...
ssrf-taint.ts:102 silent - - const rates = await fetch('https://rates.ex...
ssrf-taint.ts:104 silent - - res.send({ rates: await rates.json(), cache...
fetch-only: 3 of 20 unsafe calls reported, 0 of 8 safe calls reported
full: 20 of 20 unsafe calls reported, 0 of 8 safe calls reported
The one-client rule reports three of twenty. Every call it missed goes through a library the
handler switched to, which is the decay the rule exists to stop. The miss row is a call inside
a wrapper of your own, which the open source engine cannot see, as “When it goes wrong” explains.
Gate pull requests on the findings they add
The job installs Semgrep from the sample’s requirements.txt, which pins all 67 packages with
their SHA-256 hashes, and runs the rule tests. Then it scans with --error and
--baseline-commit, which the CLI reference describes
as showing only results not found in that commit. semgrep scan needs no Semgrep account.
- name: Install Semgrep
run: >-
python -m pip install --disable-pip-version-check
--no-deps --require-hashes -r .semgrep/requirements.txt
- name: Test the rules against their fixtures
run: semgrep --test --metrics=off .semgrep/rules
- name: Fail on findings the base branch does not have
if: github.event_name == 'pull_request'
env:
BASE_SHA: ${{ github.event.pull_request.base.sha }}
run: >-
semgrep scan --config .semgrep/rules/ssrf-taint.yaml --exclude=.semgrep
--metrics=off --error --baseline-commit "$BASE_SHA"
fetch-depth: 0 in the checkout step gives the job the base commit. The script below builds a
scratch repository with one unsafe call on the base branch and runs that scan step:
node baseline.mjs
pull request 1, no baseline exit 1 src/legacy.ts:2 src/webhooks.ts:3
pull request 1, baseline = base branch exit 1 src/webhooks.ts:3
pull request 2, baseline = base branch exit 0 (no findings)
pull request 1, no baseline, no --exclude exit 1 src/legacy.ts:2 src/webhooks.ts:3 + 20 in .semgrep/
The legacy call stays visible on main through the last step of the job, without blocking a pull request that never touched it.
Keep the scan offline
Semgrep’s metrics page says it sends metrics by default
whenever rules come from its registry or the user is logged in, and --metrics=off stops them.
The sample turns them off in semgrep.mjs, which every script and test runs Semgrep through: it
sets SEMGREP_SEND_METRICS=off and SEMGREP_ENABLE_VERSION_CHECK=0, and every Semgrep command
in the sample also passes --metrics=off. The check points the proxy variables at a local recorder:
node offline-check.mjs
--metrics=off, version check off findings 9 connections asked for: none
The control run, with metrics and the version check switched on, waits over a minute on the
refused connections, so record.sh ran it twice and committed the result:
cat records/offline-check.txt
semgrep 1.187.0, two runs, identical
--metrics=off, version check off findings 9 connections asked for: none
--metrics=on, version check on findings 9 connections asked for: metrics.semgrep.dev:443 semgrep.dev:443
Check it worked
Run the suite. It runs semgrep --test on the rules, breaks copies of them, and checks the
comparison, the baseline, the workflow, and the offline run:
node --test semgrep.test.mjs
1..12
# tests 12
# suites 0
# pass 12
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 35385.101691
Twelve tests pass. The third is the one to keep. It takes each of the 19 client entries out of a
copy of the rules in turn, and asserts that a ruleid line goes unreported every time.
When it goes wrong
A rule written for one client passes code that switched libraries. Keep a ruleid line for
every client in the fixture, so a narrowed rule fails the test run:
node narrowed-rule.mjs
semgrep --test exit 1
3/4: 1 unit tests did not pass:
✖ ssrf-request-data-to-http-client-ts
missed lines: [25], incorrect lines: []
Line 25 is the axios.get call. Add a client to the sinks and to the fixture in the same commit.
A Go typed metavariable
that lacks its colon, such as ($R *http.Request), still loads without an error but matches
nothing. A test in the sample counts the matches with the colon and finds none without it.
A call inside your own wrapper goes unreported, because the open source engine tracks taint within
one function. Semgrep’s taint documentation calls
interprocedural analysis
a Semgrep Pro feature. List the wrapper itself as a sink, as a test in the sample does with
grab, or move the check into the wrapper.
The first scan reports the fixtures themselves. Pass --exclude=.semgrep, or the twenty
deliberate findings show up in every full scan, as the last row of the baseline output shows.
When not to do this
Do not treat a clean scan as proof that the code is safe. The rule trusts the validator’s name,
so a helper called assertPublicUrl that checks nothing passes every scan.
Do not write these rules if Snyk Code already runs in your pipeline and its Ssrf rule covers
your clients. A Rule Extension that names your validator is less to maintain than a second
scanner with its own fixtures.
Do not rely on the open source Semgrep engine where request data crosses functions or files before it reaches a client. CodeQL follows that flow, at the price of a database build and a license that limits it to open source code without Advanced Security.2
Skip the rule entirely when every outbound call already leaves through an egress proxy that refuses internal addresses. The proxy applies to whatever client the code uses.
Related how-tos
Last verified
Verified 2026-11-30 against Semgrep 1.187.0 on Python 3.12, and Node 22.23.5. Every output block is what the command before it printed. CodeQL and Snyk Code were not run. CodeQL’s license limits it to open source code without a paid license, and Snyk Code needs an account. Both rows, and the Snyk sentence on custom rules, come from their documentation.
Footnotes
-
SSRF entered the OWASP Top 10 in 2021 as A10. Its page says the category was “added from the Top 10 community survey” and concedes a relatively low incidence rate in the data. The 2025 edition lists CWE-918 among the notable weaknesses of A01, Broken Access Control, the category with the most occurrences in the contributed data. ↩︎ Back to text
-
The CodeQL CLI license lets each user “perform academic research” and “demonstrate the Software” with it. Analysis needs “an Open Source Codebase,” and building databases in CI needs one that “is hosted and maintained on GitHub.com” as well. A paid GitHub Advanced Security license lifts both restrictions, in a note set in italics below the list of what the terms forbid. ↩︎ Back to text