Skip to main content
Menu
Advisories

Overview Data passed to one render could reach other renders in three ways, all fixed in 2.9.6

GHSA-25W8-77GG-4CCWPublished Updated GitHub

Description

Data passed to one render could reach other renders in three ways, all fixed in 2.9.6.

1. Locals in the options of a cached compile. With the default memory cache (cache: 'memory'), or a custom { get, set } cache object, compile(source, options) caches the compiled template under options.filename, and the cached function kept the locals given in the options of the call that created it. A later render, compile or compileFile of the same file returned that function: render(source, { filename, locals }) rendered the first call's locals on every later call, and a later caller that passed its own locals got the first call's locals merged under them. The same function also served a later renderFile of that file and an {% include %} of it from another template. render(source, { filename, locals }) is the shape the API documentation shows for rendering a template string whose include, extends or import must resolve: if an application renders a page from a string with filename set and the current user's data in locals, every later request for that page shows the first user's data until the cache is cleared or the process restarts. This case does not arise with cache: false, in the runtime-only build, or when every call passes its data when the template runs (renderFile(path, locals), compile(source, options)(locals)) and no call puts locals in the options of a compile of that file.

2. An instance's default locals. A render without call-time locals on an instance with default locals (new Swig({ locals }) or setDefaults({ locals })), and run(tpl) without locals, used the defaults object itself as the template's context. The names a template set ({% set %}, macros, imports, loop variables, Django {% cycle %} state) stayed in that object and reached every later render of every template on that instance, and instances built without their own locals share one defaults object, so run on one instance reached the others. With a request-scoped helper in the default locals, such as one that reads the current user from AsyncLocalStorage, one of two concurrent renders on the async render path showed the other's data.

3. A locals object passed to more than one render. On an instance without default locals, a render used the locals object the caller passed (renderFile(path, locals), compileFile(path)(locals), compile(source)(locals), a template stored with register) as the template's context, so the names the template set landed in that object: a later render given the same object read them, and two concurrent renders on the async render path given one object read each other's. With a request-scoped helper in that object, one user's page showed another user's data.

Impact

Data passed to one render, often the data of one user, appears in renders made for other callers. How much this matters depends on what an application passes in its locals, whether it passes one object to several renders, and whether its default locals hold request-scoped values.

Affected & patched

Every release of @rhinostone/swig-core, @rhinostone/swig, @rhinostone/swig-twig, @rhinostone/swig-jinja2 and @rhinostone/swig-django before 2.9.6; the code dates from the original swig 1.4.2. The concurrent cases need the async render path: native swig and swig-twig have it since 2.2.0 and swig-jinja2 since its first release, 2.5.0; in swig-django they need a loop holding an include, which renders on that path since 2.9.4.

Fixed in 2.9.6:

  • the cache keeps a function without the locals of the call that created it, and each compile or compileFile call that passes locals gets a function that applies them, each call starting from a fresh context;
  • a render without call-time locals, and run without locals, work on a copy of the default locals;
  • a render works on a copy of the locals object the caller passes, made at its top. A template rendered as part of the render, such as an include, still shares its includer's context, so an include's sets still reach the page.

Three cases still write into the object they are given: a template rendered from inside another render of the same instance (a helper that renders a partial with an object of its own), an include given an object with with … only, and a dotted set such as {% set site.user = who %}, since the copy is shallow. The fixes change how the engine passes locals to a template, not the code it generates for one. The 2.9.6 frontends require @rhinostone/swig-core 2.9.6, so upgrade the packages together.

Workarounds

  • Upgrade to 2.9.6 or later.
  • Otherwise, give every render, run included, a fresh locals object of its own (on an instance with default locals, a render given call-time locals already worked on a merged copy), pass the data when the template runs (compile(source, { filename })(locals) or renderFile(path, locals)) instead of in the options of a compile, or set cache: false.

References

Affected versions

References

GitHub · gina-io/swig