-
-
Notifications
You must be signed in to change notification settings - Fork 144
feat: Node-style primordials for runtime builtins #1990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
215 changes: 215 additions & 0 deletions
215
test-app/app/src/main/assets/app/tests/testPrimordials.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| describe("primordials", function () { | ||
| const boom = function () { | ||
| throw new Error("intrinsic tampered"); | ||
| }; | ||
|
|
||
| // Tampering with the intrinsics breaks Jasmine and most of the runtime as | ||
| // well, so the tampered window stays synchronous and assertion-free: | ||
| // results go into locals, the originals come back in a finally, and only | ||
| // then do the expectations run. Nothing inside the window may use an array | ||
| // method or `.call` either — plain indexing and direct calls only. | ||
| function withTampered(patches, body) { | ||
| const originals = []; | ||
| for (let i = 0; i < patches.length; i++) { | ||
| originals[i] = patches[i][0][patches[i][1]]; | ||
| } | ||
| try { | ||
| for (let i = 0; i < patches.length; i++) { | ||
| patches[i][0][patches[i][1]] = boom; | ||
| } | ||
| return body(); | ||
| } finally { | ||
| for (let i = 0; i < patches.length; i++) { | ||
| patches[i][0][patches[i][1]] = originals[i]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const arrayAndCall = [ | ||
| [Array.prototype, "slice"], | ||
| [Array.prototype, "indexOf"], | ||
| [Array.prototype, "push"], | ||
| [Array.prototype, "splice"], | ||
| [Function.prototype, "call"], | ||
| ]; | ||
|
|
||
| it("the tampering used by this suite is actually observable", function () { | ||
| const outcome = withTampered(arrayAndCall, function () { | ||
| try { | ||
| [1, 2].slice(0); | ||
| return "no throw"; | ||
| } catch (e) { | ||
| return e.message; | ||
| } | ||
| }); | ||
|
|
||
| expect(outcome).toBe("intrinsic tampered"); | ||
| expect([1, 2].slice(0).length).toBe(2); | ||
| }); | ||
|
|
||
| it("global dispatchEvent delivers to every listener while intrinsics are tampered", function () { | ||
| const seen = []; | ||
| const first = function (e) { seen[seen.length] = "first:" + e.type; }; | ||
| const second = { handleEvent: function (e) { seen[seen.length] = "second:" + e.type; } }; | ||
| const event = new Event("primordials-dispatch"); | ||
|
|
||
| global.addEventListener("primordials-dispatch", first); | ||
| global.addEventListener("primordials-dispatch", second); | ||
|
|
||
| let dispatchResult; | ||
| try { | ||
| dispatchResult = withTampered(arrayAndCall, function () { | ||
| return global.dispatchEvent(event); | ||
| }); | ||
| } finally { | ||
| global.removeEventListener("primordials-dispatch", first); | ||
| global.removeEventListener("primordials-dispatch", second); | ||
| } | ||
|
|
||
| expect(dispatchResult).toBe(true); | ||
| expect(seen.join(",")).toBe("first:primordials-dispatch,second:primordials-dispatch"); | ||
| }); | ||
|
|
||
| it("addEventListener/removeEventListener and once work while intrinsics are tampered", function () { | ||
| const calls = []; | ||
| const persistent = function () { calls[calls.length] = "persistent"; }; | ||
| const onceOnly = function () { calls[calls.length] = "once"; }; | ||
|
|
||
| try { | ||
| withTampered(arrayAndCall, function () { | ||
| global.addEventListener("primordials-registration", persistent); | ||
| global.addEventListener("primordials-registration", onceOnly, { once: true }); | ||
| global.dispatchEvent(new Event("primordials-registration")); | ||
| global.dispatchEvent(new Event("primordials-registration")); | ||
| global.removeEventListener("primordials-registration", persistent); | ||
| global.dispatchEvent(new Event("primordials-registration")); | ||
| }); | ||
| } finally { | ||
| global.removeEventListener("primordials-registration", persistent); | ||
| global.removeEventListener("primordials-registration", onceOnly); | ||
| } | ||
|
|
||
| expect(calls.join(",")).toBe("persistent,once,persistent"); | ||
| }); | ||
|
|
||
| it("reportError still reaches an error listener while intrinsics are tampered", function () { | ||
| let received = null; | ||
| // preventDefault keeps the unhandled tail (which aborts the process) | ||
| // out of the picture. | ||
| const onError = function (e) { | ||
| received = e; | ||
| e.preventDefault(); | ||
| }; | ||
| const error = new Error("primordials-report"); | ||
|
|
||
| global.addEventListener("error", onError); | ||
| try { | ||
| withTampered(arrayAndCall, function () { | ||
| global.reportError(error); | ||
| }); | ||
| } finally { | ||
| global.removeEventListener("error", onError); | ||
| } | ||
|
|
||
| expect(received).not.toBeNull(); | ||
| expect(received.type).toBe("error"); | ||
| expect(received.error).toBe(error); | ||
| expect(received.message).toBe("primordials-report"); | ||
| }); | ||
|
|
||
| it("console.log of a circular object neither throws nor crashes with JSON.stringify tampered", function () { | ||
| // The smart-stringify builtin both calls JSON.stringify and tracks | ||
| // already-visited objects with Array.prototype.indexOf/push. Its output | ||
| // is not reachable from JS and JsonStringifyObject swallows a throwing | ||
| // stringify, so this only pins down that the tampered path stays | ||
| // non-fatal; the primordial routing itself is covered by review. | ||
| const circular = { name: "primordials" }; | ||
| circular.self = circular; | ||
|
|
||
| let threw = null; | ||
| try { | ||
| withTampered([ | ||
| [JSON, "stringify"], | ||
| [Array.prototype, "indexOf"], | ||
| [Array.prototype, "push"], | ||
| ], function () { | ||
| console.log(circular); | ||
| }); | ||
| } catch (e) { | ||
| threw = e; | ||
| } | ||
|
|
||
| expect(threw).toBeNull(); | ||
| }); | ||
|
|
||
| it("the searchParams accessor works while Object.defineProperty is tampered", function () { | ||
| const url = new URL("https://example.com/path?a=1"); | ||
|
|
||
| let readBack = null; | ||
| let searchAfterAppend = null; | ||
| let threw = null; | ||
| try { | ||
| withTampered([[Object, "defineProperty"]], function () { | ||
| const params = url.searchParams; | ||
| readBack = params.get("a"); | ||
| params.append("b", "2"); | ||
| searchAfterAppend = url.search; | ||
| }); | ||
| } catch (e) { | ||
| threw = e; | ||
| } | ||
|
|
||
| expect(threw).toBeNull(); | ||
| expect(readBack).toBe("1"); | ||
| expect(searchAfterAppend).toBe("?a=1&b=2"); | ||
| }); | ||
|
|
||
| it("revokeObjectURL and InternalAccessor.getData work while Map methods are tampered", function () { | ||
| let data; | ||
| let threw = null; | ||
| try { | ||
| withTampered([ | ||
| [Map.prototype, "get"], | ||
| [Map.prototype, "set"], | ||
| [Map.prototype, "delete"], | ||
| ], function () { | ||
| URL.revokeObjectURL("blob:nativescript/primordials-missing"); | ||
| data = URL.InternalAccessor.getData("blob:nativescript/primordials-missing"); | ||
| }); | ||
| } catch (e) { | ||
| threw = e; | ||
| } | ||
|
|
||
| expect(threw).toBeNull(); | ||
| expect(data).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("org.json.JSONObject.from works while the intrinsics json-helper uses are tampered", function () { | ||
| const source = { | ||
| text: "primordials", | ||
| when: new Date(1570696661136), | ||
| list: [1, 2], | ||
| }; | ||
|
|
||
| let converted = null; | ||
| let threw = null; | ||
| try { | ||
| withTampered([ | ||
| [Array, "isArray"], | ||
| [Array.prototype, "forEach"], | ||
| [Object, "keys"], | ||
| [Date.prototype, "toJSON"], | ||
| ], function () { | ||
| converted = org.json.JSONObject.from(source); | ||
| }); | ||
| } catch (e) { | ||
| threw = e; | ||
| } | ||
|
|
||
| expect(threw).toBeNull(); | ||
| expect(converted instanceof org.json.JSONObject).toBe(true); | ||
| expect(converted.getString("text")).toBe("primordials"); | ||
| expect(converted.getString("when")).toBe("2019-10-10T08:37:41.136Z"); | ||
| expect(converted.getJSONArray("list").length()).toBe(2); | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: NativeScript/android
Length of output: 1978
🏁 Script executed:
Repository: NativeScript/android
Length of output: 41449
🏁 Script executed:
Repository: NativeScript/android
Length of output: 407
Exercise blob insertion and retrieval while
Map.prototypemethods are replaced.This test only revokes and reads a missing URL. Create a
Bloband object URL insidewithTampered, verify the stored data, revoke the URL, and verify thatURL.InternalAccessor.getDatareturnsundefined.🤖 Prompt for AI Agents