A local-first personal finance app
[AI] Sync server: harden CORS proxy method validation (#7788)
Matiss Janis Aboltins18c704b3bae7
diff --git a/packages/sync-server/src/app-cors-proxy.js b/packages/sync-server/src/app-cors-proxy.jsindex c5f45c3666..d94e9283dd 100644--- a/packages/sync-server/src/app-cors-proxy.js+++ b/packages/sync-server/src/app-cors-proxy.js@@ -177,15 +177,12 @@ app.use('/', async (req, res) => {}try {- // Extract method, body, and headers from the request body (sent by loot-core)- const {- method = 'GET',- body,- headers: customHeaders = {},- } = req.body || {};+ const { method = 'GET', headers: customHeaders = {} } = req.body || {};- const methodNormalized =- typeof method === 'string' ? method.toUpperCase() : 'GET';+ if (typeof method !== 'string') {+ return res.status(400).json({ error: 'Invalid method parameter' });+ }+ const methodNormalized = method.toUpperCase();if (!['GET', 'HEAD'].includes(methodNormalized)) {return res.status(405).json({ error: 'Method not allowed' });}@@ -218,13 +215,8 @@ app.use('/', async (req, res) => {}const response = await fetch(url.href, {- method,+ method: methodNormalized,headers: requestHeaders,- body: ['GET', 'HEAD'].includes(method)- ? undefined- : typeof body === 'string'- ? body- : JSON.stringify(body),});const contentType =diff --git a/packages/sync-server/src/app-cors-proxy.test.js b/packages/sync-server/src/app-cors-proxy.test.jsindex ef39e98893..4d456be689 100644--- a/packages/sync-server/src/app-cors-proxy.test.js+++ b/packages/sync-server/src/app-cors-proxy.test.js@@ -414,6 +414,55 @@ describe('app-cors-proxy', () => {expect(res.statusCode).toBe(405);expect(res.body.error).toBe('Method not allowed');});++ it('should reject non-string method (array bypass)', async () => {+ global.fetch.mockClear();++ const res = await request(app)+ .get('/')+ .send({ method: ['POST'], body: { evil: true } })+ .query({ url: 'https://api.github.com/repos/user/repo1' });++ expect(res.statusCode).toBe(400);+ expect(res.body.error).toBe('Invalid method parameter');++ const proxyCalls = global.fetch.mock.calls.filter(+ ([url]) => url === 'https://api.github.com/repos/user/repo1',+ );+ expect(proxyCalls).toHaveLength(0);+ });++ it('should reject non-string method (object bypass)', async () => {+ global.fetch.mockClear();++ const res = await request(app)+ .get('/')+ .send({ method: { toString: () => 'POST' } })+ .query({ url: 'https://github.com/user/repo1' });++ expect(res.statusCode).toBe(400);+ expect(res.body.error).toBe('Invalid method parameter');++ const proxyCalls = global.fetch.mock.calls.filter(+ ([url]) => url === 'https://github.com/user/repo1',+ );+ expect(proxyCalls).toHaveLength(0);+ });++ it('should forward the validated method to fetch, not the raw input', async () => {+ global.fetch.mockClear();++ await request(app)+ .get('/')+ .send({ method: 'get' })+ .query({ url: 'https://github.com/user/repo1' });++ const proxyCall = global.fetch.mock.calls.find(+ ([url]) => url === 'https://github.com/user/repo1',+ );+ expect(proxyCall).toBeDefined();+ expect(proxyCall[1].method).toBe('GET');+ });});describe('GitHub authentication', () => {diff --git a/upcoming-release-notes/7788.md b/upcoming-release-notes/7788.mdnew file mode 100644index 0000000000..7179bcb49c--- /dev/null+++ b/upcoming-release-notes/7788.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [MatissJanis]+---++Fix an issue where the CORS proxy could be bypassed.