Skip to main content

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

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

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

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

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

Not flagged

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

Why 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

  1. Reports every call to tempfile.mktemp(), resolved through imports, as HIGH.
  2. Finds os.makedirs(path, exist_ok=True) and Path(...).mkdir(..., exist_ok=True), with exist_ok given as a literal True.
  3. Reports the call when the path is a fixed folder under the shared temporary root: a literal /tmp or /var/tmp, or tempfile.gettempdir(), followed by constant names through os.path.join(), Path(...) / "name", joinpath(), + or an f-string, local names, self attributes set in the same function and a or default fallbacks.
  4. 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.

app/pdf.py · Python · the fix, against the flagged file
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 pdf

When it can be wrong

  • A single-user container or CI job where nobody else can write /tmp is 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 # noqa does not hide HIGH security findings.

    Python
    path = tempfile.mktemp()  # skylos: ignore[SKY-D348] socket path, bind() fails if taken
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

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

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