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
64 lines
3.0 KiB
TypeScript
64 lines
3.0 KiB
TypeScript
// portal-query-allowlist (F38): the one way a BFF route builds the query it sends to mobile-api.
|
|
// Only allow-listed keys are forwarded, each value is validated, and the output is percent-encoded,
|
|
// so a client can neither smuggle an extra upstream parameter (`a=1%26memberCode%3DX`) nor reach
|
|
// a parameter the route never meant to expose. This file is identical in both portals.
|
|
|
|
export type QueryRule =
|
|
| { type: "int"; min: number; max: number }
|
|
| { type: "string"; maxLength: number; pattern?: RegExp }
|
|
| { type: "enum"; values: readonly string[] };
|
|
|
|
const INT = /^-?\d{1,15}$/;
|
|
|
|
function accept(value: string, rule: QueryRule): string | null {
|
|
if (value === "") return null;
|
|
switch (rule.type) {
|
|
case "int": {
|
|
if (!INT.test(value)) return null;
|
|
return String(Math.min(rule.max, Math.max(rule.min, Number(value))));
|
|
}
|
|
case "string":
|
|
if (value.length > rule.maxLength) return null;
|
|
return !rule.pattern || rule.pattern.test(value) ? value : null;
|
|
case "enum":
|
|
return rule.values.includes(value) ? value : null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The upstream query for `source`: "" when nothing is forwarded, otherwise "?k=v&…". Keys absent from
|
|
* `rules` are dropped; a repeated key forwards its first value only; an empty or invalid value is dropped,
|
|
* except an out-of-range int, which is clamped to its rule's min/max. Keys keep the caller's order.
|
|
*/
|
|
export function upstreamQuery(source: URLSearchParams, rules: Readonly<Record<string, QueryRule>>): string {
|
|
const out = new URLSearchParams();
|
|
for (const [key, raw] of source) {
|
|
if (out.has(key) || !Object.hasOwn(rules, key)) continue;
|
|
const value = accept(raw, rules[key]);
|
|
if (value !== null) out.append(key, value);
|
|
}
|
|
// Serialised with encodeURIComponent (a space is %20, not URLSearchParams' "+"): both decode the same upstream.
|
|
const qs = [...out].map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join("&");
|
|
return qs ? `?${qs}` : "";
|
|
}
|
|
|
|
/** Rules shared by most list routes. Each route still names its own keys; nothing is forwarded by default. */
|
|
export function paging(maxSize: number): Record<"page" | "size", QueryRule> {
|
|
return { page: { type: "int", min: 0, max: 10000 }, size: { type: "int", min: 1, max: maxSize } };
|
|
}
|
|
|
|
export const SORT: Readonly<Record<"sortBy" | "sortDir", QueryRule>> = {
|
|
sortBy: { type: "string", maxLength: 64, pattern: /^[A-Za-z0-9_.]+$/ },
|
|
sortDir: { type: "enum", values: ["asc", "desc"] },
|
|
};
|
|
|
|
/** Free text or an id/date filter that mobile-api validates itself: bounded, no control characters. */
|
|
export const TEXT: QueryRule = { type: "string", maxLength: 200, pattern: /^[^\x00-\x1f\x7f]*$/ };
|
|
|
|
/** A query value used as a path segment: the integer when it is a plain integer within [min, max], else null. */
|
|
export function pathInt(value: string | null, min: number, max: number): number | null {
|
|
if (value === null || !/^\d{1,15}$/.test(value)) return null;
|
|
const n = Number(value);
|
|
return n >= min && n <= max ? n : null;
|
|
}
|