| 1 | import { OID_LENGTH, type RefTransition } from '../wal/wal.types.js'; |
| 2 | import { readHead, type |
| 3 | import { InvalidReceivePackRequestError } |
| 4 | import { FLUSH_PACKET } |
| 5 | |
| 6 | const PKT_LENGTH_CHARS = 4 |
| 7 | const OID_HEX_LENGTH = OID_LENGTH |
| 8 | const PACK_SIGNATURE = ' |
| 9 | |
| 10 | /** Git cannot rewind a chunked request to retry with credentials, so a large push is preceded by a throwaway POST of one flush packet. Answering it with an error kills the push before the body is sent. */ |
| 11 | export async function isProbeRequest |
| 12 | if (body.size |
| 13 | return ( |
| 14 | (await readHead(body |
| 15 | ); |
| 16 | } |
| 17 | |
| 18 | /** Most pushes fit the first window; an import pushing the head ref of every pull request in a big repository runs to megabytes. */ |
| 19 | const INITIAL_HEAD_BYTES = 64 |
| 20 | /** Each command is ~100 bytes, so this still admits ~600k refs while bounding what one request can make us buffer. */ |
| 21 | const MAX_COMMAND_SECTION_BYTES = 64 |
| 22 | |
| 23 | export interface ReceivePackRequest |
| 24 | transitions: RefTransition |
| 25 | capabilities: string[] |
| 26 | pack: Buffer |
| 27 | } |
| 28 | |
| 29 | export interface ReceivePackHeader |
| 30 | transitions: RefTransition |
| 31 | capabilities: string[] |
| 32 | packOffset: number; |
| 33 | packSize: number; |
| 34 | } |
| 35 | |
| 36 | /** Reads only the command section, leaving the packfile on disk. A 2 GiB push cannot be a Buffer - `Hash.update` and most Buffer operations cap at INT_MAX. */ |
| 37 | export async function readReceivePackHeader |
| 38 | body: GitRequestBody |
| 39 | ): Promise<ReceivePackHeader |
| 40 | let limit = |
| 41 | let head = |
| 42 | while ( |
| 43 | head.length < body |
| 44 | limit < MAX_COMMAND_SECTION_BYTES |
| 45 | !holdsCommandSection(head) |
| 46 | ) { |
| 47 | limit *= 2; |
| 48 | head = await |
| 49 | } |
| 50 | const { transitions, |
| 51 | |
| 52 | return { |
| 53 | transitions, |
| 54 | capabilities, |
| 55 | packOffset, |
| 56 | packSize: body.size |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | /** <pkt-line> "<old-oid> <new-oid> <ref>\0<capabilities>" <pkt-line> "<old-oid> <new-oid> <ref>" 0000 PACK... */ |
| 61 | export function parseReceivePackRequest(body |
| 62 | const { transitions, |
| 63 | const pack = |
| 64 | |
| 65 | if (pack.length |
| 66 | throw new InvalidReceivePackRequestError( |
| 67 | } |
| 68 | |
| 69 | return { transitions, |
| 70 | } |
| 71 | |
| 72 | /** Whether `head` reaches the flush packet; a malformed length counts as complete so the parser reports it. */ |
| 73 | function holdsCommandSection(head: |
| 74 | let offset = |
| 75 | while (offset + |
| 76 | const marker = |
| 77 | if (marker === |
| 78 | const length = |
| 79 | if (!Number.isInteger |
| 80 | offset += length; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | /** <pkt-line> "<old-oid> <new-oid> <ref>\0<capabilities>" <pkt-line> "<old-oid> <new-oid> <ref>" 0000 PACK... */ |
| 86 | function parseCommandSection(body: |
| 87 | const transitions: |
| 88 | let capabilities: |
| 89 | let offset = |
| 90 | |
| 91 | for (;;) { |
| 92 | if (offset + |
| 93 | throw new InvalidReceivePackRequestError( |
| 94 | 'truncated command section', |
| 95 | body, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | const marker = |
| 100 | if (marker === |
| 101 | offset += PKT_LENGTH_CHARS; |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | const length = |
| 106 | if (!Number.isInteger |
| 107 | throw new InvalidReceivePackRequestError( |
| 108 | `bad pkt-line length ${marker}`, |
| 109 | body, |
| 110 | ); |
| 111 | } |
| 112 | if (offset + |
| 113 | throw new InvalidReceivePackRequestError( |
| 114 | 'pkt-line runs past end of body', |
| 115 | body, |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | const payload = |
| 120 | 'utf8', |
| 121 | offset + PKT_LENGTH_CHARS, |
| 122 | offset + length, |
| 123 | ); |
| 124 | offset += length; |
| 125 | |
| 126 | const [command, |
| 127 | if (capabilityList !== |
| 128 | capabilities = capabilityList. |
| 129 | } |
| 130 | transitions.push(parseCommand |
| 131 | } |
| 132 | |
| 133 | if (transitions.length |
| 134 | throw new InvalidReceivePackRequestError( |
| 135 | } |
| 136 | |
| 137 | return { transitions, |
| 138 | } |
| 139 | |
| 140 | function parseCommand(command: |
| 141 | const [oldOid, |
| 142 | const ref = |
| 143 | |
| 144 | if (!isOid(oldOid |
| 145 | throw new InvalidReceivePackRequestError( |
| 146 | } |
| 147 | |
| 148 | return { |
| 149 | ref, |
| 150 | oldOid: Buffer.from |
| 151 | newOid: Buffer.from |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | function isOid(value: |
| 156 | return ( |
| 157 | value !== undefined && |
| 158 | value.length === OID_HEX_LENGTH |
| 159 | /^[0-9a-f]+$/. |
| 160 | ); |
| 161 | } |