Skip to content

Commit

Permalink
fix: Ignore query strings when parsing URLs (#2883)
Browse files Browse the repository at this point in the history
Mobile uses a polyfill for fetch that adds certain query strings to
fetch URLs, this breaks our CLI server implementation as it doesn't
expect query strings. This PR changes the server implementation to
ignore query strings and only look at `pathname`.
  • Loading branch information
FrederikBolding authored Nov 13, 2024
1 parent 8efc3b3 commit 8815d9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/snaps-cli/src/webpack/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,34 @@ describe('getServer', () => {
await close();
});

it('ignores query strings', async () => {
const config = getMockConfig('webpack', {
input: 'src/index.js',
server: {
root: '/foo',
port: 0,
},
});

const server = getServer(config);
const { port, close } = await server.listen();

const response = await fetch(
`http://localhost:${port}/snap.manifest.json?_=1731493314736`,
);

expect(response.status).toBe(200);
expect(await response.text()).toBe('');

expect(serveMiddleware).toHaveBeenCalledWith(
expect.any(IncomingMessage),
expect.any(ServerResponse),
expect.objectContaining({ public: expect.stringContaining('foo') }),
);

await close();
});

it('responds with 404 for non-allowed files', async () => {
const config = getMockConfig('webpack', {
input: 'src/index.js',
Expand Down
6 changes: 5 additions & 1 deletion packages/snaps-cli/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ export function getServer(config: ProcessedConfig) {
const { result } = await readJsonFile<SnapManifest>(manifestPath);
const allowedPaths = getAllowedPaths(config, result);

const path = request.url?.slice(1);
const pathname =
request.url &&
request.headers.host &&
new URL(request.url, `http://${request.headers.host}`).pathname;
const path = pathname?.slice(1);
const allowed = allowedPaths.some((allowedPath) => path === allowedPath);

if (!allowed) {
Expand Down

0 comments on commit 8815d9b

Please sign in to comment.