SKY-D359 · Security in code
Cached request-derived external Flask URL
Flask's url_for(..., _external=True) builds an absolute link such as https://shop.example.com/product/tea. Unless SERVER_NAME is set, the host part comes from the Host header of the current request, which the client chooses. SKY-D359 reports such links built inside a function whose result is cached.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
The first request fills the cache. If an attacker sends it with Host: evil.example, every later visitor gets a sitemap, email or page whose links point to the attacker's site. This is called web cache poisoning, and it can turn into phishing or password-reset theft.
What Skylos flags
from functools import lru_cache from flask import Flask, url_for app = Flask(__name__) @app.get("/product/<slug>")def product(slug): return {"slug": slug} @lru_cache(maxsize=1)def sitemap_urls(): return [url_for("product", slug=s, _external=True) for s in ("tea", "mugs")] @app.get("/sitemap.json")def sitemap(): return {"urls": sitemap_urls()}What Skylos leaves alone
from flask import Flask, url_for app = Flask(__name__) @app.get("/product/<slug>")def product(slug): return {"slug": slug} @app.get("/sitemap.json")def sitemap(): urls = [url_for("product", slug=s, _external=True) for s in ("tea", "mugs")] return {"urls": urls}Why Skylos leaves it alone
The links are built again for every request, so a forged Host header only changes the reply to the person who sent it. Skylos reports external URLs only inside cached functions.
How Skylos decides
- Finds
flask.url_for()calls, resolved through imports, that pass a literal_external=True. - Reports only when the call sits in a cached function: one decorated with
functools.lru_cache,functools.cacheorfunctools.cached_property, or a method that such a cached method of the same class calls throughself, together with functions nested in it. - Skips the file when it has exactly one Flask app and that app's config sets a literal
SERVER_NAME, or aTRUSTED_HOSTSlist with one fixed host, so the host no longer comes from the request. - Reports the rest as MEDIUM.
How to fix it
Set SERVER_NAME (or TRUSTED_HOSTS) to your real host so Flask builds external links from configuration, not from the request. If you cannot, do not cache content that contains absolute links.
from functools import lru_cache from flask import Flask, url_for app = Flask(__name__)Added: app.config["SERVER_NAME"] = "shop.example.com" @app.get("/product/<slug>")def product(slug): return {"slug": slug} @lru_cache(maxsize=1)def sitemap_urls(): return [url_for("product", slug=s, _external=True) for s in ("tea", "mugs")] @app.get("/sitemap.json")def sitemap(): return {"urls": sitemap_urls()}When it can be wrong
- A host fixed by a reverse proxy, or by
SERVER_NAMEset in another module or an environment config file, is not seen, so the call is still reported. - Caches from other libraries, such as Flask-Caching's
@cache.cached(), are not recognised, so links cached that way are not reported.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Mark a reviewed line and say why. Only the listed rule is hidden.
Python link = url_for("home", _external=True) # skylos: ignore[SKY-D359] proxy pins HostAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D359"]turns the rule off for the whole project.