Module loader ignores import attributes: JSON modules fail, and `with { type: "json" }` executes a JavaScript file

Đang mở
#356 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c
Lĩnh vực
backend

Hướng nghiên cứu

Start in runtime/script_loader.cpp at module_load_hook, then read how get_module compiles modules and how moduleRegistry keys them. Compare that flow with SpiderMonkey's GetModuleRequestType and CompileJsonModule APIs cited in the issue. Run the listed JSON import reproductions and test262 json-* tests; done means JSON imports export parsed values, invalid JSON and unknown types error, and imports with different attributes remain distinct.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

module_load_hook never looks at the import attributes of a request, so every module is compiled as JavaScript:

  • import data from "./data.json" with { type: "json" } fails (an object is parsed as a JS block; an array has no
    default export).
  • A file imported with type: "json" that actually contains JavaScript is executed.
  • Unknown type values are accepted silently.

Environment

  • StarlingMonkey v0.4.0 release starling.wasm, wasmtime 50.0.1, module mode (wasmtime run -S http --dir . starling.wasm main.js).
  • runtime/script_loader.cpp is unchanged on main (54b3329b).
  • Checked on 2026-11-03. Reference: SpiderMonkey jsshell 147.0.4 (the SpiderMonkey version StarlingMonkey v0.4.0 embeds), js -m.

Reproduction

data.json: {"a": 1, "n": -1.5e-3} · arr.json: [1,2] · evil.json: console.log("JS CODE EXECUTED"); export default 42;

// main.js
import obj from "./data.json" with { type: "json" };
console.log(JSON.stringify(obj));
import StarlingMonkey jsshell 147.0.4
./data.json with type: "json" data.json:1:5 SyntaxError: unexpected token: ':' {"a":1,"n":-0.0015}
./arr.json with type: "json" SyntaxError: The requested module 'arr.json' doesn't provide an export named: 'default' [1,2]
./evil.json with type: "json" prints JS CODE EXECUTED, then imported 42 SyntaxError: JSON.parse: unexpected character at line 1 column 1
./b.js with type: "bogus" attribute ignored TypeError: invalid module type

test262: language/import/import-attributes/json-value-object.js, json-idempotency.js and the other json-* tests in
that directory fail.

Expected

ECMA-262 HostLoadImportedModule: "If moduleRequest.[[Attributes]]
has an entry entry such that entry.[[Key]] is "type" and entry.[[Value]] is "json", … result must either be the
Completion Record returned by an invocation of ParseJSONModule or a
throw completion." So a JSON import yields a module whose default export is the parsed value, and non-JSON content
is an error, never executed. ModuleRequestsEqual also compares
attributes, so ./x with and without type: "json" are different requests.

The point of type: "json" is that importing data cannot run code; currently that guarantee does not hold.

Cause

module_load_hook in runtime/script_loader.cpp resolves the specifier and always calls get_module(...), which
compiles the source as a JavaScript module. It never reads the request's module type, and moduleRegistry is keyed by
resolved path only.

Suggested fix (sketch)

In module_load_hook, read the module type from the request (JS::GetModuleRequestType(cx, moduleRequest) returns
JS::ModuleType::JSON for type: "json"). For JSON, compile with JS::CompileJsonModule(cx, opts, source), as
jsshell's ModuleLoader::loadAndParse does. Key the registry by (resolved path, module type), and reject unknown
types with a TypeError.

Ngôn ngữ chính
C
Star
297
Fork
64
Merge trung bình
2 ngày 2 giờ
Pull request đã merge (30 ngày)
19

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bytecodealliance/StarlingMonkey

Tất cả issue của bytecodealliance/StarlingMonkey

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.