Skip to content

Commit 38915e7

Browse files
committed
fix: commit ref parsing and error handling
1 parent 1929df2 commit 38915e7

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/proxy/processors/push-action/parsePush.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,22 @@ async function exec(req: any, action: Action): Promise<Action> {
2222
action.addStep(step);
2323
return action;
2424
}
25-
const messageParts = req.body.toString('utf8').split(' ');
25+
const packetLines = parsePacketLines(req.body);
26+
const refUpdates = packetLines.filter((line) => line.includes('refs/heads/'));
2627

27-
action.branch = messageParts[2].trim().replace('\u0000', '');
28-
action.setCommit(messageParts[0].substr(4), messageParts[1]);
28+
if (refUpdates.length !== 1) {
29+
step.log('Invalid number of branch updates.');
30+
step.log(`Expected 1, but got ${refUpdates.length}`);
31+
step.setError('Your push has been blocked. Please make sure you are pushing to a single branch.');
32+
action.addStep(step);
33+
return action;
34+
}
35+
36+
const parts = refUpdates[0].split(' ');
37+
const [oldCommit, newCommit, ref] = parts;
38+
39+
action.branch = ref.replace(/\0.*/, '').trim();
40+
action.setCommit(oldCommit, newCommit);
2941

3042
const index = req.body.lastIndexOf('PACK');
3143
const buf = req.body.slice(index);
@@ -257,6 +269,26 @@ const unpack = (buf: Buffer) => {
257269
return [inflated.toString('utf8'), deflated.length];
258270
};
259271

272+
const parsePacketLines = (buffer: Buffer): string[] => {
273+
const lines: string[] = [];
274+
let offset = 0;
275+
276+
while (offset + 4 <= buffer.length) {
277+
const lengthHex = buffer.toString('utf8', offset, offset + 4);
278+
const length = parseInt(lengthHex, 16);
279+
280+
if (length === 0) {
281+
// Flush packet ("0000") marks the end of the ref section
282+
break;
283+
}
284+
285+
const line = buffer.toString('utf8', offset + 4, offset + length);
286+
lines.push(line);
287+
offset += length;
288+
}
289+
return lines;
290+
}
291+
260292
exec.displayName = 'parsePush.exec';
261293

262294
export {

0 commit comments

Comments
 (0)