Replace non-essential eval usage with explicit registries for static lookups (palettes + JS export)#5863
Closed
Chaitu7032 wants to merge 2 commits intosugarlabs:masterfrom
Closed
Replace non-essential eval usage with explicit registries for static lookups (palettes + JS export)#5863Chaitu7032 wants to merge 2 commits intosugarlabs:masterfrom
Chaitu7032 wants to merge 2 commits intosugarlabs:masterfrom
Conversation
Contributor
|
✅ All Jest tests passed! This PR is ready to merge. |
Contributor
Author
|
@walterbender sir , @omsuneri |
Contributor
|
✅ All Jest tests passed! This PR is ready to merge. |
shashank03-dev
suggested changes
Feb 22, 2026
Contributor
Author
|
Sure will take a look on that |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
This PR removes eval from a small number of locations where it was used only for static, name-based reflection (palette image constants and API action class enumeration/dispatch).
The change improves auditability, maintainability, and CSP readiness, while preserving existing runtime behavior and public APIs.
No dynamic or user-controlled code execution paths are modified.
Scope / Non-goals (explicit)
In scope
Out of scope / unchanged
What changed
1) Palette built-in image selection (remove static
eval)File: js/palette.jsBefore
Built-in palette icons for media, camera, and video were resolved using:
eval(b.blkname + "PALETTE")After
Introduces an explicit, lazy registry:
const BUILTIN_IMAGE_PALETTE_REGISTRY = { media: () => mediaPALETTE, camera: () => cameraPALETTE, video: () => videoPALETTE };Palette rendering now:
Why this is safe
2) JS export API enumeration & dispatch (replace reflective eval)
File: js/js-export/export.jsBefore
API enumeration used:
-
eval(className + ".prototype")(Painter)-
eval(className)(Singer.*Actions, Turtle.DictActions)runCommand()usedeval(cname)to resolve the action objectAfter
Introduces a hoisted, explicit registry:
STATIC_API_CLASS_REGISTRY: string → resolver functionEnumeration logic:
PainterusesPainter.prototypedirectlyDispatch (
runCommand):"Painter"continues routing tothis.turtle.painter(unchanged)1] the registry (preferred)
2] a backward-compatible fallback resolveGlobalByPath(cname) (no eval)
Hardening:
resolveGlobalByPathrejects non-strings and values not matching^[A-Za-z0-9_.]+$ReferenceErrorotherwiseWhy this is safe
ReferenceErrorin environments whereglobalsmay load later or be absent in testsProblem observed
When loading an HTML-exported project, the embedded JSON inside
<div class="code">...</div>is HTML-escaped(e.g., quotes become ").The loader path attempted
[JSON.parse()]directly on that escaped string, causing:[SyntaxError: Unexpected token '&', ""..." is not valid JSON]------------ ### fixed in #5808Note - please check #5808 before this merge .. for 👆 fix
tests
Summary
This PR removes non-essential eval usage in static lookup paths, improves clarity and security posture, and preserves existing behavior by design. It is intentionally scoped and avoids changes to dynamic execution or plugins.