Skip to content

Commit e5d1d9b

Browse files
authored
fix(engine): avoid transform regex backtracking (#3704)
1 parent f13037e commit e5d1d9b

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎packages/engine/src/utils/alphaBlit.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,29 @@ describe("blitRgb48leRegion", () => {
815815
// ── parseTransformMatrix tests ───────────────────────────────────────────────
816816

817817
describe("parseTransformMatrix", () => {
818+
it("preserves whitespace and numeric conversion for 2D and 3D matrices", () => {
819+
expect(parseTransformMatrix("matrix(\t1 ,\n-0 , .5 , 1e0 , 0x10 , )")).toEqual([
820+
1, -0, 0.5, 1, 16, 0,
821+
]);
822+
expect(
823+
parseTransformMatrix("matrix3d(\t1 , , 0, 0, .5, 1e0, 0, 0, 0, 0, 1, 0, 0x10, , 0, 1 )"),
824+
).toEqual([1, 0, 0.5, 1, 16, 0]);
825+
expect(parseTransformMatrix("matrix(1,,0,1,0,0)")).toBeNull();
826+
});
827+
828+
it.each([
829+
"matrix(",
830+
"matrix(1,",
831+
"matrix(1,0,",
832+
"matrix(1,0,0,",
833+
"matrix(1,0,0,1,",
834+
"matrix(1,0,0,1,0,",
835+
"matrix(1,0,0,1,0,(",
836+
"matrix3d(",
837+
])("rejects long malformed whitespace after %s", (prefix) => {
838+
expect(parseTransformMatrix(prefix + " ".repeat(100_000) + "!")).toBeNull();
839+
});
840+
818841
it("returns null for 'none'", () => {
819842
expect(parseTransformMatrix("none")).toBeNull();
820843
});

‎packages/engine/src/utils/alphaBlit.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -920,16 +920,16 @@ export function normalizeObjectFit(value: string | undefined): ObjectFit {
920920
export function parseTransformMatrix(css: string): number[] | null {
921921
if (!css || css === "none") return null;
922922

923-
const match2d = css.match(
924-
/^matrix\(\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^,]+),\s*([^,)]+)\s*\)$/,
925-
);
923+
// The captures already include whitespace; overlapping whitespace quantifiers
924+
// cause excessive backtracking on malformed input. Number handles the spaces.
925+
const match2d = css.match(/^matrix\(([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,)]+)\)$/);
926926
if (match2d) {
927927
const values = match2d.slice(1, 7).map(Number);
928928
if (!values.every(Number.isFinite)) return null;
929929
return values;
930930
}
931931

932-
const match3d = css.match(/^matrix3d\(\s*([^)]+)\)$/);
932+
const match3d = css.match(/^matrix3d\(([^)]+)\)$/);
933933
if (match3d) {
934934
const raw = match3d[1];
935935
if (!raw) return null;

0 commit comments

Comments
 (0)