Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/proxy/processors/pre-processor/parseAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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://<git proxy domain>:<port>/<proxied domain>/<repoPath>
// 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}`);
Expand Down
34 changes: 32 additions & 2 deletions src/proxy/routes/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>([
['/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
Expand All @@ -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';
};
39 changes: 39 additions & 0 deletions test/testParseAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
45 changes: 45 additions & 0 deletions test/testProxyRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
Expand Down
52 changes: 46 additions & 6 deletions test/testRouteFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,59 @@ 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',
accept: 'application/x-git-upload-pack-request',
}),
).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', () => {
Expand Down
Loading