From 1e0ae0e54cc5ef1bdec793f2ba025585ebcff7d2 Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 15:50:24 -0700 Subject: [PATCH 1/8] Validate expression syntax and indexed access Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- README.md | 6 +- package.json | 1 + spec/src/expression.ts | 149 +++++++++++++++++++++++++++++++++- spec/test/expression.test.mjs | 113 ++++++++++++++++++++++++++ 4 files changed, 266 insertions(+), 3 deletions(-) create mode 100644 spec/test/expression.test.mjs diff --git a/README.md b/README.md index 058beb4..462acef 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ The JSON specification can define data sources and transformations, scales, axes The [Try Online](https://microsoft.github.io/morphcharts/client.html) page also supports using **local files**. +Expressions in specifications support named built-in functions, dataset fields, and numeric array/string indexing. General JavaScript property access and calls on expression results are not supported; use `datum['field name']` for dataset columns and helpers such as `length(value)` for string or array lengths. + ### Gallery Check out the [Gallery](https://microsoft.github.io/morphcharts/gallery.html) to see more examples. @@ -65,4 +67,6 @@ The project can be installed and run locally using the following steps: 1. Clone the repo 2. `npm install` to install any required dependencies 3. Build the project using `npm run dev-client` -4. Start the web site using `npm run start-client`, which also opens the `client.html` page in the default web browser \ No newline at end of file +4. Start the web site using `npm run start-client`, which also opens the `client.html` page in the default web browser + +Run the expression parser regression tests with `npm run test-expression`. \ No newline at end of file diff --git a/package.json b/package.json index 22b5195..8a235f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "author": "David Brown", "scripts": { + "test-expression": "npm run dev-spec && node --test spec/test/expression.test.mjs", "dev-core": "tsc -p ./core/tsconfig.json", "dev-spec": "tsc -p ./core/tsconfig.json && tsc -p ./spec/tsconfig.json", "dev-webgpuraytrace": "tsc -p ./core/tsconfig.json && tsc -p ./renderers/webgpuraytrace/tsconfig.json", diff --git a/spec/src/expression.ts b/spec/src/expression.ts index a3b5c18..002014a 100644 --- a/spec/src/expression.ts +++ b/spec/src/expression.ts @@ -11,7 +11,127 @@ export class Expression { return this._buildExpression(this._parseExpression(expr, group, dataset), group, dataset); } + private _validateExpression(expr: string): Set { + const indices = new Set(); + let position = 0; + let token = ""; + let kind: "identifier" | "literal" | "operator" | "end"; + const next = (): void => { + while (expr[position] == " ") { position++; } + if (position == expr.length) { + token = ""; + kind = "end"; + return; + } + const rest = expr.slice(position); + const identifier = /^[a-zA-Z_][a-zA-Z0-9_]*/.exec(rest); + const number = /^(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)/.exec(rest); + if (identifier) { + token = identifier[0]; + kind = "identifier"; + } + else if (number) { + token = number[0]; + kind = "literal"; + } + else if (rest[0] == "'") { + const end = expr.indexOf("'", position + 1); + if (end == -1) { throw new Error("expression string not closed"); } + token = expr.slice(position, end + 1); + kind = "literal"; + } + else { + token = /^(?:>=|<=|==|!=|&&|\|\|)/.exec(rest)?.[0] ?? rest[0]; + kind = "operator"; + } + position += token.length; + }; + const consume = (expected: string): void => { + if (token != expected) { throw new Error(`expression expected ${expected}`); } + next(); + }; + const argumentsOrArray = (closing: string): void => { + next(); + if (token != closing) { + conditional(); + while (token == ",") { + next(); + if (token == closing) { break; } + conditional(); + } + } + consume(closing); + }; + const primary = (): void => { + if (token == "+" || token == "-" || token == "!") { + next(); + primary(); + return; + } + if (kind == "literal") { + next(); + } + else if (kind == "identifier") { + const name = token; + next(); + if (name == "datum" && token == ".") { + next(); + if (kind != "identifier") { throw new Error("expression invalid field name"); } + next(); + } + else if (token == "(") { + argumentsOrArray(")"); + } + } + else if (token == "(") { + next(); + sequence(); + consume(")"); + } + else if (token == "[") { + argumentsOrArray("]"); + } + else { + throw new Error("expression expected value"); + } + while (token == "[") { + indices.add(position - 1); + next(); + sequence(); + consume("]"); + } + }; + const binary = (): void => { + primary(); + while (["+", "-", "*", "/", "%", "^", ">", "<", ">=", "<=", "==", "!=", "&&", "||"].includes(token)) { + next(); + primary(); + } + }; + const conditional = (): void => { + binary(); + if (token == "?") { + next(); + conditional(); + consume(":"); + conditional(); + } + }; + const sequence = (): void => { + conditional(); + while (token == ",") { + next(); + conditional(); + } + }; + next(); + sequence(); + if (kind != "end") { throw new Error(`expression unexpected token ${token}`); } + return indices; + } + private _parseExpression(expr: string, group: Group, dataset: Dataset): string { + const indices = this._validateExpression(expr); let expression = ""; let i = 0; while (i < expr.length) { @@ -146,7 +266,27 @@ export class Expression { continue; } - // Brackets + // Computed access is limited to numeric indices, never prototype properties. + if (indices.has(i)) { + const start = ++i; + let brackets = 1; + while (i < expr.length && brackets > 0) { + if (expr[i] == "'") { + i = expr.indexOf("'", i + 1); + if (i == -1) { throw new Error("expression string not closed"); } + } + else if (expr[i] == "[") { brackets++; } + else if (expr[i] == "]") { brackets--; } + if (brackets > 0) { i++; } + } + if (brackets != 0) { throw new Error("expression index not closed"); } + const index = this._parseExpression(expr.slice(start, i), group, dataset); + expression += `[index((${index}))]`; + i++; + continue; + } + + // Grouping and array literals if (expr[i] == "(" || expr[i] == ")" || expr[i] == "[" || expr[i] == "]") { expression += expr[i]; i++; @@ -511,7 +651,12 @@ export class Expression { private _buildExpression(expr: string, group: Group, dataset: Dataset): any { // Return evaluatable function - return new Function("group", "dataset", "i", `return ${expr}`) as (group: Group, dataset: Dataset, i: number) => any; + return new Function("index", "group", "dataset", "i", `return ${expr}`).bind(undefined, (index: unknown): number => { + if (typeof index != "number" || !Number.isInteger(index) || index < 0) { + throw new Error("expression index must be a non-negative integer"); + } + return index; + }) as (group: Group, dataset: Dataset, i: number) => any; } private _finalClosingBracketIndex(expr: string, startIndex: number, openBrackets: number = 0): number { diff --git a/spec/test/expression.test.mjs b/spec/test/expression.test.mjs new file mode 100644 index 0000000..5e40c6f --- /dev/null +++ b/spec/test/expression.test.mjs @@ -0,0 +1,113 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { Data } from "@microsoft/morphcharts-core"; +import { Dataset } from "../dist/dataset.js"; +import { Expression } from "../dist/expression.js"; +import { Signal } from "../dist/signal.js"; + +test("signal loading rejects constructor escapes without executing their bodies", () => { + const previousName = Object.getOwnPropertyDescriptor(globalThis, "name"); + let executed = false; + globalThis.__morphExpressionInjected = () => { executed = true; }; + Object.defineProperty(globalThis, "name", { + configurable: true, + value: "__morphExpressionInjected()", + }); + try { + for (const update of [ + "(1)['constructor']['constructor']('eval(name)')()", + "(1)['constructor']['constructor']('__morphExpressionInjected()')()", + "(1)['con'+'structor']['con'+'structor']('__morphExpressionInjected()')()", + "abs['constructor']('__morphExpressionInjected()')()", + "(abs)('__morphExpressionInjected()')", + "split('constructor',',')[0]('__morphExpressionInjected()')", + ]) { + assert.throws(() => Signal.fromJSON(undefined, { name: "unsafe", update }), /expression/); + assert.equal(executed, false, update); + } + } + finally { + delete globalThis.__morphExpressionInjected; + if (previousName) { Object.defineProperty(globalThis, "name", previousName); } + else { delete globalThis.name; } + } +}); + +test("computed access cannot retrieve prototype properties, including dynamic keys", () => { + const group = { signals: { key: { update: () => "constructor" } } }; + for (const expr of [ + "(1)['constructor']", + "(1)['con'+'structor']", + "(1)[key]", + "(1)[split('constructor',',')[0]]", + "abs['constructor']", + "['a']['length']", + "['a'][[0]]", + "['a'][0.5]", + "['a'][-1]", + ]) { + const evaluate = new Expression().parseExpression(expr, group); + assert.throws(() => evaluate(group), /expression index must be a non-negative integer/, expr); + } +}); + +test("invalid syntax cannot escape index guards or introduce arbitrary calls", () => { + for (const expr of [ + "[1][0)]", + "[1][(0]", + "/abs/['constructor']", + "min(1,2)(3)", + "[abs][0](1)", + "1;random()", + ]) { + assert.throws(() => new Expression().parseExpression(expr), /expression/, expr); + } +}); + +test("supported operators, helpers, arrays, and numeric indexing retain their behavior", () => { + const group = { + signals: { + stacky: { update: () => [2, 5] }, + offset: { update: () => 1 }, + }, + getScale: () => ({ type: "band", bandwidth: () => 8, map: value => value * 2 }), + }; + for (const [expr, expected] of [ + ["1+2*3", 7], + ["!(2<1)&&3>=2?max(4,abs(-5)):0", 5], + ["2^3", 1], + ["stacky[offset]*2", 10], + ["stacky[(0,1)]", 5], + ["[[1,2],[3,4]][1][0]", 3], + ["split('a,b',',')[1]", "b"], + ["'abc'[1]", "b"], + ["length(split('a,b',','))", 2], + ["upper(trim(' hello '))", "HELLO"], + ["replace('constructor','con','de')", "destructor"], + ["year(datetime(1679529600000))", 2023], + ["bandwidth('xscale')+scale('xscale',3)", 14], + ]) { + const evaluate = new Expression().parseExpression(expr, group); + assert.deepEqual(evaluate(group), expected, expr); + } +}); + +test("dataset fields remain data, including fields named constructor", () => { + const dataset = new Dataset( + ["amount", "field name", "constructor"], + [["12", "first", "harmless"], ["25", "second", "also harmless"]], + [Data.ColumnType.integer, Data.ColumnType.string, Data.ColumnType.string], + ); + for (const [expr, expected] of [ + ["datum.amount*2", 50], + ["datum['field name']", "second"], + ["datum.constructor", "also harmless"], + ["datum['constructor']", "also harmless"], + ]) { + const evaluate = new Expression().parseExpression(expr, undefined, dataset); + assert.equal(evaluate(undefined, dataset, 1), expected, expr); + } +}); From 2227854b147ac233f091564d774b74da6c97b1f2 Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 16:12:06 -0700 Subject: [PATCH 2/8] Validate initial sample selection Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- README.md | 5 +- client/src/client.ts | 3 +- client/src/common.ts | 7 +++ client/test/sample-url.test.mjs | 93 +++++++++++++++++++++++++++++++++ package.json | 1 + 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 client/test/sample-url.test.mjs diff --git a/README.md b/README.md index 462acef..b9bb288 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ The JSON specification can define data sources and transformations, scales, axes The [Try Online](https://microsoft.github.io/morphcharts/client.html) page also supports using **local files**. +The client's `spec` and `plot` query parameters select bundled examples only: use a bare sample name or a name prefixed with `samples/`, `gallery/`, or `docs/`, optionally ending in `.json`. External URLs and other paths are not accepted. + Expressions in specifications support named built-in functions, dataset fields, and numeric array/string indexing. General JavaScript property access and calls on expression results are not supported; use `datum['field name']` for dataset columns and helpers such as `length(value)` for string or array lengths. ### Gallery @@ -69,4 +71,5 @@ The project can be installed and run locally using the following steps: 3. Build the project using `npm run dev-client` 4. Start the web site using `npm run start-client`, which also opens the `client.html` page in the default web browser -Run the expression parser regression tests with `npm run test-expression`. \ No newline at end of file +Run the expression parser regression tests with `npm run test-expression`. +Run the initial sample URL regression tests with `npm run test-sample-url`. \ No newline at end of file diff --git a/client/src/client.ts b/client/src/client.ts index a1235f0..ee824db 100644 --- a/client/src/client.ts +++ b/client/src/client.ts @@ -435,9 +435,8 @@ export class Main { private async _loadInitialSampleAsync(sample: string | null): Promise { const emptySpec = "{}"; if (sample) { - const value = sample.toLowerCase().endsWith(".json") ? sample : `${sample}.json`; - const path = value.includes("/") ? `${value.substring(0, value.lastIndexOf("/"))}/specs/${value.substring(value.lastIndexOf("/") + 1)}` : `samples/specs/${value}`; try { + const path = Common.initialSamplePath(sample); const text = await fetch(path).then(response => response.text()); this._sampleLoaded(text); } diff --git a/client/src/common.ts b/client/src/common.ts index 1e0c206..4684337 100644 --- a/client/src/common.ts +++ b/client/src/common.ts @@ -17,6 +17,13 @@ export interface ISampleCategory { } export class Common { + static initialSamplePath(sample: string): string { + const name = sample.toLowerCase().endsWith(".json") ? sample.slice(0, -5) : sample; + const match = /^(?:(samples|gallery|docs)\/)?([a-zA-Z0-9_-]+)$/.exec(name); + if (!match || match[0] != name) { throw new Error("invalid sample identifier"); } + return `${match[1] ?? "samples"}/specs/${match[2]}.json`; + } + static async loadSampleIndex(path: string): Promise { try { const response = await fetch(path); diff --git a/client/test/sample-url.test.mjs b/client/test/sample-url.test.mjs new file mode 100644 index 0000000..6fdb14e --- /dev/null +++ b/client/test/sample-url.test.mjs @@ -0,0 +1,93 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { Common } from "../wwwroot/js/common.js"; + +const invalidSamples = [ + "https://attacker.example/payload", + "http://attacker.example/payload", + "//attacker.example/payload", + "javascript:alert(1)", + "data:application/json,{}", + "../payload", + "/payload", + "samples/../payload", + "gallery/../../payload", + "other/payload", + "samples/specs/payload", + "samples\\payload", + "\\\\attacker.example\\payload", + "%2e%2e%2fpayload", + "%252e%252e%252fpayload", + "samples%2fpayload", + "samples/payload?redirect=https://attacker.example", + "samples/payload#fragment", + "samples/", + ".json", + " payload", + "payload ", + "payload\n", + "payload.json\n", + "payload\r\n", + "payload\u0000", +]; + +test("sample identifiers resolve only to bundled spec directories", () => { + for (const [sample, expected] of [ + ["bar1", "samples/specs/bar1.json"], + ["bar1.json", "samples/specs/bar1.json"], + ["bar1.JSON", "samples/specs/bar1.json"], + ["samples/bar1", "samples/specs/bar1.json"], + ["samples/bar1.json", "samples/specs/bar1.json"], + ["gallery/earthquakes1", "gallery/specs/earthquakes1.json"], + ["gallery/earthquakes1.json", "gallery/specs/earthquakes1.json"], + ["docs/primitives1", "docs/specs/primitives1.json"], + ["docs/primitives1.json", "docs/specs/primitives1.json"], + ["Example_1-2", "samples/specs/Example_1-2.json"], + ]) { + assert.equal(Common.initialSamplePath(sample), expected); + } + for (const sample of invalidSamples) { + assert.throws(() => Common.initialSamplePath(sample), /invalid sample identifier/, sample); + } +}); + +test("initial loading rejects invalid selections before fetch and keeps the empty spec fallback", async t => { + globalThis.window = {}; + const { Main } = await import("../wwwroot/js/client.js"); + delete globalThis.window; + + const fetchMock = t.mock.method(globalThis, "fetch", async () => { + assert.fail("invalid selections must not be fetched"); + }); + const logMock = t.mock.method(console, "log", () => {}); + const loaded = []; + const client = { _sampleLoaded: text => loaded.push(text) }; + for (const sample of invalidSamples) { + await Main.prototype._loadInitialSampleAsync.call(client, sample); + } + assert.equal(fetchMock.mock.callCount(), 0); + assert.deepEqual(loaded, invalidSamples.map(() => "{}")); + assert.equal(logMock.mock.callCount(), invalidSamples.length); + + loaded.length = 0; + for (const sample of [null, ""]) { + await Main.prototype._loadInitialSampleAsync.call(client, sample); + } + assert.equal(fetchMock.mock.callCount(), 0); + assert.deepEqual(loaded, ["{}", "{}"]); +}); + +test("initial loading fetches a valid bundled spec and installs its text", async t => { + const { Main } = await import("../wwwroot/js/client.js"); + const spec = '{"signals":[]}'; + const fetchMock = t.mock.method(globalThis, "fetch", async () => ({ text: async () => spec })); + let loaded; + const client = { _sampleLoaded: text => { loaded = text; } }; + await Main.prototype._loadInitialSampleAsync.call(client, "gallery/earthquakes1"); + assert.equal(fetchMock.mock.calls[0].arguments[0], "gallery/specs/earthquakes1.json"); + assert.equal(fetchMock.mock.callCount(), 1); + assert.equal(loaded, spec); +}); diff --git a/package.json b/package.json index 8a235f5..c95bef3 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "author": "David Brown", "scripts": { "test-expression": "npm run dev-spec && node --test spec/test/expression.test.mjs", + "test-sample-url": "npm run dev-client && node --test client/test/sample-url.test.mjs", "dev-core": "tsc -p ./core/tsconfig.json", "dev-spec": "tsc -p ./core/tsconfig.json && tsc -p ./spec/tsconfig.json", "dev-webgpuraytrace": "tsc -p ./core/tsconfig.json && tsc -p ./renderers/webgpuraytrace/tsconfig.json", From aedeb32c3900d9cb74554b5c31d229a042349e52 Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 16:43:57 -0700 Subject: [PATCH 3/8] Preserve external sample URL support Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- README.md | 2 +- client/src/common.ts | 10 +++++++++ client/test/sample-url.test.mjs | 38 +++++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b9bb288..59c7f60 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ The JSON specification can define data sources and transformations, scales, axes The [Try Online](https://microsoft.github.io/morphcharts/client.html) page also supports using **local files**. -The client's `spec` and `plot` query parameters select bundled examples only: use a bare sample name or a name prefixed with `samples/`, `gallery/`, or `docs/`, optionally ending in `.json`. External URLs and other paths are not accepted. +The client's `spec` and `plot` query parameters accept a bare sample name or a name prefixed with `samples/`, `gallery/`, or `docs/`, optionally ending in `.json`. External HTTP(S) and protocol-relative URLs are also supported: for example, `https://example.com/charts/demo` loads `https://example.com/charts/specs/demo.json`. External servers must permit cross-origin requests. Other protocols and local traversal paths are not accepted. Expressions in specifications support named built-in functions, dataset fields, and numeric array/string indexing. General JavaScript property access and calls on expression results are not supported; use `datum['field name']` for dataset columns and helpers such as `length(value)` for string or array lengths. diff --git a/client/src/common.ts b/client/src/common.ts index 4684337..e38868d 100644 --- a/client/src/common.ts +++ b/client/src/common.ts @@ -18,6 +18,16 @@ export interface ISampleCategory { export class Common { static initialSamplePath(sample: string): string { + if (/^(?:https?:)?\/\//i.test(sample)) { + const protocolRelative = sample.startsWith("//"); + const url = new URL(protocolRelative ? `https:${sample}` : sample); + const slash = url.pathname.lastIndexOf("/"); + let filename = url.pathname.slice(slash + 1); + if (!filename) { throw new Error("invalid sample URL"); } + if (!filename.toLowerCase().endsWith(".json")) { filename += ".json"; } + url.pathname = `${url.pathname.slice(0, slash)}/specs/${filename}`; + return protocolRelative ? url.href.slice(url.protocol.length) : url.href; + } const name = sample.toLowerCase().endsWith(".json") ? sample.slice(0, -5) : sample; const match = /^(?:(samples|gallery|docs)\/)?([a-zA-Z0-9_-]+)$/.exec(name); if (!match || match[0] != name) { throw new Error("invalid sample identifier"); } diff --git a/client/test/sample-url.test.mjs b/client/test/sample-url.test.mjs index 6fdb14e..2d359ea 100644 --- a/client/test/sample-url.test.mjs +++ b/client/test/sample-url.test.mjs @@ -6,11 +6,10 @@ import { test } from "node:test"; import { Common } from "../wwwroot/js/common.js"; const invalidSamples = [ - "https://attacker.example/payload", - "http://attacker.example/payload", - "//attacker.example/payload", "javascript:alert(1)", "data:application/json,{}", + "file:///payload", + "ftp://example.com/payload", "../payload", "/payload", "samples/../payload", @@ -34,7 +33,7 @@ const invalidSamples = [ "payload\u0000", ]; -test("sample identifiers resolve only to bundled spec directories", () => { +test("sample identifiers resolve to bundled spec directories", () => { for (const [sample, expected] of [ ["bar1", "samples/specs/bar1.json"], ["bar1.json", "samples/specs/bar1.json"], @@ -54,6 +53,19 @@ test("sample identifiers resolve only to bundled spec directories", () => { } }); +test("external HTTP(S) URLs retain the existing specs directory mapping", () => { + for (const [sample, expected] of [ + ["https://example.com/payload", "https://example.com/specs/payload.json"], + ["http://example.com/payload.json", "http://example.com/specs/payload.json"], + ["//example.com/payload", "//example.com/specs/payload.json"], + ["https://example.com/charts/payload.json", "https://example.com/charts/specs/payload.json"], + ["https://example.com/payload.JSON", "https://example.com/specs/payload.JSON"], + ["https://example.com/payload.json?version=2", "https://example.com/specs/payload.json?version=2"], + ]) { + assert.equal(Common.initialSamplePath(sample), expected); + } +}); + test("initial loading rejects invalid selections before fetch and keeps the empty spec fallback", async t => { globalThis.window = {}; const { Main } = await import("../wwwroot/js/client.js"); @@ -91,3 +103,21 @@ test("initial loading fetches a valid bundled spec and installs its text", async assert.equal(fetchMock.mock.callCount(), 1); assert.equal(loaded, spec); }); + +test("initial loading accepts external specs without bypassing expression validation", async t => { + const { Main } = await import("../wwwroot/js/client.js"); + const { Signal } = await import("../../spec/dist/signal.js"); + const spec = JSON.stringify({ + signals: [{ + name: "unsafe", + update: "('a')['constructor']['constructor']('alert(document.domain)')()", + }], + }); + const fetchMock = t.mock.method(globalThis, "fetch", async () => ({ text: async () => spec })); + let loaded; + const client = { _sampleLoaded: text => { loaded = text; } }; + await Main.prototype._loadInitialSampleAsync.call(client, "https://example.com/payload"); + assert.equal(fetchMock.mock.calls[0].arguments[0], "https://example.com/specs/payload.json"); + assert.equal(loaded, spec); + assert.throws(() => Signal.fromJSON(undefined, JSON.parse(loaded).signals[0]), /expression/); +}); From 95677b8d95a9f84a1c4810117fd3d3c916a9f420 Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 17:49:22 -0700 Subject: [PATCH 4/8] Remove test command notes from README Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 59c7f60..fe23e87 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,3 @@ The project can be installed and run locally using the following steps: 2. `npm install` to install any required dependencies 3. Build the project using `npm run dev-client` 4. Start the web site using `npm run start-client`, which also opens the `client.html` page in the default web browser - -Run the expression parser regression tests with `npm run test-expression`. -Run the initial sample URL regression tests with `npm run test-sample-url`. \ No newline at end of file From c6d199644c8e342ccc79a431d52a6c6c84938438 Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 17:53:10 -0700 Subject: [PATCH 5/8] Restore original README Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index fe23e87..058beb4 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,6 @@ The JSON specification can define data sources and transformations, scales, axes The [Try Online](https://microsoft.github.io/morphcharts/client.html) page also supports using **local files**. -The client's `spec` and `plot` query parameters accept a bare sample name or a name prefixed with `samples/`, `gallery/`, or `docs/`, optionally ending in `.json`. External HTTP(S) and protocol-relative URLs are also supported: for example, `https://example.com/charts/demo` loads `https://example.com/charts/specs/demo.json`. External servers must permit cross-origin requests. Other protocols and local traversal paths are not accepted. - -Expressions in specifications support named built-in functions, dataset fields, and numeric array/string indexing. General JavaScript property access and calls on expression results are not supported; use `datum['field name']` for dataset columns and helpers such as `length(value)` for string or array lengths. - ### Gallery Check out the [Gallery](https://microsoft.github.io/morphcharts/gallery.html) to see more examples. @@ -69,4 +65,4 @@ The project can be installed and run locally using the following steps: 1. Clone the repo 2. `npm install` to install any required dependencies 3. Build the project using `npm run dev-client` -4. Start the web site using `npm run start-client`, which also opens the `client.html` page in the default web browser +4. Start the web site using `npm run start-client`, which also opens the `client.html` page in the default web browser \ No newline at end of file From e4848ba647b5c855f22fb7f3d8e40ee785d2171a Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 18:46:59 -0700 Subject: [PATCH 6/8] Keep URL loading behavior unchanged Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- client/src/client.ts | 3 +- client/src/common.ts | 17 ----- client/test/sample-url.test.mjs | 123 -------------------------------- package.json | 1 - 4 files changed, 2 insertions(+), 142 deletions(-) delete mode 100644 client/test/sample-url.test.mjs diff --git a/client/src/client.ts b/client/src/client.ts index ee824db..a1235f0 100644 --- a/client/src/client.ts +++ b/client/src/client.ts @@ -435,8 +435,9 @@ export class Main { private async _loadInitialSampleAsync(sample: string | null): Promise { const emptySpec = "{}"; if (sample) { + const value = sample.toLowerCase().endsWith(".json") ? sample : `${sample}.json`; + const path = value.includes("/") ? `${value.substring(0, value.lastIndexOf("/"))}/specs/${value.substring(value.lastIndexOf("/") + 1)}` : `samples/specs/${value}`; try { - const path = Common.initialSamplePath(sample); const text = await fetch(path).then(response => response.text()); this._sampleLoaded(text); } diff --git a/client/src/common.ts b/client/src/common.ts index e38868d..1e0c206 100644 --- a/client/src/common.ts +++ b/client/src/common.ts @@ -17,23 +17,6 @@ export interface ISampleCategory { } export class Common { - static initialSamplePath(sample: string): string { - if (/^(?:https?:)?\/\//i.test(sample)) { - const protocolRelative = sample.startsWith("//"); - const url = new URL(protocolRelative ? `https:${sample}` : sample); - const slash = url.pathname.lastIndexOf("/"); - let filename = url.pathname.slice(slash + 1); - if (!filename) { throw new Error("invalid sample URL"); } - if (!filename.toLowerCase().endsWith(".json")) { filename += ".json"; } - url.pathname = `${url.pathname.slice(0, slash)}/specs/${filename}`; - return protocolRelative ? url.href.slice(url.protocol.length) : url.href; - } - const name = sample.toLowerCase().endsWith(".json") ? sample.slice(0, -5) : sample; - const match = /^(?:(samples|gallery|docs)\/)?([a-zA-Z0-9_-]+)$/.exec(name); - if (!match || match[0] != name) { throw new Error("invalid sample identifier"); } - return `${match[1] ?? "samples"}/specs/${match[2]}.json`; - } - static async loadSampleIndex(path: string): Promise { try { const response = await fetch(path); diff --git a/client/test/sample-url.test.mjs b/client/test/sample-url.test.mjs deleted file mode 100644 index 2d359ea..0000000 --- a/client/test/sample-url.test.mjs +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -import assert from "node:assert/strict"; -import { test } from "node:test"; -import { Common } from "../wwwroot/js/common.js"; - -const invalidSamples = [ - "javascript:alert(1)", - "data:application/json,{}", - "file:///payload", - "ftp://example.com/payload", - "../payload", - "/payload", - "samples/../payload", - "gallery/../../payload", - "other/payload", - "samples/specs/payload", - "samples\\payload", - "\\\\attacker.example\\payload", - "%2e%2e%2fpayload", - "%252e%252e%252fpayload", - "samples%2fpayload", - "samples/payload?redirect=https://attacker.example", - "samples/payload#fragment", - "samples/", - ".json", - " payload", - "payload ", - "payload\n", - "payload.json\n", - "payload\r\n", - "payload\u0000", -]; - -test("sample identifiers resolve to bundled spec directories", () => { - for (const [sample, expected] of [ - ["bar1", "samples/specs/bar1.json"], - ["bar1.json", "samples/specs/bar1.json"], - ["bar1.JSON", "samples/specs/bar1.json"], - ["samples/bar1", "samples/specs/bar1.json"], - ["samples/bar1.json", "samples/specs/bar1.json"], - ["gallery/earthquakes1", "gallery/specs/earthquakes1.json"], - ["gallery/earthquakes1.json", "gallery/specs/earthquakes1.json"], - ["docs/primitives1", "docs/specs/primitives1.json"], - ["docs/primitives1.json", "docs/specs/primitives1.json"], - ["Example_1-2", "samples/specs/Example_1-2.json"], - ]) { - assert.equal(Common.initialSamplePath(sample), expected); - } - for (const sample of invalidSamples) { - assert.throws(() => Common.initialSamplePath(sample), /invalid sample identifier/, sample); - } -}); - -test("external HTTP(S) URLs retain the existing specs directory mapping", () => { - for (const [sample, expected] of [ - ["https://example.com/payload", "https://example.com/specs/payload.json"], - ["http://example.com/payload.json", "http://example.com/specs/payload.json"], - ["//example.com/payload", "//example.com/specs/payload.json"], - ["https://example.com/charts/payload.json", "https://example.com/charts/specs/payload.json"], - ["https://example.com/payload.JSON", "https://example.com/specs/payload.JSON"], - ["https://example.com/payload.json?version=2", "https://example.com/specs/payload.json?version=2"], - ]) { - assert.equal(Common.initialSamplePath(sample), expected); - } -}); - -test("initial loading rejects invalid selections before fetch and keeps the empty spec fallback", async t => { - globalThis.window = {}; - const { Main } = await import("../wwwroot/js/client.js"); - delete globalThis.window; - - const fetchMock = t.mock.method(globalThis, "fetch", async () => { - assert.fail("invalid selections must not be fetched"); - }); - const logMock = t.mock.method(console, "log", () => {}); - const loaded = []; - const client = { _sampleLoaded: text => loaded.push(text) }; - for (const sample of invalidSamples) { - await Main.prototype._loadInitialSampleAsync.call(client, sample); - } - assert.equal(fetchMock.mock.callCount(), 0); - assert.deepEqual(loaded, invalidSamples.map(() => "{}")); - assert.equal(logMock.mock.callCount(), invalidSamples.length); - - loaded.length = 0; - for (const sample of [null, ""]) { - await Main.prototype._loadInitialSampleAsync.call(client, sample); - } - assert.equal(fetchMock.mock.callCount(), 0); - assert.deepEqual(loaded, ["{}", "{}"]); -}); - -test("initial loading fetches a valid bundled spec and installs its text", async t => { - const { Main } = await import("../wwwroot/js/client.js"); - const spec = '{"signals":[]}'; - const fetchMock = t.mock.method(globalThis, "fetch", async () => ({ text: async () => spec })); - let loaded; - const client = { _sampleLoaded: text => { loaded = text; } }; - await Main.prototype._loadInitialSampleAsync.call(client, "gallery/earthquakes1"); - assert.equal(fetchMock.mock.calls[0].arguments[0], "gallery/specs/earthquakes1.json"); - assert.equal(fetchMock.mock.callCount(), 1); - assert.equal(loaded, spec); -}); - -test("initial loading accepts external specs without bypassing expression validation", async t => { - const { Main } = await import("../wwwroot/js/client.js"); - const { Signal } = await import("../../spec/dist/signal.js"); - const spec = JSON.stringify({ - signals: [{ - name: "unsafe", - update: "('a')['constructor']['constructor']('alert(document.domain)')()", - }], - }); - const fetchMock = t.mock.method(globalThis, "fetch", async () => ({ text: async () => spec })); - let loaded; - const client = { _sampleLoaded: text => { loaded = text; } }; - await Main.prototype._loadInitialSampleAsync.call(client, "https://example.com/payload"); - assert.equal(fetchMock.mock.calls[0].arguments[0], "https://example.com/specs/payload.json"); - assert.equal(loaded, spec); - assert.throws(() => Signal.fromJSON(undefined, JSON.parse(loaded).signals[0]), /expression/); -}); diff --git a/package.json b/package.json index c95bef3..8a235f5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "author": "David Brown", "scripts": { "test-expression": "npm run dev-spec && node --test spec/test/expression.test.mjs", - "test-sample-url": "npm run dev-client && node --test client/test/sample-url.test.mjs", "dev-core": "tsc -p ./core/tsconfig.json", "dev-spec": "tsc -p ./core/tsconfig.json && tsc -p ./spec/tsconfig.json", "dev-webgpuraytrace": "tsc -p ./core/tsconfig.json && tsc -p ./renderers/webgpuraytrace/tsconfig.json", From 9e79df16efb318a743e5f40a0a81fca166fb5c38 Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 19:54:18 -0700 Subject: [PATCH 7/8] Add standard test command Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 8a235f5..9f03a81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "author": "David Brown", "scripts": { + "test": "npm run test-expression", "test-expression": "npm run dev-spec && node --test spec/test/expression.test.mjs", "dev-core": "tsc -p ./core/tsconfig.json", "dev-spec": "tsc -p ./core/tsconfig.json && tsc -p ./spec/tsconfig.json", From 7602e6b894fb5c971cecd2fe13eb9218d3604863 Mon Sep 17 00:00:00 2001 From: Dan Marshall Date: Sat, 24 Oct 2026 20:18:45 -0700 Subject: [PATCH 8/8] Run tests for pull requests and before deployment Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 089dd5de-5a83-4bf9-a0d4-3afbd572168e --- .github/workflows/static.yml | 1 + .github/workflows/test.yml | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 68f807f..b6fa11f 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -34,6 +34,7 @@ jobs: - name: Build run: | npm ci + npm test npm run dev-client npm run dist-all cp -r dist/client/* dist/ diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..4e629ca --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,24 @@ +name: Tests + +on: + pull_request: + branches: ["main"] + +permissions: + contents: read + +jobs: + test: + name: npm test + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + - name: Setup Node + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: "24" + - name: Install dependencies + run: npm ci + - name: Run tests + run: npm test