refactor(domain): extract artifact path syntax helpers (#830)
Move hasScheme/stripQueryAndFragment/admitsCanonicalPathSyntax plus looksExternal into domain/artifactPathSyntax with the deliberate policy-divergence table (artifact root vs allowedOrigins vs exact-origin fetch). Adopt in JavaScriptArtifactPathResolution and the Electron native specifier check with characterization tests. Policy unification tracked in #829.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
admitsCanonicalPathSyntax,
|
||||
hasScheme,
|
||||
looksExternal,
|
||||
stripQueryAndFragment,
|
||||
} from "./artifactPathSyntax.js";
|
||||
|
||||
describe("artifact path syntax", () => {
|
||||
it("detects URI schemes without matching bare paths", () => {
|
||||
expect(hasScheme("https://example.test/app.js")).toBe(true);
|
||||
expect(hasScheme("file:///app.js")).toBe(true);
|
||||
expect(hasScheme("node:fs")).toBe(true);
|
||||
expect(hasScheme("./relative.js")).toBe(false);
|
||||
expect(hasScheme("/absolute.js")).toBe(false);
|
||||
expect(hasScheme("app.js")).toBe(false);
|
||||
});
|
||||
|
||||
it("flags scheme-qualified and protocol-relative references as external", () => {
|
||||
expect(looksExternal("https://example.test/app.js")).toBe(true);
|
||||
expect(looksExternal("//example.test/app.js")).toBe(true);
|
||||
expect(looksExternal("./app.js")).toBe(false);
|
||||
expect(looksExternal("/app.js")).toBe(false);
|
||||
});
|
||||
|
||||
it("strips queries and fragments in fragment-first order", () => {
|
||||
expect(stripQueryAndFragment("./app.js?token=secret#hash")).toBe(
|
||||
"./app.js",
|
||||
);
|
||||
expect(stripQueryAndFragment("./app.js#hash?token=secret")).toBe(
|
||||
"./app.js",
|
||||
);
|
||||
expect(stripQueryAndFragment("./app.js")).toBe("./app.js");
|
||||
});
|
||||
|
||||
it("rejects NUL, backslash, and encoded dot/separator bytes", () => {
|
||||
expect(admitsCanonicalPathSyntax("./app.js")).toBe(true);
|
||||
expect(admitsCanonicalPathSyntax("dir\\app.js")).toBe(false);
|
||||
expect(admitsCanonicalPathSyntax("dir\0app.js")).toBe(false);
|
||||
expect(admitsCanonicalPathSyntax("./%2e%2e/secret.js")).toBe(false);
|
||||
expect(admitsCanonicalPathSyntax("./%2Fapp.js")).toBe(false);
|
||||
expect(admitsCanonicalPathSyntax("./%5capp.js")).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user