Skip to content

Speed up checking allowed methods and properties in SecurityPolicy - #5031

Open
Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/sandbox-policy-memo
Open

Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/sandbox-policy-memo

Conversation

@Kocal

@Kocal Kocal commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

In a sandboxed template, each attribute read on an object looped over all allowed classes with instanceof and in_array().

The policy now remembers the allowed (class, member) pairs, because the answer only depends on those two. Denials are never remembered and throw as before, and setAllowedMethods() and setAllowedProperties() clear the memo.

With a policy of 20 classes, checkMethodAllowed() goes from 962-982 -> 58-67 ns. A sandboxed render with 900 attribute reads goes from 4.772-5.012 -> 3.885-4.056 ms. The new SecurityPolicyTest passes on 3.x too.

In a sandboxed template, each attribute read on an object looped over all allowed classes with `instanceof` and `in_array()`.

The policy now remembers the allowed (class, member) pairs, because the answer only depends on those two. Denials are never remembered and throw as before, and `setAllowedMethods()` and `setAllowedProperties()` clear the memo.

With a policy of 20 classes, `checkMethodAllowed()` goes from 962-982 -> 58-67 ns. A sandboxed render with 900 attribute reads goes from 4.772-5.012 -> 3.885-4.056 ms. The new `SecurityPolicyTest` passes on 3.x too.
@Kocal
Kocal force-pushed the perf/sandbox-policy-memo branch from 7b46a1a to ed84664 Compare November 15, 2026 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants