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
4 changes: 4 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@
"required": ["windowMs", "limit"],
"additionalProperties": false
},
"maxPackSizeBytes": {
"type": "number",
"description": "Maximum size in bytes of a git-upload-pack or git-receive-pack request body that the proxy will buffer for inspection. Larger requests are rejected with HTTP 413 (default 104857600, i.e. 100 MiB)."
},
Comment on lines +264 to +267

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Pack-size setting missing from schema reference

Adding maxPackSizeBytes leaves the generated website reference unchanged, despite the repository's required schema documentation update. Readers cannot discover the configurable limit there.

Learn more

The website's schema reference is generated from the JSON schema. Contributing guidance requires regenerating it whenever a configuration property changes. The new property has no entry in the existing reference, leaving the public setting and its default undocumented there.

Example: An operator consulting the website schema reference after encountering HTTP 413 cannot find maxPackSizeBytes or its default value.

Recommended fix: Run npm run gen-schema-doc and commit the resulting update to the website reference with the schema change.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed this should be regenerated. npm run gen-schema-doc couldn't run in my environment: npm and pip registries are unreachable, so json-schema-for-humans isn't installed. Editing reference.mdx by hand would mean renumbering every section after rateLimit, which the generator should do instead. I'm leaving this thread open so the docs get regenerated before merge. It's flagged as an unchecked item in the PR checklist.

"privateOrganizations": {
"description": "Provider searches for listed private organizations are disabled, see commitConfig.diff.block.providers",
"type": "array"
Expand Down
7 changes: 7 additions & 0 deletions src/config/generated/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export interface GitProxyConfig {
* Provide custom URLs for the git proxy interfaces in case it cannot determine its own URL
*/
domains?: Domains;
/**
* Maximum size in bytes of a git-upload-pack or git-receive-pack request body that the
* proxy will buffer for inspection. Larger requests are rejected with HTTP 413 (default
* 104857600, i.e. 100 MiB).
*/
maxPackSizeBytes?: number;
/**
* List of plugins to integrate on GitProxy's push or pull actions. Each value is either a
* file path or a module name.
Expand Down Expand Up @@ -769,6 +775,7 @@ const typeMap: any = {
{ json: 'cookieSecret', js: 'cookieSecret', typ: u(undefined, '') },
{ json: 'csrfProtection', js: 'csrfProtection', typ: u(undefined, true) },
{ json: 'domains', js: 'domains', typ: u(undefined, r('Domains')) },
{ json: 'maxPackSizeBytes', js: 'maxPackSizeBytes', typ: u(undefined, 3.14) },
{ json: 'plugins', js: 'plugins', typ: u(undefined, a('')) },
{ json: 'privateOrganizations', js: 'privateOrganizations', typ: u(undefined, a('any')) },
{ json: 'proxyUrl', js: 'proxyUrl', typ: u(undefined, '') },
Expand Down
10 changes: 10 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ export const getRateLimit = () => {
return config.rateLimit;
};

export const DEFAULT_MAX_PACK_SIZE_BYTES = 100 * 1024 * 1024;

export const getMaxPackSizeBytes = (): number => {
const config = loadFullConfiguration();
const limit = config.maxPackSizeBytes;
return typeof limit === 'number' && Number.isFinite(limit) && limit > 0
? limit
: DEFAULT_MAX_PACK_SIZE_BYTES;
};

// Function to handle configuration updates
const handleConfigUpdate = async (newConfig: Configuration) => {
console.log('Configuration updated from external source');
Expand Down
75 changes: 59 additions & 16 deletions src/proxy/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import { PassThrough } from 'stream';
import getRawBody from 'raw-body';
import { executeChain } from '../chain';
import { exec as parseAction } from '../processors/pre-processor/parseAction';
import { exec as checkRepoInAuthorisedList } from '../processors/push-action/checkRepoInAuthorisedList';
import { exec as audit } from '../processors/post-processor/audit';
import { processUrlPath, validGitRequest } from './helper';
import { getAllProxiedHosts } from '../../db';
import { getMaxPackSizeBytes } from '../../config';
import { ProxyOptions } from 'express-http-proxy';
import { handleErrorAndLog } from '../../utils/errors';

Expand Down Expand Up @@ -162,41 +166,80 @@
req.method === 'POST' &&
/^(?:\/[^/]+)*\/[^/]+\.git\/(?:git-upload-pack|git-receive-pack)$/.test(req.url);

const rejectPackPost = (req: Request, res: Response, message: string): void => {
logAction(req.url, req.headers.host, req.headers['user-agent'], ActionType.ERROR, message);
sendErrorResponse(req, res, message);
};

const rejectTooLarge = (req: Request, res: Response, limit: number): void => {
const message = `Request body exceeds the maximum pack size of ${limit} bytes`;
logAction(req.url, req.headers.host, req.headers['user-agent'], ActionType.ERROR, message);
res.set('connection', 'close');
res.status(413).end(message);
};

const extractRawBody = async (req: Request, res: Response, next: NextFunction) => {
if (!isPackPost(req)) {
return next();
}

const proxyStream = new PassThrough({
highWaterMark: 4 * 1024 * 1024,
});
const pluginStream = new PassThrough({
highWaterMark: 4 * 1024 * 1024,
});
// run the cheap checks before reading any of the body into memory
const urlComponents = processUrlPath(req.url);
if (
!urlComponents ||
urlComponents.gitPath === undefined ||
!validGitRequest(urlComponents.gitPath, req.headers)
) {
return rejectPackPost(req, res, 'Invalid request received');
}

req.pipe(proxyStream);
req.pipe(pluginStream);
try {
const action = await checkRepoInAuthorisedList(req, await parseAction(req));
if (!action.continue()) {
// keep the audit record the action chain would have written, without reading the body
await audit(req, action);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟨 Unauthenticated denials exhaust audit storage

An unauthenticated client can send bodyless git-receive-pack requests for unknown repositories to trigger audit writes. Each request gets a timestamp ID, so repeated denials grow the persistent pushes store without a body-size cost.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior exists, but this PR doesn't introduce it. On main, a bodyless git-receive-pack POST to an unknown repo already went through executeChain, failed checkRepoInAuthorisedList, and was written by audit with a timestamp ID. This PR keeps that audit record, which the earlier review in this PR asked for. It just no longer buffers the body first.

The two possible changes pull in opposite directions:

  • Stop auditing rejected pushes: closes the storage path but loses the audit trail.
  • Rate-limit the proxy router: rateLimit currently applies only to the management service in src/service/index.ts, not the proxy app. Adding it would be a behavior change for every Git client.

Both need a maintainer decision, so I'm leaving behavior unchanged here and flagging it for whoever owns the policy.

return rejectPackPost(
req,
res,
action.errorMessage ?? 'Rejecting repo not in the authorised whitelist',
);
}
} catch (error: unknown) {
const message = handleErrorAndLog(error, 'Error checking repository authorisation');
return rejectPackPost(req, res, message);
}

const limit = getMaxPackSizeBytes();
const contentLength = Number(req.headers['content-length']);
if (Number.isFinite(contentLength) && contentLength > limit) {
return rejectTooLarge(req, res, limit);
}

try {
const buf = await getRawBody(pluginStream, { limit: '1gb' });
// read straight from the request so backpressure only depends on this reader; the
// buffered body is replayed to the upstream proxy once the plugins have inspected it
const buf = await getRawBody(req, { limit });
req.bodyRaw = buf;
req.pipe = (dest, opts) => proxyStream.pipe(dest, opts);
req.pipe = (dest, opts) => {
const replay = new PassThrough();
replay.end(buf);
return replay.pipe(dest, opts);
};
next();
} catch (error: unknown) {
if (error instanceof Error) {
console.error(error.message);
proxyStream.destroy(error);
} else {
console.error(String(error));
proxyStream.destroy(new Error(String(error)));
const err = error instanceof Error ? error : new Error(String(error));
console.error(err.message);
if ((err as { type?: string }).type === 'entity.too.large') {
return rejectTooLarge(req, res, limit);
}
res.set('connection', 'close');
res.status(500).end('Proxy error');
}
};

const getRouter = async () => {
const router = Router();
router.use(extractRawBody);

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

const originsToProxy = await getAllProxiedHosts();
const proxyKeys: string[] = [];
Expand Down
121 changes: 121 additions & 0 deletions test/extractRawBody.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,20 @@ vi.mock('../src/proxy/chain', () => ({
executeChain: vi.fn(),
}));

vi.mock('../src/db', () => ({
getRepoByUrl: vi.fn(),
writeAudit: vi.fn(),
getAllProxiedHosts: vi.fn().mockResolvedValue([]),
}));

vi.mock('../src/config', () => ({
getMaxPackSizeBytes: vi.fn().mockReturnValue(1024),
}));

// Now import the module-under-test, which will receive the mocked deps
import { extractRawBody, isPackPost } from '../src/proxy/routes';
import * as chain from '../src/proxy/chain';
import * as db from '../src/db';

describe('extractRawBody middleware', () => {
let req: any;
Expand All @@ -40,10 +51,21 @@ describe('extractRawBody middleware', () => {
beforeEach(() => {
(rawBody as Mock).mockClear();
(chain.executeChain as Mock).mockClear();
(db.getRepoByUrl as Mock).mockReset();
(db.writeAudit as Mock).mockClear();
(db.getRepoByUrl as Mock).mockImplementation(async (url: string) =>
url === 'https://github.com/proj/foo.git' ? { url } : null,
);

req = new PassThrough();
req.method = 'POST';
req.url = '/proj/foo.git/git-upload-pack';
req.originalUrl = req.url;
req.headers = {
'user-agent': 'git/2.43.0',
accept: 'application/x-git-upload-pack-result',
};
req.pipe = vi.fn(PassThrough.prototype.pipe);

res = {
set: vi.fn().mockReturnThis(),
Expand Down Expand Up @@ -74,6 +96,105 @@ describe('extractRawBody middleware', () => {
expect(req.bodyRaw).toBeDefined();
expect(typeof req.pipe).toBe('function');
});

it('replays the buffered body when the request is piped to the upstream proxy', async () => {
req.end();
await extractRawBody(req, res, next);

const upstream = new PassThrough();
const chunks: Buffer[] = [];
upstream.on('data', (chunk: Buffer) => chunks.push(chunk));
await new Promise<void>((resolve) => {
upstream.on('end', resolve);
req.pipe(upstream);
});

expect(Buffer.concat(chunks).toString()).toBe('payload');
});

it('buffers with the configured pack size limit', async () => {
req.end();
await extractRawBody(req, res, next);
expect((rawBody as Mock).mock.calls[0][1]).toEqual({ limit: 1024 });
});

it('rejects a pack POST with a non-git User-Agent before reading the body', async () => {
req.headers['user-agent'] = 'curl/8.0';

await extractRawBody(req, res, next);

expect(rawBody).not.toHaveBeenCalled();
expect(req.pipe).not.toHaveBeenCalled();
expect(next).not.toHaveBeenCalled();
expect(res.set).toHaveBeenCalledWith('connection', 'close');
expect(res.send).toHaveBeenCalledWith(expect.stringContaining('Invalid request received'));
});

it('rejects a pack POST without a git Accept header before reading the body', async () => {
delete req.headers.accept;

await extractRawBody(req, res, next);

expect(rawBody).not.toHaveBeenCalled();
expect(req.pipe).not.toHaveBeenCalled();
expect(next).not.toHaveBeenCalled();
});

it('rejects and audits a push to an unregistered repository before reading the body', async () => {
req.url = req.originalUrl = '/github.com/evil/unknown.git/git-receive-pack';
req.headers.accept = 'application/x-git-receive-pack-result';
req.headers['content-type'] = 'application/x-git-receive-pack-request';

await extractRawBody(req, res, next);

expect(db.getRepoByUrl).toHaveBeenCalledWith('https://github.com/evil/unknown.git');
expect(rawBody).not.toHaveBeenCalled();
expect(req.pipe).not.toHaveBeenCalled();
expect(next).not.toHaveBeenCalled();
expect(res.send).toHaveBeenCalledWith(expect.stringContaining('not in the authorised'));
expect(db.writeAudit).toHaveBeenCalledOnce();
const audited = (db.writeAudit as Mock).mock.calls[0][0];
expect(audited.type).toBe('push');
expect(audited.error).toBe(true);
expect(audited.url).toContain('/evil/unknown.git');
});

it('rejects a pull from an unregistered repository without auditing it', async () => {
req.url = req.originalUrl = '/github.com/evil/unknown.git/git-upload-pack';
req.headers['content-type'] = 'application/x-git-upload-pack-request';

await extractRawBody(req, res, next);

expect(rawBody).not.toHaveBeenCalled();
expect(next).not.toHaveBeenCalled();
expect(res.send).toHaveBeenCalledWith(expect.stringContaining('not in the authorised'));
expect(db.writeAudit).not.toHaveBeenCalled();
});

it('rejects with 413 when Content-Length exceeds the limit before reading the body', async () => {
req.headers['content-length'] = '1025';

await extractRawBody(req, res, next);

expect(rawBody).not.toHaveBeenCalled();
expect(req.pipe).not.toHaveBeenCalled();
expect(next).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(413);
});

it('rejects with 413 when the streamed body exceeds the limit', async () => {
const tooLarge = Object.assign(new Error('request entity too large'), {
type: 'entity.too.large',
});
(rawBody as Mock).mockRejectedValueOnce(tooLarge);
req.end();

await extractRawBody(req, res, next);

expect(next).not.toHaveBeenCalled();
expect(req.bodyRaw).toBeUndefined();
expect(res.status).toHaveBeenCalledWith(413);
});
});

describe('isPackPost()', () => {
Expand Down
25 changes: 25 additions & 0 deletions test/testProxyRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,31 @@ describe('proxy express application', () => {
expect(res2.text).toContain('Rejecting repo');
}, 5000);

it('should reject a pack POST with a non-git User-Agent without forwarding it', async () => {
const res = await request(proxy.getExpressApp()!)
.post(`${TEST_DEFAULT_REPO.proxyUrlPrefix}/git-receive-pack`)
.set('user-agent', 'curl/8.0')
.set('accept', 'application/x-git-receive-pack-result')
.set('content-type', 'application/x-git-receive-pack-request')
.send(Buffer.alloc(64 * 1024));

expect(res.status).toBe(200);
expect(res.headers.connection).toBe('close');
expect(res.text).toContain('Invalid request received');
});

it('should reject a pack POST for an unregistered repository', async () => {
const res = await request(proxy.getExpressApp()!)
.post(`${TEST_UNKNOWN_REPO.proxyUrlPrefix}/git-receive-pack`)
.set('user-agent', 'git/2.42.0')
.set('accept', 'application/x-git-receive-pack-result')
.set('content-type', 'application/x-git-receive-pack-request')
.send(Buffer.alloc(64 * 1024));

expect(res.status).toBe(200);
expect(res.text).toContain('Rejecting repo');
});

it('should create the default repo if it does not exist', async function () {
// Remove the default repo from the db and check it no longer exists
await cleanupRepo(TEST_DEFAULT_REPO.url);
Expand Down
Loading