Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-js): refuse a raw request path without a leading slash

Yudhi Armyndharis7123cd8feb3e
diff --git a/sdk/javascript/src/http.ts b/sdk/javascript/src/http.ts
index fbf2d3842..0fcd4dfba 100644
--- a/sdk/javascript/src/http.ts
+++ b/sdk/javascript/src/http.ts
@@ -19,7 +19,7 @@ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
export interface RequestOptions {
method: HttpMethod;
- /** Full path beginning with `/`, e.g. `/api/sessions`. */
+ /** Full path beginning with `/`, e.g. `/api/sessions`; any other path is refused and nothing is sent. */
path: string;
/** Query parameters, serialized into the URL. */
query?: object;
@@ -150,6 +150,11 @@ async function send<T>(
options: RequestOptions,
consume: (res: Response) => Promise<T>,
): Promise<T> {
+ // The path is appended to the base URL, so one without a leading `/` could move the host
+ // (`.example.net/x`, `@example.net/x`) and send the API key there.
+ if (!options.path.startsWith('/')) {
+ throw new TypeError(`OpenWA: path must begin with "/": ${JSON.stringify(options.path)}`);
+ }
if (options.path.includes(BLANK_SEGMENT)) {
throw new TypeError(`OpenWA: empty or dot path segment in ${JSON.stringify(options.path)}`);
}
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index 838badcaa..0c15784d1 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -105,6 +105,16 @@ describe('OpenWAClient', () => {
expect(t.calls).toHaveLength(2);
});
+ it('refuses a raw path that does not begin with a slash, so the request never leaves the base host', async () => {
+ const t = new MockTransport().passthrough({ status: 200, body: [] });
+ const c = client(t);
+ for (const path of ['.example.net/api/sessions', '@example.net/x', 'api/sessions', '']) {
+ await expect(c.request({ method: 'GET', path })).rejects.toThrow(TypeError);
+ await expect(c.requestBytes({ method: 'GET', path })).rejects.toThrow(TypeError);
+ }
+ expect(t.calls).toHaveLength(0);
+ });
+
it('sends a raw path with a trailing slash, a double slash or only a slash as written', async () => {
const t = new MockTransport().passthrough({ status: 200, body: [] });
const c = client(t);