@@ -4,6 +4,13 @@ import fs from 'fs';
4
4
import path from 'path' ;
5
5
import lod from 'lodash' ;
6
6
import { CommitContent } from '../types' ;
7
+ import {
8
+ BRANCH_PREFIX ,
9
+ EMPTY_COMMIT_HASH ,
10
+ PACK_SIGNATURE ,
11
+ PACKET_SIZE ,
12
+ } from '../constants' ;
13
+
7
14
const BitMask = require ( 'bit-mask' ) as any ;
8
15
9
16
const dir = path . resolve ( __dirname , './.tmp' ) ;
@@ -29,8 +36,7 @@ async function exec(req: any, action: Action): Promise<Action> {
29
36
return action ;
30
37
}
31
38
const [ packetLines , packDataOffset ] = parsePacketLines ( req . body ) ;
32
-
33
- const refUpdates = packetLines . filter ( ( line ) => line . includes ( 'refs/heads/' ) ) ;
39
+ const refUpdates = packetLines . filter ( ( line ) => line . includes ( BRANCH_PREFIX ) ) ;
34
40
35
41
if ( refUpdates . length !== 1 ) {
36
42
step . log ( 'Invalid number of branch updates.' ) ;
@@ -57,7 +63,7 @@ async function exec(req: any, action: Action): Promise<Action> {
57
63
const buf = req . body . slice ( packDataOffset ) ;
58
64
59
65
// Verify that data actually starts with PACK signature
60
- if ( buf . length < 4 || buf . toString ( 'utf8' , 0 , 4 ) !== 'PACK' ) {
66
+ if ( buf . length < PACKET_SIZE || buf . toString ( 'utf8' , 0 , PACKET_SIZE ) !== PACK_SIGNATURE ) {
61
67
step . log ( `Expected PACK signature at offset ${ packDataOffset } , but found something else.` ) ;
62
68
step . setError ( 'Your push has been blocked. Invalid PACK data structure.' ) ;
63
69
action . addStep ( step ) ;
@@ -68,9 +74,14 @@ async function exec(req: any, action: Action): Promise<Action> {
68
74
const contents = getContents ( contentBuff as any , meta . entries as number ) ;
69
75
70
76
action . commitData = getCommitData ( contents as any ) ;
71
-
72
- if ( action . commitFrom === '0000000000000000000000000000000000000000' ) {
73
- action . commitFrom = action . commitData [ action . commitData . length - 1 ] . parent ;
77
+ if ( action . commitData . length === 0 ) {
78
+ step . log ( 'No commit data found when parsing push.' )
79
+ } else {
80
+ if ( action . commitFrom === EMPTY_COMMIT_HASH ) {
81
+ action . commitFrom = action . commitData [ action . commitData . length - 1 ] . parent ;
82
+ }
83
+ const user = action . commitData [ action . commitData . length - 1 ] . committer ;
84
+ action . user = user ;
74
85
}
75
86
76
87
const user = action . commitData [ action . commitData . length - 1 ] . committer ;
@@ -180,7 +191,7 @@ const getCommitData = (contents: CommitContent[]) => {
180
191
181
192
const { tree, parents, authorInfo, committerInfo } = getParsedData ( headerLines ) ;
182
193
// No parent headers -> zero hash
183
- const parent = parents . length > 0 ? parents [ 0 ] : '0000000000000000000000000000000000000000' ;
194
+ const parent = parents . length > 0 ? parents [ 0 ] : EMPTY_COMMIT_HASH ;
184
195
185
196
// Validation for required attributes
186
197
if ( ! tree || ! authorInfo || ! committerInfo ) {
@@ -210,17 +221,17 @@ const getCommitData = (contents: CommitContent[]) => {
210
221
* @return {Array } An array containing the metadata and the remaining buffer.
211
222
*/
212
223
const getPackMeta = ( buffer : Buffer ) => {
213
- const sig = buffer . slice ( 0 , 4 ) . toString ( 'utf-8' ) ;
214
- const version = buffer . readUIntBE ( 4 , 4 ) ;
215
- const entries = buffer . readUIntBE ( 8 , 4 ) ;
224
+ const sig = buffer . slice ( 0 , PACKET_SIZE ) . toString ( 'utf-8' ) ;
225
+ const version = buffer . readUIntBE ( PACKET_SIZE , PACKET_SIZE ) ;
226
+ const entries = buffer . readUIntBE ( PACKET_SIZE * 2 , PACKET_SIZE ) ;
216
227
217
228
const meta = {
218
229
sig : sig ,
219
230
version : version ,
220
231
entries : entries ,
221
232
} ;
222
233
223
- return [ meta , buffer . slice ( 12 ) ] ;
234
+ return [ meta , buffer . slice ( PACKET_SIZE * 3 ) ] ;
224
235
} ;
225
236
226
237
/**
@@ -358,8 +369,8 @@ const parsePacketLines = (buffer: Buffer): [string[], number] => {
358
369
const lines : string [ ] = [ ] ;
359
370
let offset = 0 ;
360
371
361
- while ( offset + 4 <= buffer . length ) {
362
- const lengthHex = buffer . toString ( 'utf8' , offset , offset + 4 ) ;
372
+ while ( offset + PACKET_SIZE <= buffer . length ) {
373
+ const lengthHex = buffer . toString ( 'utf8' , offset , offset + PACKET_SIZE ) ;
363
374
const length = Number ( `0x${ lengthHex } ` ) ;
364
375
365
376
// Prevent non-hex characters from causing issues
@@ -369,7 +380,7 @@ const parsePacketLines = (buffer: Buffer): [string[], number] => {
369
380
370
381
// length of 0 indicates flush packet (0000)
371
382
if ( length === 0 ) {
372
- offset += 4 ; // Include length of the flush packet
383
+ offset += PACKET_SIZE ; // Include length of the flush packet
373
384
break ;
374
385
}
375
386
@@ -378,7 +389,7 @@ const parsePacketLines = (buffer: Buffer): [string[], number] => {
378
389
throw new Error ( `Invalid packet line length ${ lengthHex } at offset ${ offset } ` ) ;
379
390
}
380
391
381
- const line = buffer . toString ( 'utf8' , offset + 4 , offset + length ) ;
392
+ const line = buffer . toString ( 'utf8' , offset + PACKET_SIZE , offset + length ) ;
382
393
lines . push ( line ) ;
383
394
offset += length ; // Move offset to the start of the next line's length prefix
384
395
}
0 commit comments