Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
114 lines
4.5 KiB
TypeScript
114 lines
4.5 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { paging, pathInt, SORT, TEXT, upstreamQuery } from "./upstreamQuery";
|
|
|
|
// portal-query-allowlist (F38): a BFF route forwards only allow-listed, validated, encoded query params.
|
|
|
|
const sp = (qs: string) => new URLSearchParams(qs);
|
|
|
|
describe("upstreamQuery", () => {
|
|
const rules = {
|
|
page: { type: "int", min: 0, max: 10000 },
|
|
size: { type: "int", min: 1, max: 100 },
|
|
q: { type: "string", maxLength: 10 },
|
|
code: { type: "string", maxLength: 20, pattern: /^[A-Z0-9]+$/ },
|
|
stage: { type: "enum", values: ["TO_PAY", "SHIPPED"] },
|
|
} as const;
|
|
|
|
it("returns an empty string when nothing is forwarded", () => {
|
|
expect(upstreamQuery(sp(""), rules)).toBe("");
|
|
expect(upstreamQuery(sp("evil=1"), rules)).toBe("");
|
|
});
|
|
|
|
it("forwards allow-listed keys in the caller's order, with a leading ?", () => {
|
|
expect(upstreamQuery(sp("size=5&page=1"), rules)).toBe("?size=5&page=1");
|
|
});
|
|
|
|
it("drops keys that are not in the rules", () => {
|
|
expect(upstreamQuery(sp("page=1&memberCode=X&size=5&buyerCode=Y"), rules)).toBe("?page=1&size=5");
|
|
});
|
|
|
|
it("keeps only the first value of a repeated key", () => {
|
|
expect(upstreamQuery(sp("page=1&page=2&size=5&size=7"), rules)).toBe("?page=1&size=5");
|
|
});
|
|
|
|
it("clamps an int above max to max and below min to min", () => {
|
|
expect(upstreamQuery(sp("size=100000"), rules)).toBe("?size=100");
|
|
expect(upstreamQuery(sp("size=0"), rules)).toBe("?size=1");
|
|
expect(upstreamQuery(sp("page=-5"), rules)).toBe("?page=0");
|
|
});
|
|
|
|
it("drops an int that is not an integer", () => {
|
|
expect(upstreamQuery(sp("page=abc&size=1.5"), rules)).toBe("");
|
|
expect(upstreamQuery(sp("page=1e3"), rules)).toBe("");
|
|
expect(upstreamQuery(sp("page=1%26size%3D9"), rules)).toBe("");
|
|
});
|
|
|
|
it("drops empty strings", () => {
|
|
expect(upstreamQuery(sp("q=&page=&size=5"), rules)).toBe("?size=5");
|
|
});
|
|
|
|
it("drops a string longer than maxLength or failing its pattern", () => {
|
|
expect(upstreamQuery(sp("q=12345678901"), rules)).toBe("");
|
|
expect(upstreamQuery(sp("code=ab-1"), rules)).toBe("");
|
|
expect(upstreamQuery(sp("code=AB1"), rules)).toBe("?code=AB1");
|
|
});
|
|
|
|
it("forwards only a listed enum value", () => {
|
|
expect(upstreamQuery(sp("stage=SHIPPED"), rules)).toBe("?stage=SHIPPED");
|
|
expect(upstreamQuery(sp("stage=ALL"), rules)).toBe("");
|
|
});
|
|
|
|
it("encodes & and = so a value cannot inject another parameter", () => {
|
|
const out = upstreamQuery(sp("q=a%26b%3Dc"), rules);
|
|
expect(out).toBe("?q=a%26b%3Dc");
|
|
const parsed = new URLSearchParams(out.slice(1));
|
|
expect([...parsed.keys()]).toEqual(["q"]);
|
|
expect(parsed.get("q")).toBe("a&b=c");
|
|
});
|
|
|
|
it("encodes a space as %20 and leaves no raw reserved characters", () => {
|
|
expect(upstreamQuery(sp("q=a%20b%23"), rules)).toBe("?q=a%20b%23");
|
|
});
|
|
|
|
it("cannot be steered by a level-style injection (level=1%26memberCode%3DX)", () => {
|
|
const out = upstreamQuery(sp("level=1%26memberCode%3DX"), { level: { type: "int", min: 1, max: 99 } });
|
|
expect(out).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("shared rules", () => {
|
|
const rules = { q: TEXT, ...paging(500), ...SORT };
|
|
|
|
it("paging clamps size to the portal's cap and page to [0, 10000]", () => {
|
|
expect(upstreamQuery(sp("page=99999&size=501"), rules)).toBe("?page=10000&size=500");
|
|
expect(upstreamQuery(sp("page=0&size=500"), rules)).toBe("?page=0&size=500");
|
|
});
|
|
|
|
it("SORT forwards a plain field name and asc/desc only", () => {
|
|
expect(upstreamQuery(sp("sortBy=member.name&sortDir=desc"), rules)).toBe("?sortBy=member.name&sortDir=desc");
|
|
expect(upstreamQuery(sp("sortBy=name%3Bdrop&sortDir=sideways"), rules)).toBe("");
|
|
});
|
|
|
|
it("TEXT drops control characters and over-long values", () => {
|
|
expect(upstreamQuery(sp("q=a%0Ab"), rules)).toBe("");
|
|
expect(upstreamQuery(sp(`q=${"a".repeat(201)}`), rules)).toBe("");
|
|
expect(upstreamQuery(sp("q=IRENE%20LIM"), rules)).toBe("?q=IRENE%20LIM");
|
|
});
|
|
});
|
|
|
|
describe("pathInt", () => {
|
|
it("returns the integer when it is within range", () => {
|
|
expect(pathInt("2", 1, 10)).toBe(2);
|
|
});
|
|
|
|
it("returns null for absent, non-integer, out-of-range or injected values", () => {
|
|
expect(pathInt(null, 1, 10)).toBeNull();
|
|
expect(pathInt("", 1, 10)).toBeNull();
|
|
expect(pathInt("abc", 1, 10)).toBeNull();
|
|
expect(pathInt("0", 1, 10)).toBeNull();
|
|
expect(pathInt("11", 1, 10)).toBeNull();
|
|
expect(pathInt("2/../../admin", 1, 10)).toBeNull();
|
|
expect(pathInt("2?x=1", 1, 10)).toBeNull();
|
|
});
|
|
});
|