Skip to main content
Menu
Advisories

Overview With the default memory cache (cache: 'memory'), a compiled template keeps its parse tree, and a page that extends that template used the cached parse tree as its parent

GHSA-G628-5X3R-G298Published Updated GitHub

Description

With the default memory cache (cache: 'memory'), a compiled template keeps its parse tree, and a page that extends that template used the cached parse tree as its parent. Compiling the page rewrites its parent's tree to put the page's block overrides in place. For a block nested inside another block, or inside another tag with a body (an if, a for loop, a filter, a with), that rewrite landed in the cached tree itself. From then on, every page compiled later that extends the same layout without overriding that block rendered the first page's block, until the cache was cleared or the process restarted.

This happens once the layout is in the compile cache, that is once it was compiled with compileFile, rendered on its own or included by another template, or, in a chain of three levels, once the middle template was rendered as a page. For example, take a layout whose outer block holds a nested side block, an administration page that overrides side with content meant for administrators, and public pages that extend the same layout without overriding side: once the administration page has been compiled, the public pages show the administrators' content.

The leaked block is rendered with the data of the page that shows it, not with the data of the page it came from, so no data from another request is exposed; what is exposed is template content written for another page. No special input is needed: the leak follows from the order in which the process first compiles its pages.

In native swig the same shared parent also made {% parent %} in a middle template that was in the compile cache render that template's block twice.

Impact

Content written for one page, for example a block that only administrators should see, shows on other pages, and those pages render something other than what their templates say. How much this matters depends on what an application's templates put in nested blocks.

Affected & patched

Every release of @rhinostone/swig-core, @rhinostone/swig, @rhinostone/swig-twig, @rhinostone/swig-jinja2 and @rhinostone/swig-django before 2.9.5; the code dates from the original swig 1.4.2.

Fixed in 2.9.5: a parent taken from the compile cache is copied before its blocks are remapped, so the cached tree is no longer rewritten, and the copy carries the parent's file name, which also fixes the doubled {% parent %}. Templates that are not in the compile cache, and renders with cache: false, are unchanged. The 2.9.5 frontends require @rhinostone/swig-core 2.9.5, so upgrade the packages together.

Workarounds

  • Upgrade to 2.9.5 or later.
  • Otherwise, set cache: false (every render then compiles its templates again), or do not override blocks that are nested inside another block or tag.

References

Affected versions

References

GitHub · gina-io/swig