Skip to main content
Menu
Advisories

@rhinostone/swig-core: code execution via an unescaped template filename in the compiled template body

GHSA-Q89C-947C-M7XRPublished Updated GitHub

Description

swig compiles every template into a JavaScript function via new Function(...). When a caller passes a filename option to render, compile or precompile (or a flavor equivalent), the value is stored on the compiled include / extends / import nodes as resolveFrom and concatenated into a double-quoted string literal in the generated function body without escaping its double quotes or line breaks; renderFile and compileFile set the same value from the resolved template path. A filename containing a double-quote terminates that literal, so the rest of the value is parsed as JavaScript and runs whenever the compiled template is rendered — arbitrary code execution in the Node.js process. The flaw is in the code generator (@rhinostone/swig-core), independent of the include path-traversal issue GHSA-2mf3-mr2r-r4vf, and is present in every published release through 2.8.2 (and in the pre-2.0 1.x line).

Attack scenario

The precondition is that the application passes attacker-influenced data as the filename option — the same class of application-level precondition as GHSA-2mf3-mr2r-r4vf. An application that keys a template cache or error reporting on a request-derived identifier and passes that identifier as filename is exposed. A filename whose value contains a double-quote breaks out of the resolveFrom literal in the compiled body; the injected code becomes part of the compiled function and runs when the template renders, and because compiled templates are cached under their filename, it runs again on every later render that reuses that cache entry. render / compile / precompile take filename directly from the caller; renderFile / compileFile derive it from the resolved path.

Impact

Arbitrary JavaScript execution with the Node.js process's privileges: file read/write, command execution, environment and secret access, outbound network, process termination. Confidentiality, integrity and availability are all affected.

Affected & patched

Affected: @rhinostone/swig (the 1.x line and 2.x through 2.8.2), and the shared @rhinostone/swig-core code generator — hence @rhinostone/swig-twig, @rhinostone/swig-jinja2 and @rhinostone/swig-django — all < 2.8.3. Fixed in 2.8.3 (all five packages, lockstep): the code generator escapes resolveFrom (backslash, line breaks, double-quote) at every emission site — the single chokepoint through which all include / extends / import / from nodes pass — so no filename can terminate the generated literal. The same change fixes a latent bug for backslash (Windows) paths through import / from / extends on the async render path.

Workarounds

  • Upgrade to 2.8.3.
  • If you cannot upgrade: do not pass untrusted or request-derived data as the filename option to render / compile / precompile (or a flavor equivalent); keep a request-derived identifier in a separate application structure and pass a fully server-controlled string as filename.

References

Affected versions

References

GitHub · gina-io/swig