SKY-D348 · Security in code
Insecure temporary filename
The shared temporary folder (/tmp on Linux and macOS) can be written by every user and process on the machine. SKY-D348 reports Python code that picks a temporary name first and creates the file later, with tempfile.mktemp(), or that reuses a fixed, guessable folder under /tmp with exist_ok=True.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
Between choosing the name and opening it, another local user can create that path as a symlink to a file of yours, such as ~/.ssh/authorized_keys. Your program then writes into their target, or reads the data they planted. A fixed folder like /tmp/report-export can simply be created by someone else first.
What Skylos flags
import subprocessimport tempfile def render_pdf(html): path = tempfile.mktemp(suffix=".html") with open(path, "w") as handle: handle.write(html) subprocess.run(["wkhtmltopdf", path, path + ".pdf"], check=True) return path + ".pdf"What Skylos leaves alone
import subprocessimport tempfile def render_pdf(html): with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as handle: handle.write(html) subprocess.run(["wkhtmltopdf", handle.name, handle.name + ".pdf"], check=True) return handle.name + ".pdf"Why Skylos leaves it alone
NamedTemporaryFile() creates and opens the file in one step, with a random name and owner-only permissions, so nobody can slip a symlink in first. Only mktemp() is reported among the tempfile functions.
More languages
Python
Flagged
import osimport tempfile def export_report(rows): work_dir = os.path.join(tempfile.gettempdir(), "report-export") os.makedirs(work_dir, exist_ok=True) path = os.path.join(work_dir, "report.csv") with open(path, "w") as handle: handle.writelines(rows) return pathNot flagged
import osimport tempfile def export_report(rows): work_dir = tempfile.mkdtemp(prefix="report-export-") path = os.path.join(work_dir, "report.csv") with open(path, "w") as handle: handle.writelines(rows) return pathWhy Skylos leaves it alone
mkdtemp() creates a new folder with a random name that only the current user can open. The flagged version accepts a folder that any local user could have created, or replaced with a symlink, before the program ran.
How Skylos decides
- Reports every call to
tempfile.mktemp(), resolved through imports, as HIGH. - Finds
os.makedirs(path, exist_ok=True)andPath(...).mkdir(..., exist_ok=True), withexist_okgiven as a literalTrue. - Reports the call when the path is a fixed folder under the shared temporary root: a literal
/tmpor/var/tmp, ortempfile.gettempdir(), followed by constant names throughos.path.join(),Path(...) / "name",joinpath(),+or an f-string, local names,selfattributes set in the same function anda or defaultfallbacks. - Leaves the path alone when any part after the root is random, caller-supplied or unknown, because such a name cannot be guessed in advance.
How to fix it
Let tempfile create the file or folder for you, in one step, with a random name: NamedTemporaryFile(), mkstemp(), mkdtemp() or TemporaryDirectory(). Put fixed file names inside that private folder.
Added: import osimport subprocessimport tempfile Removed: def render_pdf(html):Removed: path = tempfile.mktemp(suffix=".html")Removed: with open(path, "w") as handle:Removed: handle.write(html)Removed: subprocess.run(["wkhtmltopdf", path, path + ".pdf"], check=True)Removed: return path + ".pdf"Added: def render_pdf(html, out_dir):Added: with tempfile.TemporaryDirectory() as work:Added: page = os.path.join(work, "page.html")Added: with open(page, "w") as handle:Added: handle.write(html)Added: pdf = os.path.join(out_dir, "page.pdf")Added: subprocess.run(["wkhtmltopdf", page, pdf], check=True)Added: return pdfWhen it can be wrong
- A single-user container or CI job where nobody else can write
/tmpis still reported; the code is still unsafe on a shared machine. mktemp()used only to invent a name that is passed to a tool which creates the file exclusively itself is reported the same way.
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. A bare
# noqadoes not hide HIGH security findings.Python path = tempfile.mktemp() # skylos: ignore[SKY-D348] socket path, bind() fails if takenAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D348"]turns the rule off for the whole project.