refactor: share gateway security path canonicalization

This commit is contained in:
Peter Steinberger
2026-02-26 17:23:41 +01:00
parent 15e3e63705
commit 08e3357480
3 changed files with 57 additions and 53 deletions

View File

@@ -3,6 +3,7 @@ import { describe, expect, test, vi } from "vitest";
import type { createSubsystemLogger } from "../logging/subsystem.js";
import type { ResolvedGatewayAuth } from "./auth.js";
import type { HooksConfigResolved } from "./hooks.js";
import { canonicalizePathVariant } from "./security-path.js";
import { createGatewayHttpServer, createHooksRequestHandler } from "./server-http.js";
import { withTempConfig } from "./test-temp-config.js";
@@ -87,30 +88,7 @@ function createHooksConfig(): HooksConfigResolved {
}
function canonicalizePluginPath(pathname: string): string {
let decoded = pathname;
for (let pass = 0; pass < 3; pass++) {
let nextDecoded = decoded;
try {
nextDecoded = decodeURIComponent(decoded);
} catch {
break;
}
if (nextDecoded === decoded) {
break;
}
decoded = nextDecoded;
}
let resolved = decoded;
try {
resolved = new URL(decoded, "http://localhost").pathname;
} catch {
resolved = decoded;
}
const collapsed = resolved.toLowerCase().replace(/\/{2,}/g, "/");
if (collapsed.length <= 1) {
return collapsed;
}
return collapsed.replace(/\/+$/, "");
return canonicalizePathVariant(pathname);
}
type RouteVariant = {