Skip to main content

SKY-D359 · Security in code

Cached request-derived external Flask URL

Medium

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

app/shop.py · PythonReported on line 15
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

app/shop.py · Python
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

  1. Finds flask.url_for() calls, resolved through imports, that pass a literal _external=True.
  2. Reports only when the call sits in a cached function: one decorated with functools.lru_cache, functools.cache or functools.cached_property, or a method that such a cached method of the same class calls through self, together with functions nested in it.
  3. Skips the file when it has exactly one Flask app and that app's config sets a literal SERVER_NAME, or a TRUSTED_HOSTS list with one fixed host, so the host no longer comes from the request.
  4. 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.

app/shop.py · Python · the fix, against the flagged file
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_NAME set 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 Host
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D359"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.