package pluginkit import ( "bytes" "context" "crypto/ed25519" "crypto/rand" "crypto/rsa" "crypto/sha256" "crypto/x509" "encoding/base64" "encoding/hex" "encoding/json" "encoding/pem" "errors" "io" "math" "net/http" "net/url" "runtime" "strings" "sync/atomic" "testing" "time" "unicode/utf8" "unsafe" "weak" "github.com/MoeForge/moejs" "github.com/golang-jwt/jwt/v5" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestEngineCallsESMExportWithInjectedUtils(t *testing.T) { t.Parallel() logs := make([]string, 0, 1) engine, err := Compile(` export function sign(ctx) { console.log("called", ctx.name); return { now: utils.unixNow(), digest: utils.hmacSHA256(ctx.message, ctx.secret), encoded: utils.base64(ctx.message), }; } export const meta = { apiVersion: 1, key: "mock" }; `, Options{ Key: "mock", Version: "1.0.0", Now: func() time.Time { return time.Unix(1234, 0) }, Log: func(message string) { logs = append(logs, message) }, }) require.NoError(t, err) result, err := engine.Call(context.Background(), "sign", map[string]any{ "name": "fixture", "message": "hello", "secret": "secret", }) require.NoError(t, err) assert.Equal(t, map[string]any{ "now": int64(1234), "digest": "88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b", "encoded": "aGVsbG8=", }, result) assert.Equal(t, []string{"[plugin:mock@1.0.0] called fixture"}, logs) meta, err := engine.Export(context.Background(), "meta") require.NoError(t, err) assert.Equal(t, map[string]any{"apiVersion": int64(1), "key": "mock"}, meta) } // TestUtilsSigningReproducesPublishedVectors rebuilds published request // signatures in plugin JavaScript from utils.sha256 and utils.hmacSHA256, with // the chained signing keys passed on as bytes. func TestUtilsSigningReproducesPublishedVectors(t *testing.T) { t.Parallel() tests := []struct { name string source string wantCanonicalHash string wantSignature string }{ { // The AWS Signature Version 4 example (IAM ListUsers) of the AWS // General Reference, archived at // https://web.archive.org/web/20220119151157/http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html // and https://web.archive.org/web/20220307204322/https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html name: "AWS SigV4", source: ` export function sign() { const canonicalRequest = [ "GET", "/", "Action=ListUsers&Version=2010-05-08", "content-type:application/x-www-form-urlencoded; charset=utf-8\nhost:iam.amazonaws.com\nx-amz-date:20150830T123600Z\n", "content-type;host;x-amz-date", utils.sha256(""), ].join("\n"); const canonicalHash = utils.sha256(canonicalRequest); const stringToSign = ["AWS4-HMAC-SHA256", "20150830T123600Z", "20150830/us-east-1/iam/aws4_request", canonicalHash].join("\n"); let signingKey = utils.hmacSHA256("20150830", "AWS4wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY", "bytes"); for (const part of ["us-east-1", "iam", "aws4_request"]) { signingKey = utils.hmacSHA256(part, signingKey, "bytes"); } return {canonicalHash, signature: utils.hmacSHA256(stringToSign, signingKey)}; }`, wantCanonicalHash: "f536975d06c0309214f805bb90ccff089219ecd68b2577efef23edd43b7e1a59", wantSignature: "5d672d79c15b13162d9279b0855cfba6789a8edb4c82c400e06b5924a6f2b5d7", }, { // The TC3-HMAC-SHA256 example (CVM DescribeInstances) of Tencent // Cloud's signature v3 document, archived with its EXAMPLE keys // unmasked at // https://web.archive.org/web/20190822032928/https://cloud.tencent.com/document/api/213/30654 name: "Tencent Cloud TC3", source: ` export function sign() { const payload = '{"Limit": 1, "Filters": [{"Values": ["\\u672a\\u547d\\u540d"], "Name": "instance-name"}]}'; const canonicalRequest = [ "POST", "/", "", "content-type:application/json; charset=utf-8\nhost:cvm.tencentcloudapi.com\n", "content-type;host", utils.sha256(payload), ].join("\n"); const canonicalHash = utils.sha256(canonicalRequest); const stringToSign = ["TC3-HMAC-SHA256", "1551113065", "2019-02-25/cvm/tc3_request", canonicalHash].join("\n"); let signingKey = utils.hmacSHA256("2019-02-25", "TC3Gu5t9xGARNpq86cd98joQYCN3EXAMPLE", "bytes"); for (const part of ["cvm", "tc3_request"]) { signingKey = utils.hmacSHA256(part, signingKey, "bytes"); } return {canonicalHash, signature: utils.hmacSHA256(stringToSign, signingKey)}; }`, wantCanonicalHash: "5ffe6a04c0664d6b969fab9a13bdab201d63ee709638e2749d62a09ca18d7031", wantSignature: "72e494ea809ad7a8c8f7a4507b9bddcbaa8e581f516e8da2f66e2c5a96525168", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { t.Parallel() engine, err := Compile(test.source, Options{Key: "sign", Version: "1"}) require.NoError(t, err) result, err := engine.Call(t.Context(), "sign") require.NoError(t, err) assert.Equal(t, map[string]any{"canonicalHash": test.wantCanonicalHash, "signature": test.wantSignature}, result) }) } } func TestUtilsSignRS256BuildsVerifiableJWT(t *testing.T) { t.Parallel() key, err := rsa.GenerateKey(rand.Reader, 2048) require.NoError(t, err) pkcs8, err := x509.MarshalPKCS8PrivateKey(key) require.NoError(t, err) engine, err := Compile(` export function jwt(ctx) { const header = utils.base64URL(JSON.stringify({alg: "RS256", typ: "JWT"})); const claims = utils.base64URL(JSON.stringify({ iss: "signer@example.iam.gserviceaccount.com", scope: "https://www.googleapis.com/auth/cloud-platform", aud: "https://oauth2.googleapis.com/token", iat: 1762128000, exp: 1762131600, })); const input = header + "." + claims; return input + "." + utils.signRS256(input, ctx.privateKey); }`, Options{Key: "jwt", Version: "1"}) require.NoError(t, err) tests := []struct { name string block *pem.Block }{ {name: "PKCS #8 PRIVATE KEY", block: &pem.Block{Type: "PRIVATE KEY", Bytes: pkcs8}}, {name: "PKCS #1 RSA PRIVATE KEY", block: &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { result, err := engine.Call(t.Context(), "jwt", map[string]any{"privateKey": string(pem.EncodeToMemory(test.block))}) require.NoError(t, err) token, ok := result.(string) require.True(t, ok) parsed, err := jwt.Parse(token, func(*jwt.Token) (any, error) { return &key.PublicKey, nil }, jwt.WithValidMethods([]string{"RS256"}), jwt.WithoutClaimsValidation()) require.NoError(t, err) assert.Equal(t, "JWT", parsed.Header["typ"]) assert.Equal(t, jwt.MapClaims{ "iss": "signer@example.iam.gserviceaccount.com", "scope": "https://www.googleapis.com/auth/cloud-platform", "aud": "https://oauth2.googleapis.com/token", "iat": float64(1762128000), "exp": float64(1762131600), }, parsed.Claims) }) } } func TestUtilsSigningEncodingsAndErrors(t *testing.T) { t.Parallel() abc := sha256.Sum256([]byte("abc")) _, edPrivate, err := ed25519.GenerateKey(rand.Reader) require.NoError(t, err) edPKCS8, err := x509.MarshalPKCS8PrivateKey(edPrivate) require.NoError(t, err) rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) require.NoError(t, err) rsaPublic, err := x509.MarshalPKIXPublicKey(&rsaKey.PublicKey) require.NoError(t, err) keys := map[string]any{ "ed25519": string(pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: edPKCS8})), "rsaPublic": string(pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: rsaPublic})), } tests := []struct { name string expression string want any }{ {name: "hex by default", expression: `utils.sha256("abc")`, want: hex.EncodeToString(abc[:])}, {name: "base64 padded", expression: `utils.sha256("abc", "base64")`, want: base64.StdEncoding.EncodeToString(abc[:])}, {name: "base64url unpadded", expression: `utils.sha256("abc", "base64url")`, want: base64.RawURLEncoding.EncodeToString(abc[:])}, {name: "bytes as ArrayBuffer", expression: `utils.sha256(new TextEncoder().encode("abc"), "bytes")`, want: abc[:]}, {name: "DataView reads only its view", expression: `utils.sha256(new DataView(new TextEncoder().encode("xabcx").buffer, 1, 3))`, want: hex.EncodeToString(abc[:])}, {name: "byte HMAC key equals its string", expression: `utils.hmacSHA256("hello", new TextEncoder().encode("secret"))`, want: "88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b"}, {name: "unknown encoding", expression: `utils.sha256("abc", "binary")`, want: "TypeError: utils.sha256 encoding must be hex, base64, base64url or bytes"}, {name: "input over 64 MiB", expression: `utils.sha256(new Uint8Array(64 * 1024 * 1024 + 1))`, want: "RangeError: utils.sha256 input exceeds 64 MiB"}, {name: "not PEM", expression: `utils.signRS256("data", "not a key")`, want: "TypeError: utils.signRS256 private key is not PEM text"}, {name: "public key PEM", expression: `utils.signRS256("data", ctx.rsaPublic)`, want: "TypeError: utils.signRS256 requires a PRIVATE KEY or RSA PRIVATE KEY PEM block"}, {name: "non-RSA PKCS #8 key", expression: `utils.signRS256("data", ctx.ed25519)`, want: "TypeError: utils.signRS256 private key is not a valid RSA private key"}, {name: "PEM over 16 KiB", expression: `utils.signRS256("data", "x".repeat(16 * 1024 + 1))`, want: "RangeError: utils.signRS256 private key PEM exceeds 16 KiB"}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { t.Parallel() engine, err := Compile(` export function run(ctx) { try { return `+test.expression+`; } catch (error) { return error.name + ": " + error.message; } }`, Options{Key: "encode", Version: "1"}) require.NoError(t, err) result, err := engine.Call(t.Context(), "run", keys) require.NoError(t, err) assert.Equal(t, test.want, result) }) } } func TestEngineJSONCloneProducesIndependentNativeContainers(t *testing.T) { engine, err := Compile(` export function clone(input) { const result = utils.json.clone(input); result.items.push({text:"appended"}); result.items[0].text = "changed"; return {result, array:Array.isArray(result.items), prototype:Object.getPrototypeOf(result) === Object.prototype, ownProto:Object.prototype.hasOwnProperty.call(result,"__proto__"), available:utils.hasCapability("json-clone@1"), unavailable:utils.hasCapability("json-clone@2")}; } export function invalid(kind) { let value; switch(kind) { case "cycle": value={}; value.self=value; break; case "function": value={run:function(){}}; break; case "undefined": value={missing:undefined}; break; case "nan": value=NaN; break; case "bigint": value=1n; break; case "date": value=new Date(); break; case "sparse": value=[,1]; break; case "depth": value = null; for (let i = 0; i < 33; i++) value = [value]; break; case "nodes": value=new Array(32768).fill(0); break; } return utils.json.clone(value); }`, Options{}) require.NoError(t, err) input := map[string]any{"items": []any{map[string]any{"text": "original"}}, "__proto__": map[string]any{"marker": "data"}, "zero": math.Copysign(0, -1)} value, err := engine.Call(t.Context(), "clone", input) require.NoError(t, err) result := value.(map[string]any) assert.Equal(t, true, result["array"]) assert.Equal(t, true, result["prototype"]) assert.Equal(t, true, result["ownProto"]) assert.Equal(t, true, result["available"]) assert.Equal(t, false, result["unavailable"]) cloned := result["result"].(map[string]any) assert.Equal(t, []any{map[string]any{"text": "changed"}, map[string]any{"text": "appended"}}, cloned["items"]) assert.Equal(t, map[string]any{"marker": "data"}, cloned["__proto__"]) assert.True(t, math.Signbit(cloned["zero"].(float64))) assert.Equal(t, []any{map[string]any{"text": "original"}}, input["items"]) for _, kind := range []string{"cycle", "function", "undefined", "nan", "bigint", "date", "sparse", "depth", "nodes"} { t.Run(kind, func(t *testing.T) { _, err := engine.Call(t.Context(), "invalid", kind) require.Error(t, err) }) } } func TestEngineRawJSONArgumentsMatchDecodedArguments(t *testing.T) { engine, err := Compile(` export function describe(...values) { const seen = []; const stack = values.reverse().map((v) => ({v})); while (stack.length > 0) { const {v, key, close} = stack.pop(); if (close) { seen.push(close); continue; } if (key !== undefined) seen.push(key); if (v === null || typeof v !== "object") { seen.push(typeof v + ":" + (Object.is(v, -0) ? "-0" : JSON.stringify(v))); continue; } const array = Array.isArray(v); seen.push(array ? "[" : Object.getPrototypeOf(v) === Object.prototype ? "{" : "{?"); stack.push({close: array ? "]" : "}"}); const keys = Object.keys(v); for (let i = keys.length - 1; i >= 0; i--) { stack.push({v: v[keys[i]], key: array ? undefined : JSON.stringify(keys[i])}); } } return seen.join(" "); }`, Options{}) require.NoError(t, err) // Hosts pass text written by the host JSON codec, where both paths agree on // key order, numbers and strings. values := map[string]any{ "object": map[string]any{ "__proto__": map[string]any{"polluted": true}, "10": "ten", "2": "two", "a": []any{}, "b": map[string]any{}, "numbers": []any{0, math.Copysign(0, -1), 1e21, int64(123456789012345678), 1.5e-7, -12, math.MaxInt64, math.SmallestNonzeroFloat64}, "strings": []any{"", "<>&

", "Γ©δΈ­πŸ˜€", "\u0000\t\"\\", "a\xffb"}, "nested": map[string]any{"z": nil, "y": []any{true, false, map[string]any{"x": []any{[]any{1}}}}}, }, "array": []any{map[string]any{"id": "x", "b": 1, "a": 2}, nil, "s"}, "string": "plain", "number": 42.5, "null": nil, "deep": json.RawMessage(strings.Repeat("[", 600) + strings.Repeat("]", 600)), } for name, value := range values { t.Run(name, func(t *testing.T) { text, err := marshalJSON(value) require.NoError(t, err) var decoded any require.NoError(t, unmarshalJSON(text, &decoded)) want, err := engine.Call(t.Context(), "describe", decoded) require.NoError(t, err) got, err := engine.Call(t.Context(), "describe", RawJSON(text)) require.NoError(t, err) assert.Equal(t, want, got) nested, err := engine.Call(t.Context(), "describe", map[string]any{"value": RawJSON(text)}) require.NoError(t, err) wantNested, err := engine.Call(t.Context(), "describe", map[string]any{"value": decoded}) require.NoError(t, err) assert.Equal(t, wantNested, nested) }) } _, err = engine.Call(t.Context(), "describe", "first", RawJSON(`{"a":`)) require.EqualError(t, err, "plugin @ hook describe argument 2: unexpected end of JSON input") } func TestJSONStateChangesAndEncodedLimits(t *testing.T) { const changes = `[ [{"op":"set","path":[],"value":{"text":"<","parts":[],"nested":{"value":null}}}], [{"op":"appendText","path":["text"],"value":"&\n\"ε›ΎεƒπŸ˜€\u2028"}, {"op":"append","path":["parts"],"value":{"text":"a","enabled":false}}, {"op":"set","path":["nested","value"],"value":[1,2]}], [{"op":"appendText","path":["parts",0,"text"],"value":"b"}, {"op":"set","path":["nested","value",0],"value":0}, {"op":"set","path":["nested","a>20*3/4) require.NoError(t, err) assert.Equal(t, want+1, value) } _, err = engine.Call(t.Context(), "run", -1) require.ErrorIs(t, err, moejs.ErrMemoryLimit, "the limit stops the hook, which cannot catch it, before its timeout") var hookErr *HookError assert.False(t, errors.As(err, &hookErr)) value, err := engine.Call(t.Context(), "run", 0) require.NoError(t, err) assert.Equal(t, int64(1), value, "a runtime stopped by the memory limit is discarded") } func TestEngineDoesNotReinitializeIdleRuntimeAfterGC(t *testing.T) { var initialized atomic.Int64 engine, err := Compile(`console.log("initialized"); export function run(){return 42;}`, Options{ Log: func(string) { initialized.Add(1) }, }) require.NoError(t, err) runtime.GC() runtime.GC() value, err := engine.Call(t.Context(), "run") require.NoError(t, err) assert.Equal(t, int64(42), value) assert.Equal(t, int64(1), initialized.Load()) } func TestEngineIdleRuntimeReleasesLastCallArguments(t *testing.T) { engine, err := Compile(`export function run(body){return body.image.length;}`, Options{Concurrency: 1}) require.NoError(t, err) image := strings.Repeat("A", 1<<20) released := weak.Make(unsafe.StringData(image)) value, err := engine.Call(t.Context(), "run", map[string]any{"image": image}) require.NoError(t, err) assert.Equal(t, int64(1<<20), value) runtime.GC() runtime.GC() assert.Nil(t, released.Value(), "an idle pooled runtime must not keep the last request body alive") value, err = engine.Call(t.Context(), "run", map[string]any{"image": "AB"}) require.NoError(t, err) assert.Equal(t, int64(2), value) } func TestPooledRuntimeKeepsNoCallData(t *testing.T) { plugin, err := CompilePlugin(transformMeta+` let calls = 0; export function ingress(ctx, req) { calls++; const fetched = utils.fetch({url: "https://api.example/data", responseType: "bytes"}); /secret-(\d+)/.test(req.body.text); return {action: "continue", findings: [{rule: "fetched", count: fetched.body.byteLength}]}; } export function egress(ctx, req) { calls++; const seen = {calls, regexp: RegExp.input + RegExp.lastMatch + RegExp.leftContext + RegExp.rightContext + RegExp.$1}; try { utils.fetch({url: "https://api.example/data"}); } catch (e) { seen.fetch = e.message; } try { utils.paramOverride({}, {}); } catch (e) { seen.paramOverride = e.message; } return {action: "continue", effects: [seen]}; }`, Options{Concurrency: 1}) require.NoError(t, err) // Each tracked value is large enough to have memory of its own, as in // TestEngineIdleRuntimeReleasesLastCallArguments; the context holds // marker for as long as it lives. padding := strings.Repeat("x", 64<<10) fetches := 0 var fetchedBody weak.Pointer[byte] marker := new([64 << 10]byte) contextReleased := weak.Make(marker) ctx := WithFetcher(context.WithValue(context.Background(), marker, "marker"), func(context.Context, FetchRequest) (FetchResponse, error) { fetches++ body := bytes.Repeat([]byte{'f'}, 64<<10) fetchedBody = weak.Make(&body[0]) return FetchResponse{Status: http.StatusOK, Body: body}, nil }) body := []byte(`{"text":"secret-1234","padding":"` + padding + `"}`) config := []byte(`{"limit":7,"padding":"` + padding + `"}`) bodyReleased, configReleased := weak.Make(&body[0]), weak.Make(&config[0]) result, err := plugin.CallTransform(ctx, TransformContext{Stage: "ingress", Config: config}, TransformRequest{Body: body}) require.NoError(t, err) assert.Equal(t, []TransformFinding{{Rule: "fetched", Count: 64 << 10}}, result.Findings) // The host keeps none of the finished call: its arguments, its context, // its transform context (whose config is also an argument) and what // utils.fetch returned to it. runtime.GC() runtime.GC() assert.Nil(t, bodyReleased.Value(), "request body") assert.Nil(t, configReleased.Value(), "instance config") assert.Nil(t, fetchedBody.Value(), "utils.fetch response") assert.Nil(t, contextReleased.Value(), "call context") // The next call on the same runtime sees module scope, which is shared by // design, and none of the last call's data through host utilities or the // RegExp statics. result, err = plugin.CallTransform(context.Background(), TransformContext{Stage: "egress"}, TransformRequest{Body: json.RawMessage(`{}`)}) require.NoError(t, err) assert.JSONEq(t, `[{"calls":2,"regexp":"", "fetch":"utils.fetch is not available in egress", "paramOverride":"utils.paramOverride is not available in egress"}]`, string(result.Effects)) assert.Equal(t, 1, fetches) } func TestEngineBoundsConcurrentCallsAndCancelsAnOccupiedRuntime(t *testing.T) { entered := make(chan struct{}, 2) release := make(chan struct{}) defer close(release) engine, err := Compile(`export function run(loop) { utils.unixNow(); if (loop) { while (true) {} } return 42; }`, Options{ Concurrency: 2, Now: func() time.Time { entered <- struct{}{} <-release return time.Unix(1, 0) }, }) require.NoError(t, err) ctx, cancel := context.WithCancel(t.Context()) defer cancel() canceled := make(chan error, 1) healthy := make(chan error, 1) go func() { _, err := engine.Call(ctx, "run", true); canceled <- err }() go func() { _, err := engine.Call(t.Context(), "run", false); healthy <- err }() <-entered <-entered _, err = engine.CallPathWithAdmissionTimeout(t.Context(), time.Nanosecond, "run", nil, false) require.ErrorIs(t, err, ErrCallAdmissionTimeout) cancel() release <- struct{}{} release <- struct{}{} require.ErrorIs(t, <-canceled, context.Canceled) require.NoError(t, <-healthy) } func TestEngineFetchContract(t *testing.T) { engine, err := Compile(` export function run(request) { return utils.fetch(request); } export function fetchBytes(url) { return Array.from(new Uint8Array(utils.fetch({url: url, responseType: "bytes"}).body)); } export function fetchSizes(paths) { return paths.map(path => { try { return utils.fetch({url: "https://api.example/" + path, responseType: "bytes"}).body.byteLength; } catch (e) { return e.message; } }); } export function fetchFiveTimes() { const failures = []; for (let i = 0; i < 5; i++) { try { utils.fetch({url: "https://api.example/json"}); } catch (e) { failures.push(e.message); } } return failures; }`, Options{Key: "fetch"}) require.NoError(t, err) var requests []FetchRequest const fullResponse = 4 << 20 payload := make([]byte, fullResponse+1) ctx := WithFetcher(t.Context(), func(_ context.Context, request FetchRequest) (FetchResponse, error) { requests = append(requests, request) switch request.URL { case "https://api.example/json": return FetchResponse{Status: http.StatusOK, Headers: map[string]string{"Content-Type": "application/json"}, Body: []byte(`{"file":{"id":7}}`)}, nil case "https://api.example/text": return FetchResponse{Status: http.StatusNotFound, Body: []byte("not found")}, nil case "https://api.example/bytes": return FetchResponse{Status: http.StatusPartialContent, Body: []byte{0, 0x80, 0xff, '{'}}, nil case "https://api.example/full": return FetchResponse{Status: http.StatusOK, Body: payload[:fullResponse]}, nil case "https://api.example/partial": return FetchResponse{Body: payload[:fullResponse-1]}, io.ErrUnexpectedEOF case "https://api.example/large": return FetchResponse{Status: http.StatusOK, Body: payload}, nil case "https://api.example/down?token=secret": return FetchResponse{}, &url.Error{Op: "Get", URL: request.URL, Err: errors.New("connection refused")} } return FetchResponse{Status: http.StatusOK, Body: []byte(`{"ignored":true}`)}, nil }) _, err = engine.Call(t.Context(), "run", map[string]any{"url": "https://api.example/json"}) assert.ErrorContains(t, err, "utils.fetch is not available in run") value, err := engine.Call(ctx, "run", map[string]any{"url": "https://api.example/json", "headers": map[string]any{"Authorization": "Bearer key"}}) require.NoError(t, err) assert.Equal(t, map[string]any{ "status": int64(http.StatusOK), "headers": map[string]any{"Content-Type": "application/json"}, "body": map[string]any{"file": map[string]any{"id": int64(7)}}, }, value) assert.Equal(t, []FetchRequest{{Hook: "run", URL: "https://api.example/json", Method: http.MethodGet, Headers: map[string]string{"Authorization": "Bearer key"}}}, requests) value, err = engine.Call(ctx, "run", map[string]any{"url": "https://api.example/text"}) require.NoError(t, err) assert.Equal(t, map[string]any{"status": int64(http.StatusNotFound), "headers": map[string]any{}, "body": "not found"}, value) value, err = engine.Call(ctx, "run", map[string]any{"url": "https://api.example/head", "method": "head"}) require.NoError(t, err) assert.Equal(t, "", value.(map[string]any)["body"]) assert.Equal(t, http.MethodHead, requests[len(requests)-1].Method) // Bytes that are neither JSON nor UTF-8 reach the plugin unchanged. value, err = engine.Call(ctx, "fetchBytes", "https://api.example/bytes") require.NoError(t, err) assert.Equal(t, []any{int64(0), int64(0x80), int64(0xff), int64('{')}, value) sent := len(requests) for name, request := range map[string]map[string]any{ "unsupported member \"body\"": {"url": "https://api.example/json", "body": "x"}, "only GET and HEAD": {"url": "https://api.example/json", "method": "POST"}, `responseType must be "json" or "bytes"`: {"url": "https://api.example/json", "responseType": "text"}, "request header \"Host\" is not": {"url": "https://api.example/json?token=secret", "headers": map[string]any{"Host": "other.example"}}, "absolute http(s) URL": {"url": "/v1/files/retrieve"}, "exceeds 65536 bytes": {"url": "https://api.example/" + strings.Repeat("a", 64<<10)}, } { _, err = engine.Call(ctx, "run", request) require.Error(t, err, name) assert.Contains(t, err.Error(), name) assert.NotContains(t, err.Error(), "secret") } assert.Len(t, requests, sent, "rejected requests must not reach the fetcher") _, err = engine.Call(ctx, "run", map[string]any{"url": "https://api.example/large"}) assert.ErrorContains(t, err, "utils.fetch response from api.example exceeds 4194304 bytes") const budgetError = "utils.fetch allows at most 8388608 response bytes per hook call" sent = len(requests) value, err = engine.Call(ctx, "fetchSizes", []string{"full", "full", "bytes"}) require.NoError(t, err) assert.Equal(t, []any{int64(fullResponse), int64(fullResponse), budgetError}, value) assert.Len(t, requests, sent+2, "exhausting the byte budget stops further network calls") sent = len(requests) value, err = engine.Call(ctx, "fetchSizes", []string{"partial", "full", "bytes", "json"}) require.NoError(t, err) assert.Equal(t, []any{"utils.fetch to api.example failed: unexpected EOF", int64(fullResponse), budgetError, budgetError}, value) require.Len(t, requests, sent+3) assert.Equal(t, int64(1), requests[sent+2].ResponseBodyLimit(), "the transport can read one more response byte") _, err = engine.Call(ctx, "run", map[string]any{"url": "https://api.example/down?token=secret"}) var hookErr *HookError require.ErrorAs(t, err, &hookErr) assert.Equal(t, "utils.fetch to api.example failed: connection refused", hookErr.Message) value, err = engine.Call(ctx, "fetchFiveTimes") require.NoError(t, err) assert.Equal(t, []any{"utils.fetch allows at most 4 requests per hook call"}, value) } func TestEngineFetchLendsItsExecutionSlot(t *testing.T) { engine, err := Compile(` export function fetchOnce() { return utils.fetch({url: "https://api.example/slow"}).body; } export function plain() { return 42; }`, Options{Key: "fetch-slot", Concurrency: 1}) require.NoError(t, err) entered := make(chan struct{}) release := make(chan struct{}) ctx := WithFetcher(t.Context(), func(context.Context, FetchRequest) (FetchResponse, error) { close(entered) <-release return FetchResponse{Status: http.StatusOK, Body: []byte(`"done"`)}, nil }) fetched := make(chan any, 1) go func() { value, err := engine.Call(ctx, "fetchOnce") assert.NoError(t, err) fetched <- value }() <-entered // The only slot is lent while the fetch waits; without that this call // would wait for its deadline. waitContext, cancel := context.WithTimeout(t.Context(), 5*time.Second) defer cancel() value, err := engine.Call(waitContext, "plain") require.NoError(t, err) assert.Equal(t, int64(42), value) close(release) assert.Equal(t, "done", <-fetched) } func TestEngineFetchWaitDoesNotCountTowardCallTimeout(t *testing.T) { engine, err := Compile(`export function run() { return utils.fetch({url: "https://api.example/slow"}).status; }`, Options{ Key: "fetch-clock", Timeout: 20 * time.Millisecond, }) require.NoError(t, err) entered := make(chan struct{}) release := make(chan struct{}) ctx := WithFetcher(t.Context(), func(context.Context, FetchRequest) (FetchResponse, error) { close(entered) <-release return FetchResponse{Status: http.StatusNoContent}, nil }) result := make(chan error, 1) var value any go func() { var err error value, err = engine.Call(ctx, "run") result <- err }() <-entered // Five times the JavaScript timeout: the clock is stopped while waiting. time.Sleep(100 * time.Millisecond) close(release) require.NoError(t, <-result) assert.Equal(t, int64(http.StatusNoContent), value) } func TestEngineInterruptsAFetchThatCannotResume(t *testing.T) { engine, err := Compile(` let calls = 0; export function run(fetch) { calls++; if (!fetch) return calls; try { utils.fetch({url: "https://api.example/slow"}); } catch (e) { return "caught: " + e.message; } return "resumed"; }`, Options{Key: "fetch-resume", Concurrency: 1}) require.NoError(t, err) entered := make(chan struct{}) release := make(chan struct{}) ctx, cancel := context.WithCancel(t.Context()) defer cancel() ctx = WithFetcher(ctx, func(context.Context, FetchRequest) (FetchResponse, error) { close(entered) <-release return FetchResponse{Status: http.StatusOK}, nil }) result := make(chan error, 1) go func() { _, err := engine.Call(ctx, "run", true) result <- err }() <-entered // Another call takes the lent slot and the caller leaves before the fetch // returns, so the slot cannot be taken back. engine.semaphore <- struct{}{} cancel() close(release) err = <-result <-engine.semaphore require.ErrorIs(t, err, context.Canceled) var hookErr *HookError assert.False(t, errors.As(err, &hookErr), "JavaScript must not catch a failed resume") value, err := engine.Call(t.Context(), "run", false) require.NoError(t, err) assert.Equal(t, int64(1), value, "an interrupted runtime must be discarded") assert.Empty(t, engine.semaphore) assert.Empty(t, engine.parked) } func TestEnginePreservesMutableExportAndMemberBindings(t *testing.T) { engine, err := Compile(` export let run = function(){return 1;}; export const native = {render:function(){return 1;}}; export function replace(){run=function(){return 2;};native.render=function(){return 3;};} `, Options{Concurrency: 1}) require.NoError(t, err) value, err := engine.Call(t.Context(), "run") require.NoError(t, err) assert.Equal(t, int64(1), value) _, err = engine.Call(t.Context(), "replace") require.NoError(t, err) value, err = engine.Call(t.Context(), "run") require.NoError(t, err) assert.Equal(t, int64(2), value) value, err = engine.CallMember(t.Context(), "native", "render") require.NoError(t, err) assert.Equal(t, int64(3), value) } func TestEngineHasExportUsesBindingsAsLoaded(t *testing.T) { engine, err := Compile(` export let later; export const unset = undefined; export function run(){return 1;} export function assign(){later=function(){return 2;};} `, Options{Concurrency: 1}) require.NoError(t, err) engine.semaphore <- struct{}{} assert.True(t, engine.HasExport("run")) assert.False(t, engine.HasExport("later")) assert.False(t, engine.HasExport("unset")) assert.False(t, engine.HasExport("missing")) <-engine.semaphore _, err = engine.Call(t.Context(), "assign") require.NoError(t, err) assert.False(t, engine.HasExport("later")) value, err := engine.Call(t.Context(), "later") require.NoError(t, err) assert.Equal(t, int64(2), value) } func TestEngineHookErrorExtractsSanitizedJSMessage(t *testing.T) { t.Parallel() tests := []struct { name string source string wantMessage string wantLen int }{ { name: "Error object", source: `export function run() { throw new Error("model is required"); }`, wantMessage: "model is required", }, { name: "raw string throw", source: `export function run() { throw "raw string"; }`, wantMessage: "raw string", }, { name: "truncates to 512 runes", source: `export function run() { throw new Error("x".repeat(2000)); }`, wantLen: 512, wantMessage: strings.Repeat("x", 512), }, { name: "scrubs control characters", source: "export function run() { throw new Error(\"line1\\nline2\\x1b[31mred\"); }", wantMessage: "line1 line2 [31mred", }, { name: "throwing message getter falls back without crashing", source: `export function run() { throw {get message() { throw {get message() { return "deep"; }}; }}; }`, wantMessage: "plugin hook failed", }, } for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) { t.Parallel() engine, err := Compile(testCase.source, Options{Key: "diag", Version: "1.0.0"}) require.NoError(t, err) _, err = engine.Call(context.Background(), "run") require.Error(t, err) var hookErr *HookError require.True(t, errors.As(err, &hookErr)) assert.Equal(t, "run", hookErr.Hook) assert.Equal(t, testCase.wantMessage, hookErr.Message) if testCase.wantLen > 0 { assert.Equal(t, testCase.wantLen, utf8.RuneCountInString(hookErr.Message)) } assert.Contains(t, hookErr.Error(), "plugin diag@1.0.0") assert.NotContains(t, hookErr.Message, "Error:") assert.NotContains(t, hookErr.Message, "plugin diag@") }) } } func TestEngineReportsProtocolAdmissionTimeoutSeparately(t *testing.T) { engine, err := Compile(` export const protocols = { responses: {renderEvents: function() { return {events: [], done: false}; }}, }; `, Options{Key: "admission", Version: "1", Concurrency: 1}) require.NoError(t, err) engine.semaphore <- struct{}{} _, err = engine.CallPathWithAdmissionTimeout( context.Background(), time.Nanosecond, "protocols", []string{"responses", "renderEvents"}, ) <-engine.semaphore require.ErrorIs(t, err, ErrCallAdmissionTimeout) result, err := engine.CallPathWithAdmissionTimeout( context.Background(), time.Second, "protocols", []string{"responses", "renderEvents"}, ) require.NoError(t, err) assert.Equal(t, map[string]any{"events": []any{}, "done": false}, result) } func TestEngineExportInterruptsLongRunningGetter(t *testing.T) { t.Parallel() engine, err := Compile(` export const meta = { apiVersion: 1, get name() { while (true) {} }, }; `, Options{Key: "meta-loop", Version: "1.0.0", Timeout: 20 * time.Millisecond}) require.NoError(t, err) _, err = engine.Export(context.Background(), "meta") require.ErrorContains(t, err, "export meta interrupted") } func TestEngineExportReturnsThrownGetterError(t *testing.T) { t.Parallel() engine, err := Compile(` export const meta = { apiVersion: 1, get name() { throw new Error("getter failed"); }, }; `, Options{Key: "meta-throw", Version: "1.0.0"}) require.NoError(t, err) _, err = engine.Export(context.Background(), "meta") require.ErrorContains(t, err, "export meta failed") assert.Contains(t, err.Error(), "getter failed") } func TestEngineNestedHooksRequireOwnProperties(t *testing.T) { t.Parallel() engine, err := Compile(` const inheritedRenderers = { inherited: function(value) { return value; }, constructor: function(value) { return value; }, toString: function(value) { return value; }, ["__proto__"]: function(value) { return value; }, }; export const renderers = Object.create(inheritedRenderers); renderers.own = function(value) { return {id: value.id}; }; const inheritedProtocol = { renderFinal: function(value) { return value; }, }; export const protocols = { responses: Object.create(inheritedProtocol), }; `, Options{Key: "own-hooks", Version: "1.0.0"}) require.NoError(t, err) found, err := engine.HasCallablePath(context.Background(), "renderers", "own") require.NoError(t, err) assert.True(t, found) result, err := engine.CallMember(context.Background(), "renderers", "own", map[string]any{"id": "task-1"}) require.NoError(t, err) assert.Equal(t, map[string]any{"id": "task-1"}, result) for _, member := range []string{"inherited", "constructor", "toString", "__proto__"} { t.Run(member, func(t *testing.T) { found, err := engine.HasCallablePath(context.Background(), "renderers", member) require.NoError(t, err) assert.False(t, found) _, err = engine.CallMember(context.Background(), "renderers", member) require.ErrorContains(t, err, "not found") }) } found, err = engine.HasCallablePath(context.Background(), "protocols", "responses", "renderFinal") require.NoError(t, err) assert.False(t, found) _, err = engine.CallPath(context.Background(), "protocols", []string{"responses", "renderFinal"}) require.ErrorContains(t, err, "not found") } func TestCompileInterruptsLongRunningInitialization(t *testing.T) { t.Parallel() _, err := Compile(`while (true) {}; export function run() {}`, Options{ Key: "loop", Version: "1", Timeout: 20 * time.Millisecond, }) require.Error(t, err) assert.Contains(t, err.Error(), "initialization timed out") } func TestValidateRequestURL(t *testing.T) { t.Parallel() tests := []struct { name string requestURL string baseURL string allowedHosts []string wantError string }{ {name: "same host", requestURL: "https://api.example.com/v1/task", baseURL: "https://api.example.com/v1"}, {name: "default port", requestURL: "https://api.example.com:443/v1/task", baseURL: "https://api.example.com"}, {name: "approved host", requestURL: "https://upload.example.com/task", baseURL: "https://api.example.com", allowedHosts: []string{"upload.example.com"}}, {name: "approved host with port", requestURL: "http://192.168.1.10:8080/task", baseURL: "http://192.168.1.10:8000", allowedHosts: []string{"192.168.1.10:8080"}}, {name: "approved host with explicit default port", requestURL: "https://upload.example.com/task", baseURL: "https://api.example.com", allowedHosts: []string{"upload.example.com:443"}}, {name: "approved host port mismatch", requestURL: "http://192.168.1.10:9000/task", baseURL: "http://192.168.1.10:8000", allowedHosts: []string{"192.168.1.10:8080"}, wantError: "not allowed"}, {name: "subdomain is not implicit", requestURL: "https://evil.api.example.com/task", baseURL: "https://api.example.com", wantError: "not allowed"}, {name: "userinfo trick", requestURL: "https://api.example.com@evil.example/task", baseURL: "https://api.example.com", wantError: "not allowed"}, {name: "relative URL", requestURL: "/v1/task", baseURL: "https://api.example.com", wantError: "absolute"}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { err := ValidateRequestURL(test.requestURL, test.baseURL, test.allowedHosts) if test.wantError == "" { require.NoError(t, err) return } require.Error(t, err) assert.True(t, strings.Contains(err.Error(), test.wantError), err.Error()) }) } } func TestEngineCallPathJSONIntoKeepsMemberOrder(t *testing.T) { engine, err := Compile(`export const native = {render(view) { return {zeta: view.id, alpha: undefined, mid: {b: 2, a: 1}}; }};`, Options{Key: "order", Version: "1"}) require.NoError(t, err) var text json.RawMessage require.NoError(t, engine.CallPathJSONInto(t.Context(), &text, "native", []string{"render"}, map[string]any{"id": "x"})) assert.Equal(t, `{"zeta":"x","mid":{"b":2,"a":1}}`, string(text)) }