| 1 | import { OID_LENGTH, type RefTransition } from '../wal/wal.types.js'; |
| 2 | import { readHead, type |
| 3 | import { InvalidReceivePackRequestError } |
| 4 | |
| 5 | const PKT_LENGTH_CHARS = 4 |
| 6 | const FLUSH_PACKET = ' |
| 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 | /** A push touching thousands of refs still keeps its command section well inside this. */ |
| 19 | const MAX_COMMAND_SECTION_BYTES = 1024 |
| 20 | |
| 21 | export interface ReceivePackRequest |
| 22 | transitions: RefTransition |
| 23 | capabilities: string[] |
| 24 | pack: Buffer |
| 25 | } |
| 26 | |
| 27 | export interface ReceivePackHeader |
| 28 | transitions: RefTransition |
| 29 | capabilities: string[] |
| 30 | packOffset: number; |
| 31 | packSize: number; |
| 32 | } |
| 33 | |
| 34 | /** 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. */ |
| 35 | export async function readReceivePackHeader |
| 36 | body: GitRequestBody |
| 37 | ): Promise<ReceivePackHeader |
| 38 | const head = |
| 39 | const { transitions, |
| 40 | |
| 41 | return { |
| 42 | transitions, |
| 43 | capabilities, |
| 44 | packOffset, |
| 45 | packSize: body.size |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Splits a receive-pack body into its command section and packfile. |
| 51 | * |
| 52 | * <pkt-line> "<old-oid> <new-oid> <ref>\0<capabilities>" <pkt-line> "<old-oid> <new-oid> <ref>" 0000 PACK... |
| 53 | */ |
| 54 | export function parseReceivePackRequest(body |
| 55 | const { transitions, |
| 56 | const pack = |
| 57 | |
| 58 | if (pack.length |
| 59 | throw new InvalidReceivePackRequestError( |
| 60 | } |
| 61 | |
| 62 | return { transitions, |
| 63 | } |
| 64 | |
| 65 | /** <pkt-line> "<old-oid> <new-oid> <ref>\0<capabilities>" <pkt-line> "<old-oid> <new-oid> <ref>" 0000 PACK... */ |
| 66 | function parseCommandSection(body: |
| 67 | const transitions: |
| 68 | let capabilities: |
| 69 | let offset = |
| 70 | |
| 71 | for (;;) { |
| 72 | if (offset + |
| 73 | throw new InvalidReceivePackRequestError( |
| 74 | 'truncated command section', |
| 75 | body, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | const marker = |
| 80 | if (marker === |
| 81 | offset += PKT_LENGTH_CHARS; |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | const length = |
| 86 | if (!Number.isInteger |
| 87 | throw new InvalidReceivePackRequestError( |
| 88 | `bad pkt-line length ${marker}`, |
| 89 | body, |
| 90 | ); |
| 91 | } |
| 92 | if (offset + |
| 93 | throw new InvalidReceivePackRequestError( |
| 94 | 'pkt-line runs past end of body', |
| 95 | body, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | const payload = |
| 100 | 'utf8', |
| 101 | offset + PKT_LENGTH_CHARS, |
| 102 | offset + length, |
| 103 | ); |
| 104 | offset += length; |
| 105 | |
| 106 | const [command, |
| 107 | if (capabilityList !== |
| 108 | capabilities = capabilityList. |
| 109 | } |
| 110 | transitions.push(parseCommand |
| 111 | } |
| 112 | |
| 113 | if (transitions.length |
| 114 | throw new InvalidReceivePackRequestError( |
| 115 | } |
| 116 | |
| 117 | return { transitions, |
| 118 | } |
| 119 | |
| 120 | function parseCommand(command: |
| 121 | const [oldOid, |
| 122 | const ref = |
| 123 | |
| 124 | if (!isOid(oldOid |
| 125 | throw new InvalidReceivePackRequestError( |
| 126 | } |
| 127 | |
| 128 | return { |
| 129 | ref, |
| 130 | oldOid: Buffer.from |
| 131 | newOid: Buffer.from |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | function isOid(value: |
| 136 | return ( |
| 137 | value !== undefined && |
| 138 | value.length === OID_HEX_LENGTH |
| 139 | /^[0-9a-f]+$/. |
| 140 | ); |
| 141 | } |