Skip to content

Fix explicit null value handling in compat mode - #2151

Merged
kibertoad merged 1 commit into
handlebars-lang:masterfrom
theodorejb:fix-compat-explicit-null
Jun 14, 2026
Merged

Fix explicit null value handling in compat mode#2151
kibertoad merged 1 commit into
handlebars-lang:masterfrom
theodorejb:fix-compat-explicit-null

Conversation

@theodorejb

@theodorejb theodorejb commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Follow-up to GH-2150.

container.lookup used result != null which caused explicit null values to fall through to parent contexts. This was inconsistent with expected behavior and with Mustache, which stops and returns a key when present in the object, regardless of its value. The whole point of compat mode is to match Mustache's lookup semantics, so I don't think this behavior was intended.

There were no tests for compat with an explicit null value, apart from a couple compat + strict mode tests I just added myself in GH-2150. One of these tests ("should still perform recursive lookup with a multi-part path not in context") was inconsistent with the test immediately below it ("should directly return an explicitly null property"), and that is fixed here.

This change makes it possible for unify the lookup logic in container.lookup and container.strictLookup, additionally fixing an issue where strictLookup didn't respect the allowedProtoMethods runtime option the same as lookup (see the added test in spec/security.js compared to the test above it).

Also fixed compat mode Map value resolution, and included the test for it from #2156 (if that is merged first, I can rebase this to fix the merge conflict).

@theodorejb
theodorejb force-pushed the fix-compat-explicit-null branch from b54ff13 to 51f0151 Compare April 30, 2026 02:32
theodorejb added a commit to devtheorem/php-handlebars that referenced this pull request Apr 30, 2026
I also opened handlebars-lang/handlebars.js#2151 to apply the same fix to Handlebars.js.
Follow-up to handlebars-langGH-2150.

`container.lookup` used `result != null` which caused explicit null values to
fall through to parent contexts. This was inconsistent with expected behavior
and with Mustache, which stops and returns a key when present in the object,
regardless of its value. The whole point of `compat` mode is to match Mustache's
lookup semantics, so I don't think this behavior was intended.

There were no tests for `compat` with an explicit null value, apart from a
couple `compat` + `strict` mode tests I just added myself in handlebars-langGH-2150.
One of these tests ("should still perform recursive lookup with a multi-part
path not in context") was inconsistent with the test immediately below it
("should directly return an explicitly null property"), and that is fixed here.

This change makes it possible for unify the lookup logic in `container.lookup`
and `container.strictLookup`, additionally fixing an issue where `strictLookup`
didn't respect the `allowedProtoMethods` runtime option the same as `lookup`
(see the added test in `spec/security.js` compared to the test above it).

This also fixes Map value resolution in compat-mode depthed lookup
(included test by @TorMatzAndren from handlebars-lang#2156).
@theodorejb
theodorejb force-pushed the fix-compat-explicit-null branch from 51f0151 to acaa839 Compare May 17, 2026 17:30
@theodorejb

Copy link
Copy Markdown
Contributor Author

@jaylinski @kibertoad By any chance can this be reviewed?

@kibertoad
kibertoad merged commit ad6397a into handlebars-lang:master Jun 14, 2026
8 checks passed
@kibertoad

Copy link
Copy Markdown
Contributor

thank you!

@theodorejb
theodorejb deleted the fix-compat-explicit-null branch June 14, 2026 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants