Skip to content

Fix strict lookup in compat mode - #2150

Merged
kibertoad merged 1 commit into
handlebars-lang:masterfrom
theodorejb:fix-compat-strict-lookup
Apr 21, 2026
Merged

Fix strict lookup in compat mode#2150
kibertoad merged 1 commit into
handlebars-lang:masterfrom
theodorejb:fix-compat-strict-lookup

Conversation

@theodorejb

@theodorejb theodorejb commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Fixes #2149, fixes #1741

@theodorejb
theodorejb force-pushed the fix-compat-strict-lookup branch 2 times, most recently from c5900c7 to 0b0f7b3 Compare April 15, 2026 20:02
@theodorejb theodorejb changed the title Fix compiling of strict lookup in compat mode Fix strict lookup in compat mode Apr 15, 2026
@theodorejb
theodorejb force-pushed the fix-compat-strict-lookup branch 2 times, most recently from a6dd9a6 to 2f55d99 Compare April 15, 2026 22:37
@jaylinski
jaylinski requested a review from Copilot April 19, 2026 16:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect strict lookups when compat mode is enabled, preventing TypeError crashes and restoring expected strict error behavior.

Changes:

  • Added strict+compat regression tests covering boolean block contexts, each item lookups, recursion, and null handling.
  • Introduced a runtime helper (container.strictLookup) to safely traverse depths for strict property resolution in compat scenarios.
  • Updated the JS compiler to emit container.strictLookup(...) when compat mode has already performed a depthed lookup.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
spec/strict.js Adds test coverage for strict+compat mode behaviors and regressions.
lib/handlebars/runtime.js Adds container.strictLookup to safely resolve strict properties across depths in compat mode.
lib/handlebars/compiler/javascript-compiler.js Emits container.strictLookup(...) in the strict+compat codegen path to avoid in operator crashes on primitives.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread spec/strict.js
Comment thread lib/handlebars/runtime.js Outdated
@theodorejb
theodorejb force-pushed the fix-compat-strict-lookup branch from 2f55d99 to 255ef4c Compare April 19, 2026 19:48
@kibertoad

Copy link
Copy Markdown
Contributor

One suggestion: add a direct "{{v}} with strict+compat where v doesn't exist at any depth" test. Your "still throw when a property is missing at all depths" covers the same code path but via {{#each items}}, and a minimal top-level case would more clearly regress-guard the originally reported form.

@theodorejb
theodorejb force-pushed the fix-compat-strict-lookup branch from 255ef4c to cd47c94 Compare April 21, 2026 19:16
@theodorejb

Copy link
Copy Markdown
Contributor Author

@kibertoad I rebased now and added a test case for a simple {{v}} template where the variable is missing.

@kibertoad
kibertoad merged commit 3105ca7 into handlebars-lang:master Apr 21, 2026
8 checks passed
@kibertoad

Copy link
Copy Markdown
Contributor

thank you!

@theodorejb
theodorejb deleted the fix-compat-strict-lookup branch April 21, 2026 19:34
theodorejb added a commit to theodorejb/handlebars.js that referenced this pull request Apr 30, 2026
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).
theodorejb added a commit to theodorejb/handlebars.js that referenced this pull request May 17, 2026
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).
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.

Strict lookup doesn't work correctly in compat mode Exception when the strict and compat compiler options are used together

3 participants