SKY-D324 · Security in code
Symlink-following file write
A symbolic link (symlink) is a file that points to another path. When a program writes to a path whose last part is a symlink, the operating system writes to the link's target instead. SKY-D324 reports a file write whose path is built from outside input, with nothing in the function that rejects symlinks.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
If an attacker can place a link in the folder (through another upload, an extracted archive or a shared temporary directory), a write meant for notes/todo.txt lands in ~/.ssh/authorized_keys or the app's own config, with the server's permissions. Cutting the name down to its last part with Path(name).name does not help: that last part can be the link.
What Skylos flags
from pathlib import Path from flask import Flask, request app = Flask(__name__)NOTES_DIR = Path("/srv/app/notes") @app.post("/notes")def save_note(): name = Path(request.form["name"]).name with open(NOTES_DIR / name, "w", encoding="utf-8") as fh: fh.write(request.form["body"]) return {"saved": name}What Skylos leaves alone
from pathlib import Path from flask import Flask, request app = Flask(__name__)NOTES_DIR = Path("/srv/app/notes") @app.post("/notes")def save_note(): name = Path(request.form["name"]).name with open(NOTES_DIR / name, "x", encoding="utf-8") as fh: fh.write(request.form["body"]) return {"saved": name}Why Skylos leaves it alone
Mode "x" creates a new file and fails if anything already exists at that path, a symlink included, so the write can never follow a planted link. Skylos skips exclusive creates. (A note can then not be overwritten; the fix below keeps overwriting.)
How Skylos decides
- Looks at Python writes:
open()and a path's.open()in a mode that can write (w,a,+; a mode it cannot read counts as both read and write),Path.write_text()andwrite_bytes(), andos.open()withO_WRONLY,O_RDWR,O_CREAT,O_TRUNCorO_APPEND. - 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, which removes folders but not a final link. - Skips exclusive creates (
open(p, "x"), oros.open()with theosmodule'sO_CREAT | O_EXCL) and anos.open()call that namesO_NOFOLLOW. - Skips the write when an earlier statement in the same function mentions a link, file-type or containment check:
is_symlink,readlink,lstat,S_ISLNK,O_NOFOLLOW,is_file,S_ISREG,fstat,resolve,relative_tooris_relative_to. It matches the name only; it does not prove that the check rejects the path. - Decides who controls the path. Request data and route or MCP tool arguments are reported. Click or Typer command arguments,
sys.argvand environment variables belong to whoever runs the program, so they are skipped. For a plain helper's parameter, Skylos follows the callers through the scanned project: it reports when a resolved caller passes untrusted input, keeps the finding as an unlisted, non-blocking review signal when the trace is cut off before reaching untrusted input, and otherwise moves it tosuppressedas safe or unknown. - Reports HIGH on the line of the write call. When the path still holds the raw input instead of a base name, the same line usually also gets SKY-D215.
How to fix it
Open the file with O_NOFOLLOW, so the call fails when the last part of the path is a link, and keep the name reduced to a base name inside the fixed folder. Write the flags in the call or in a local variable of the same function, where Skylos sees them.
Added: import osfrom pathlib import Path from flask import Flask, request app = Flask(__name__)NOTES_DIR = Path("/srv/app/notes") @app.post("/notes")def save_note(): name = Path(request.form["name"]).nameRemoved: with open(NOTES_DIR / name, "w", encoding="utf-8") as fh:Added: flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOWAdded: fd = os.open(NOTES_DIR / name, flags, 0o600)Added: with os.fdopen(fd, "w", encoding="utf-8") as fh: fh.write(request.form["body"]) return {"saved": name}When it can be wrong
- Flags kept in a module constant (
WRITE_FLAGS = ... | os.O_NOFOLLOWat the top of the file) are not seen inside the function, so a safeos.open(path, WRITE_FLAGS)is still reported. - The other way round, any earlier mention of a guard name silences the rule: a bare
.resolve()with no containment check, or anis_file()test, which itself follows links. - A helper whose callers Skylos cannot resolve is moved to
suppressedas unknown instead of being reported; look there when a scan of part of a project shows nothing.
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 write on its reported line and say why. A bare
# noqadoes not hide a HIGH security finding.Python with open(out_path, "w") as fh: # skylos: ignore[SKY-D324] folder is not sharedAccept the writes you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D324"]turns the rule off for the whole project.