SKY-D325 · Security in code
Symlink-following file read
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
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
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
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
- 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()andread_bytes(), andos.open()without write flags. - 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 withPath(x).name. A base name joined to a fixed folder is still reported: the last part can be a link. - Skips an
os.open()call whenO_NOFOLLOWappears in the call or in an earlier statement of the same function, or when it uses theosmodule'sO_CREAT | O_EXCL. - Skips a proven read-only
os.open()with the nativeO_DIRECTORYflag: it opens a directory descriptor, not file contents. SKY-D215 still checks its path. Unknown flags and a fallback such asgetattr(os, "O_DIRECTORY", 0)do not prove this exception. - 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. - A
with open(...) as fhblock whose handle is only read in constant chunks (fh.read(65536),shutil.copyfileobj()) needs only the link or containment check. - 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. - 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.
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_NOFOLLOWkept 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 usAccept the reads you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D325"]turns the rule off for the whole project.