From 0fa72c10af9402e33d56b2f8b794712cf15cebcf Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:00:14 +0000 Subject: [PATCH] fix: reject pack POSTs with mismatched Content-Type, classify by path bug: a POST to git-receive-pack with a non-exact Content-Type was classified as a default action and forwarded upstream without running the push chain. Co-Authored-By: Samir Chaudhry --- .../processors/pre-processor/parseAction.ts | 17 +++--- src/proxy/routes/helper.ts | 34 +++++++++++- test/testParseAction.test.ts | 39 ++++++++++++++ test/testProxyRoute.test.ts | 45 ++++++++++++++++ test/testRouteFilter.test.ts | 52 ++++++++++++++++--- 5 files changed, 168 insertions(+), 19 deletions(-) diff --git a/src/proxy/processors/pre-processor/parseAction.ts b/src/proxy/processors/pre-processor/parseAction.ts index 08c4637eb..dfee5ba20 100644 --- a/src/proxy/processors/pre-processor/parseAction.ts +++ b/src/proxy/processors/pre-processor/parseAction.ts @@ -17,27 +17,22 @@ import { Request } from 'express'; import { Action } from '../../actions'; -import { processUrlPath } from '../../routes/helper'; +import { getActionTypeFromGitPath, processUrlPath } from '../../routes/helper'; import * as db from '../../../db'; const exec = async (req: Request) => { const id = Date.now(); const timestamp = id; - let type = 'default'; - - //inspect content-type headers to classify requests as push or pull operations - // see git http protocol docs for more details: https://github.com/git/git/blob/master/Documentation/gitprotocol-http.adoc - if (req.headers['content-type'] === 'application/x-git-upload-pack-request') { - type = 'pull'; - } else if (req.headers['content-type'] === 'application/x-git-receive-pack-request') { - type = 'push'; - } - // Proxy URLs take the form https://:// // e.g. https://git-proxy-instance.com:8443/github.com/finos/git-proxy.git // We'll receive /github.com/finos/git-proxy.git as the req.url / req.originalUrl // Add protocol (assume SSL) to reconstruct full URL - noting path will start with a / const pathBreakdown = processUrlPath(req.originalUrl); + + // classify the request as push or pull from the git operation in the URL path, which is + // what the upstream executes, rather than from client-controlled headers + // see git http protocol docs: https://github.com/git/git/blob/master/Documentation/gitprotocol-http.adoc + const type = getActionTypeFromGitPath(pathBreakdown?.gitPath); let url = 'https:/' + (pathBreakdown?.repoPath ?? 'NOT-FOUND'); console.log(`Parse action calculated repo URL: ${url} for inbound URL path: ${req.originalUrl}`); diff --git a/src/proxy/routes/helper.ts b/src/proxy/routes/helper.ts index d715fb89e..5d4881b5b 100644 --- a/src/proxy/routes/helper.ts +++ b/src/proxy/routes/helper.ts @@ -157,6 +157,12 @@ export const processGitURLForNameAndOrg = (gitUrl: string): GitNameBreakdown | n } }; +/** Content-Type required for each git pack endpoint. */ +const PACK_CONTENT_TYPES = new Map([ + ['/git-upload-pack', 'application/x-git-upload-pack-request'], + ['/git-receive-pack', 'application/x-git-receive-pack-request'], +]); + /** * Check whether an HTTP request has the expected properties of a * Git HTTP request. The URL is expected to be "sanitized", stripped of @@ -181,12 +187,36 @@ export const validGitRequest = (gitPath: string, headers: IncomingHttpHeaders): // sent in this request return agent.startsWith('git/'); } - if (['/git-upload-pack', '/git-receive-pack'].includes(gitPath)) { + const expectedContentType = PACK_CONTENT_TYPES.get(gitPath); + if (expectedContentType) { if (!accept) { return false; } // https://www.git-scm.com/docs/http-protocol#_uploading_data - return agent.startsWith('git/') && accept.startsWith('application/x-git-'); + // The Content-Type must exactly match the operation named in the path so that + // the request cannot be classified differently from the operation it performs. + return ( + agent.startsWith('git/') && + accept.startsWith('application/x-git-') && + headers['content-type'] === expectedContentType + ); } return false; }; + +/** + * Determine the git operation type from the git path of a proxied request. + * @param {string | undefined} gitPath Path of the git operation (everything after .git) + * @return {'push' | 'pull' | 'default'} The action type for the request. + */ +export const getActionTypeFromGitPath = ( + gitPath: string | undefined, +): 'push' | 'pull' | 'default' => { + if (gitPath === '/git-receive-pack') { + return 'push'; + } + if (gitPath === '/git-upload-pack') { + return 'pull'; + } + return 'default'; +}; diff --git a/test/testParseAction.test.ts b/test/testParseAction.test.ts index 3c00dff34..abdeacbf8 100644 --- a/test/testParseAction.test.ts +++ b/test/testParseAction.test.ts @@ -98,4 +98,43 @@ describe('Pre-processor: parseAction', () => { expect(action.type).toBe('push'); expect(action.url).toBe('https://github.com/finos/git-proxy.git'); }); + + it('should classify a receive-pack request as a push regardless of content-type', async () => { + for (const contentType of [ + 'application/x-git-receive-pack-request;charset=utf-8', + 'application/x-git-upload-pack-request', + undefined, + ]) { + const req = { + originalUrl: '/github.com/finos/git-proxy.git/git-receive-pack', + method: 'POST', + headers: { 'content-type': contentType }, + } as unknown as Request; + + const action = await preprocessor.exec(req); + expect(action.type).toBe('push'); + } + }); + + it('should classify an upload-pack request as a pull regardless of content-type', async () => { + const req = { + originalUrl: '/github.com/finos/git-proxy.git/git-upload-pack', + method: 'POST', + headers: { 'content-type': 'application/x-git-receive-pack-request' }, + } as Request; + + const action = await preprocessor.exec(req); + expect(action.type).toBe('pull'); + }); + + it('should classify ref discovery requests as default', async () => { + const req = { + originalUrl: '/github.com/finos/git-proxy.git/info/refs?service=git-receive-pack', + method: 'GET', + headers: {}, + } as Request; + + const action = await preprocessor.exec(req); + expect(action.type).toBe('default'); + }); }); diff --git a/test/testProxyRoute.test.ts b/test/testProxyRoute.test.ts index 32f720015..2cf8497eb 100644 --- a/test/testProxyRoute.test.ts +++ b/test/testProxyRoute.test.ts @@ -431,6 +431,50 @@ describe('proxyFilter', () => { expect(result).toBe(false); expect(statusMock).toHaveBeenCalledWith(200); }); + + it.each([ + 'application/x-git-receive-pack-request;charset=utf-8', + 'APPLICATION/X-GIT-RECEIVE-PACK-REQUEST', + 'application/x-git-upload-pack-request', + ])( + 'should reject a POST to git-receive-pack with content-type %s without running the chain', + async (contentType) => { + const executeChainSpy = vi.spyOn(chain, 'executeChain'); + mockReq.method = 'POST'; + mockReq.url = '/github.com/finos/git-proxy.git/git-receive-pack'; + mockReq.headers = { + host: 'localhost:8080', + 'user-agent': 'git/2.30.0', + accept: 'application/x-git-receive-pack-result', + 'content-type': contentType, + }; + + const result = await proxyFilter?.(mockReq as Request, mockRes as Response); + + expect(result).toBe(false); + expect(executeChainSpy).not.toHaveBeenCalled(); + }, + ); + + it('should run the chain for a POST to git-receive-pack with the exact content-type', async () => { + const executeChainSpy = vi.spyOn(chain, 'executeChain').mockResolvedValue({ + error: false, + blocked: false, + } as Action); + mockReq.method = 'POST'; + mockReq.url = '/github.com/finos/git-proxy.git/git-receive-pack'; + mockReq.headers = { + host: 'localhost:8080', + 'user-agent': 'git/2.30.0', + accept: 'application/x-git-receive-pack-result', + 'content-type': 'application/x-git-receive-pack-request', + }; + + const result = await proxyFilter?.(mockReq as Request, mockRes as Response); + + expect(result).toBe(true); + expect(executeChainSpy).toHaveBeenCalledOnce(); + }); }); describe('Blocked requests', () => { @@ -665,6 +709,7 @@ describe('proxy route helpers', () => { const res = validGitRequest('/git-upload-pack', { 'user-agent': 'git/2.40.0', accept: 'application/x-git-upload-pack-request', + 'content-type': 'application/x-git-upload-pack-request', }); expect(res).toBe(true); }); diff --git a/test/testRouteFilter.test.ts b/test/testRouteFilter.test.ts index 159783ec9..50b5cf1b5 100644 --- a/test/testRouteFilter.test.ts +++ b/test/testRouteFilter.test.ts @@ -137,12 +137,7 @@ describe('url helpers and filter functions used in the proxy', () => { }); it('validGitRequest should return true for safe requests', () => { - [ - '/info/refs?service=git-upload-pack', - '/info/refs?service=git-receive-pack', - '/git-upload-pack', - '/git-receive-pack', - ].forEach((url) => { + ['/info/refs?service=git-upload-pack', '/info/refs?service=git-receive-pack'].forEach((url) => { expect( validGitRequest(url, { 'user-agent': 'git/2.30.0', @@ -150,6 +145,51 @@ describe('url helpers and filter functions used in the proxy', () => { }), ).toBe(true); }); + expect( + validGitRequest('/git-upload-pack', { + 'user-agent': 'git/2.30.0', + accept: 'application/x-git-upload-pack-result', + 'content-type': 'application/x-git-upload-pack-request', + }), + ).toBe(true); + expect( + validGitRequest('/git-receive-pack', { + 'user-agent': 'git/2.30.0', + accept: 'application/x-git-receive-pack-result', + 'content-type': 'application/x-git-receive-pack-request', + }), + ).toBe(true); + }); + + it('validGitRequest should return false for pack requests whose content-type does not match the operation', () => { + [ + undefined, + 'application/x-git-receive-pack-request;charset=utf-8', + 'Application/X-Git-Receive-Pack-Request', + 'application/x-git-upload-pack-request', + 'application/octet-stream', + ].forEach((contentType) => { + expect( + validGitRequest('/git-receive-pack', { + 'user-agent': 'git/2.30.0', + accept: 'application/x-git-receive-pack-result', + 'content-type': contentType, + }), + ).toBe(false); + }); + [ + undefined, + 'application/x-git-upload-pack-request;charset=utf-8', + 'application/x-git-receive-pack-request', + ].forEach((contentType) => { + expect( + validGitRequest('/git-upload-pack', { + 'user-agent': 'git/2.30.0', + accept: 'application/x-git-upload-pack-result', + 'content-type': contentType, + }), + ).toBe(false); + }); }); it('validGitRequest should return false for unsafe URLs', () => {