FIR-1951: Add automatic URL encoding in preprocessing for special characters in query parameters (#1547)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Nicolas Camara <nicolascamara29@gmail.com>
This commit is contained in:
devin-ai-integration[bot] 2025-05-19 19:31:47 +02:00 committed by GitHub
parent 7ccbbec488
commit 8eeb3c5cd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -0,0 +1,34 @@
import { url } from "../../../controllers/v1/types";
import { describe, it, expect } from "@jest/globals";
describe("URL Schema Validation with Special Characters", () => {
it("should handle URLs with special characters in query parameters", () => {
const testUrl = "https://www.boulanger.com/c/nav-filtre/televiseur?_merchant_des~boulanger|brand~lg";
expect(() => url.parse(testUrl)).not.toThrow();
const parsedUrl = url.parse(testUrl);
expect(parsedUrl).toContain("_merchant_des%7Eboulanger%7Cbrand%7Elg");
});
it("should preserve URL structure when encoding special characters", () => {
const testUrl = "https://example.com/path?param1=value1&param2=value~with|special&param3=normal";
expect(() => url.parse(testUrl)).not.toThrow();
const parsedUrl = url.parse(testUrl);
expect(parsedUrl).toContain("example.com/path?");
expect(parsedUrl).toContain("param1=value1");
expect(parsedUrl).toContain("param2=value%7Ewith%7Cspecial");
expect(parsedUrl).toContain("param3=normal");
});
it("should handle URLs with already encoded special characters", () => {
const testUrl = "https://example.com/path?param=value%7Eencoded";
expect(() => url.parse(testUrl)).not.toThrow();
const parsedUrl = url.parse(testUrl);
expect(parsedUrl).toContain("param=value%7Eencoded");
});
});

View File

@ -24,8 +24,18 @@ export type Format =
export const url = z.preprocess( export const url = z.preprocess(
(x) => { (x) => {
if (!protocolIncluded(x as string)) { if (!protocolIncluded(x as string)) {
return `http://${x}`; x = `http://${x}`;
} }
try {
const urlObj = new URL(x as string);
if (urlObj.search) {
const searchParams = new URLSearchParams(urlObj.search.substring(1));
return `${urlObj.origin}${urlObj.pathname}?${searchParams.toString()}`;
}
} catch (e) {
}
return x; return x;
}, },
z z