Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"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",
"dev-webgpuraytrace": "tsc -p ./core/tsconfig.json && tsc -p ./renderers/webgpuraytrace/tsconfig.json",
Expand Down
149 changes: 147 additions & 2 deletions spec/src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,127 @@ export class Expression {
return this._buildExpression(this._parseExpression(expr, group, dataset), group, dataset);
}

private _validateExpression(expr: string): Set<number> {
const indices = new Set<number>();
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) {
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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 {
Expand Down
113 changes: 113 additions & 0 deletions spec/test/expression.test.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
});
Loading