XF 2.3 PSA: Code that runs during template rendering and modifies container values must be modified for XenForo 2.2.18+ / 2.3.9+

Kirby

Well-known member
Licensed customer
XenForo 2.2.18 / 2.3.9 intoduce a new security mechanisam for accessing the App container.
If there is code that runs during template rendering and modifies container values, this code must be adapted.

@Chris D
This is IMHO a somewhat breaking change, might be worth to note this in the release announcement.
 
Last edited:
It is absolutely a breaking change and I got part way through writing this very PSA but didn’t get a chance to finish it. So thank you!

It was all a bit hectic yesterday. Obviously such a change would usually be communicated days or weeks in advance.
 
Does this mean it is necessary to do really intensive testing (beyond a short functional test) with 2.3.9 on a clone of the own configuration before deploying it to the actual forum? Especially with a bunch of add ons installed? Or is this something normal users do not have to care about?
 
It totally depends on the Add-ons you use, if those modify app container values during rendering they will break with XenForo 2.3.9 /2.2.18 until they are modified.

However I'd say the average Add-on most likely doesn't do that.
 
With some help from Claude.code, I found mine.

Look for add-on code that saves a value into XenForo’s app container while a page is being drawn. Reading from the container is still fine; only writing breaks.

1. Code that writes to the container

Any of these is a write:

Pattern Example
Array-style assignment $app['foo'] = …, $container['foo'] = …, \XF::app()['foo'] = …
offsetSet \XF::app()->offsetSet('foo', true) (this is what the addon I found did)
Container methods ->set(, ->extend(, ->factory(, ->extendFactory(, ->removeFactory( on the container
Removing a key unset($app['foo']), ->offsetUnset(
Property-style assignment on the app \XF::app()->foo = … (goes through __set and writes to the container)

Watch out for ->set(: it also matches harmless calls like $entity->set(), $session->set(), $request->set() and registry()->set(). Only calls on the container or the app count.

2. Whether that code runs during rendering

A write is only a problem if it happens while a template, macro or template extension is rendering.

Safe (runs before rendering):

app_setup, app_pub_setup, app_admin_setup listeners. Nearly all the container writes in your add-ons are here.
Controller actions, services, repositories and cron jobs, as long as the controller calls them before it returns its view.

Risky (runs during rendering):

Templater functions and filters: fn… / filter… methods in classes that extend XF\Template\Templater, or anything added with addFunction / addFilter in a templater_setup listener.
Listeners on templater_template_pre_render, templater_template_post_render, templater_macro_pre_render and templater_macro_post_render. Nested templates render under the outer page’s lock.
Widget render() methods.
Methods called from <xf:callback> in templates.
Entity getters and methods that templates call, such as {{ $thread.getSomething() }}.
BB code renderers, ad and advertising position code, and anything else that builds HTML while a template is rendering.
3. The telltale pattern

Watch for the container being used as a “did I already do this?” flag or as a per-request cache, set from inside one of the risky places above. That’s the LAU bug exactly. The usual fixes:

Use a static property on the class instead (what I did for LAU).
Or register the value once in an app_setup listener and only read it later.
4. A quick grep for a new add-on

Run this from the add-on’s folder:

grep -rnE "offsetSet|offsetUnset|extendFactory|removeFactory|->extend\(|container\(\)->set\(|(\\\$app|\\\$container|\\\$c|app\(\))\[[^]]+\]\s*=[^=>]|XF::app\(\)->\w+\s*=[^=>]|unset\(\s*\\\$(app|container)\[" --include=*.php .

For each hit, open the file and ask whether it’s inside an app_setup-type listener. If yes, it’s fine. If it’s in a Templater extension, widget, template listener or callback, it’s a likely breaker.

5. Other signs
The add-on’s changelog or release notes mention “2.2.18”, “2.3.9” or “container locked”. Many vendors released fixes for this.
The add-on hasn’t been updated since before 2.2.18 and extends XF\Template\Templater. That’s worth a closer look.
After an upgrade, the server error log shows “Cannot modify container while locked”, and the stack trace names the add-on file. That’s the definitive check.
 
Back
Top Bottom