Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(ingress): percent-encode the route in minted ingress URLs

Yudhi Armyndharis43cdde5d18ee
diff --git a/src/core/plugins/plugin-manifest-ingress.spec.ts b/src/core/plugins/plugin-manifest-ingress.spec.ts
index 937e5fea1..71f8a1834 100644
--- a/src/core/plugins/plugin-manifest-ingress.spec.ts
+++ b/src/core/plugins/plugin-manifest-ingress.spec.ts
@@ -105,6 +105,18 @@ describe('validateIngressManifest', () => {
}
});
+ it('rejects a route holding a lone UTF-16 surrogate (no URL can encode or decode to it)', () => {
+ for (const route of ['\ud800', 'a\udc00b', 'x\ud83d']) {
+ const m = baseManifest();
+ m.ingress[0].route = route;
+ expect(() => validateIngressManifest(m as never)).toThrow(/single URL path segment/);
+ }
+ // A well-formed surrogate pair is an ordinary astral character.
+ const m = baseManifest();
+ m.ingress[0].route = 'hook-\ud83d\ude80';
+ expect(() => validateIngressManifest(m as never)).not.toThrow();
+ });
+
it('rejects a dedupOn value other than header or body', () => {
const m = baseManifest();
(m.ingress[0] as { dedupOn?: string }).dedupOn = 'bdy';
diff --git a/src/core/plugins/plugin.interfaces.ts b/src/core/plugins/plugin.interfaces.ts
index dfbfe7a12..3f8828f65 100644
--- a/src/core/plugins/plugin.interfaces.ts
+++ b/src/core/plugins/plugin.interfaces.ts
@@ -317,14 +317,14 @@ export const SUPPORTED_SDK_MAJOR = 1;
// validated once at load.
const HTTP_HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
const HTTP_HEADER_VALUE = /^[\t\x20-\x7e\x80-\xff]*$/;
-// An ingress route must survive as one URL path segment. The minted ingress URL appends it verbatim
-// and the controller matches the first decoded segment after the instance id, so '/' (and '\\', which a
-// WHATWG URL parser turns into '/') splits it, '?' and '#' end the path, a bare '%' is a malformed
-// escape, and a URL parser strips tab and newline (so control characters are refused outright).
-// Anything else, a space or a non-ASCII letter included, is percent-encoded by the client and
-// decoded back before the match.
+// An ingress route must survive as one URL path segment. The controller matches the first decoded
+// segment after the instance id, so '/' (and '\\', which a WHATWG URL parser turns into '/') splits
+// it, '?' and '#' end the path, a bare '%' is a malformed escape, and a URL parser strips tab and
+// newline (so control characters are refused outright). Anything else, a space or a non-ASCII letter
+// included, is percent-encoded in the minted ingress URL and decoded back before the match. A lone
+// UTF-16 surrogate has no UTF-8 form: encodeURIComponent throws on it and no URL decodes to it.
// eslint-disable-next-line no-control-regex
-const INGRESS_ROUTE_SEGMENT = /^[^/\\?#%\x00-\x1f\x7f]+$/;
+const INGRESS_ROUTE_SEGMENT = /^[^/\\?#%\x00-\x1f\x7f\p{Cs}]+$/u;
/**
* Validates a manifest's `ingress` declarations: SDK major compatibility, the `webhook:ingress`
@@ -363,7 +363,7 @@ export function validateIngressManifest(manifest: PluginManifest, allowUnsignedI
if (typeof r.route !== 'string' || !INGRESS_ROUTE_SEGMENT.test(r.route) || r.route === '.' || r.route === '..') {
throw new Error(
`Plugin ${manifest.id}: ingress route '${String(r.route)}' must be a single URL path segment ` +
- `(no '/', '\\', '?', '#', '%' or control character, and not '.' or '..')`,
+ `(no '/', '\\', '?', '#', '%', control character or lone surrogate, and not '.' or '..')`,
);
}
if (r.signature.scheme === 'none' && !allowUnsignedIngress) {
diff --git a/src/modules/integration/ingress-url.spec.ts b/src/modules/integration/ingress-url.spec.ts
index 4cd462feb..2e8a15b3e 100644
--- a/src/modules/integration/ingress-url.spec.ts
+++ b/src/modules/integration/ingress-url.spec.ts
@@ -18,6 +18,14 @@ describe('buildIngressUrls', () => {
expect(buildIngressUrls(undefined, 'p', 'i', ['r'])).toEqual([{ route: 'r', url: '/api/ingress/p/i/r' }]);
});
+ it('percent-encodes the route so a URL parser keeps it and the router decodes it back', () => {
+ for (const route of ['hook ', 'a b', 'caf\u00e9']) {
+ const url = buildIngressUrls('https://x', 'p', 'i', [route])[0].url;
+ const segment = new URL(url).pathname.split('/').pop() ?? '';
+ expect(decodeURIComponent(segment)).toBe(route);
+ }
+ });
+
it('returns an empty array when the plugin declares no routes', () => {
expect(buildIngressUrls('https://x', 'p', 'i', [])).toEqual([]);
});
diff --git a/src/modules/integration/ingress-url.ts b/src/modules/integration/ingress-url.ts
index 9b4916b42..06c1166fd 100644
--- a/src/modules/integration/ingress-url.ts
+++ b/src/modules/integration/ingress-url.ts
@@ -11,7 +11,8 @@ export class IngressUrl {
}
// Absolute URL from BASE_URL when set (trailing slash trimmed), else a relative path the operator
-// prepends with their own host. Never throws.
+// prepends with their own host. The route is percent-encoded: a URL parser strips a trailing space
+// and the router decodes the segment back to the declared route. Never throws.
export function buildIngressUrls(
baseUrl: string | undefined,
pluginId: string,
@@ -21,6 +22,6 @@ export function buildIngressUrls(
const base = (baseUrl ?? '').replace(/\/+$/, '');
return routes.map(route => ({
route,
- url: `${base}/api/ingress/${pluginId}/${instanceId}/${route}`,
+ url: `${base}/api/ingress/${pluginId}/${instanceId}/${encodeURIComponent(route)}`,
}));
}