Skip to main content

SKY-D325 · Security in code

Symlink-following file read

MediumCWE-59

A symbolic link (symlink) is a file that points to another path; reading it reads the target. SKY-D325 reports a file read whose path is built from outside input, when nothing in the function rejects links or other special files and nothing caps how much is read.

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

If someone can leave a link named report.csv in the export folder, a download endpoint that serves report.csv hands out /etc/shadow, a private key or the app's .env. A path such as /dev/zero or a huge file read without a size cap can also exhaust the server's memory.

What Skylos flags

app/exports.py · PythonReported on line 12
from pathlib import Path from flask import Flask, request app = Flask(__name__)EXPORTS_DIR = Path("/srv/app/exports")  @app.get("/exports")def download_export():    name = Path(request.args["file"]).name    data = (EXPORTS_DIR / name).read_bytes()    return data, 200, {"Content-Type": "text/csv"}

What Skylos leaves alone

app/show_export.py · Python
from pathlib import Path import click  @click.command()@click.argument("export_file")def show_export(export_file):    """Print an export file to the terminal."""    click.echo(Path(export_file).read_text())  if __name__ == "__main__":    show_export()

Why Skylos leaves it alone

The path is an argument of a Click command, so it comes from the person running the program, who can already read any file they choose. Skylos treats Click and Typer arguments, sys.argv and environment variables as the operator's input, not as an attacker's.

How Skylos decides

  1. Looks at Python reads: open() and a path's .open() in a read mode (r; a + mode when the handle is seen being read; a mode it cannot read), Path.read_text() and read_bytes(), and os.open() without write flags.
  2. The path must be able to carry outside data: a tainted value (a function parameter, request.*, input(), os.getenv()), a string built with an f-string, +, % or .format(), or a name taken from such a value with Path(x).name. A base name joined to a fixed folder is still reported: the last part can be a link.
  3. Skips an os.open() call when O_NOFOLLOW appears in the call or in an earlier statement of the same function, or when it uses the os module's O_CREAT | O_EXCL.
  4. Skips a proven read-only os.open() with the native O_DIRECTORY flag: it opens a directory descriptor, not file contents. SKY-D215 still checks its path. Unknown flags and a fallback such as getattr(os, "O_DIRECTORY", 0) do not prove this exception.
  5. Skips the read when earlier statements in the same function mention both a link or containment check (is_symlink, readlink, lstat, S_ISLNK, O_NOFOLLOW, resolve, relative_to, is_relative_to, or a file-type check) and a size cap (st_size, MAX_BYTES, MAX_FILE, MAX_SIDE) or file-type check (is_file, S_ISREG, fstat). A file-type name alone meets both. It matches names only.
  6. A with open(...) as fh block whose handle is only read in constant chunks (fh.read(65536), shutil.copyfileobj()) needs only the link or containment check.
  7. Decides who controls the path as SKY-D324 does: request data and route or MCP tool arguments are reported, operator input is skipped, and a plain helper's parameter is reported only when the scanned callers pass untrusted input (otherwise it moves to suppressed; a trace cut off before reaching untrusted input stays as an unlisted, non-blocking review signal). Reports MEDIUM on the line of the read call.
  8. Caller provenance does not establish an attacker's ability to create or replace a symlink. The finding states that prerequisite separately from the read-size concern; deployment permissions still need review.

How to fix it

Open the file with O_NOFOLLOW so a link in the last part makes the call fail, check with fstat that it is a regular file, and read at most a fixed number of bytes.

app/exports.py · Python · the fix, against the flagged file
Added: import osAdded: import statfrom pathlib import Path Removed: from flask import Flask, requestAdded: from flask import Flask, abort, request app = Flask(__name__)EXPORTS_DIR = Path("/srv/app/exports")Added: MAX_EXPORT_BYTES = 10 * 1024 * 1024  @app.get("/exports")def download_export():    name = Path(request.args["file"]).nameRemoved:     data = (EXPORTS_DIR / name).read_bytes()Added:     fd = os.open(EXPORTS_DIR / name, os.O_RDONLY | os.O_NOFOLLOW)Added:     with os.fdopen(fd, "rb") as fh:Added:         if not stat.S_ISREG(os.fstat(fh.fileno()).st_mode):Added:             abort(404)Added:         data = fh.read(MAX_EXPORT_BYTES)    return data, 200, {"Content-Type": "text/csv"}

When it can be wrong

  • A read inside a folder only your own process writes to cannot be redirected, but Skylos cannot see who writes the folder.
  • The guard is found by name, so it can also hide a real problem: an earlier path.is_file(), which follows links, is enough to silence the rule, and so is a bare .resolve() together with a size check.
  • O_NOFOLLOW kept in a module constant is not seen inside the function. Write it in the call or in a local variable.

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

  • Silence one reviewed read on its reported line and say why.

    Python
    data = path.read_bytes()  # skylos: ignore[SKY-D325] folder written only by us
  • Accept the reads you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

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

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