interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / patch / p3gate.om
| 1 | note patch/p3gate.om: the upload form in fbig (server/main.om freqbig), the write gate, the text |
| 2 | note answers, and the field decoders. |
| 3 | note ---- text answers (kind 1, text/plain) |
| 4 | fn patext to void |
| 5 | put fobl 0 0 |
| 6 | put fkind 0 1 |
| 7 | fsetct tpctxt |
| 8 | end |
| 9 | note parefu: "refused: <reason>" with status st |
| 10 | fn parefu st ulong t view uchar * to long |
| 11 | patext |
| 12 | fput tprefu |
| 13 | fput t |
| 14 | fputc 10 |
| 15 | ret long st |
| 16 | end |
| 17 | note ---- the form: name=value pairs joined by &, each of the seven names once, nothing else |
| 18 | fn pafis s ulong e ulong t view uchar * to bool |
| 19 | if ne sub e s len t |
| 20 | ret false |
| 21 | end |
| 22 | for i ulong len t bound 16 |
| 23 | if ne get fbig add s i get t i |
| 24 | ret false |
| 25 | end |
| 26 | end |
| 27 | ret true |
| 28 | end |
| 29 | note pafidx: the index of the field name fbig[s, e), 9 when it is none of the seven |
| 30 | fn pafidx s ulong e ulong to ulong |
| 31 | if pafis s e tpfcsrf |
| 32 | ret 0 |
| 33 | end |
| 34 | if pafis s e tpfrepo |
| 35 | ret 1 |
| 36 | end |
| 37 | if pafis s e tpfbase |
| 38 | ret 2 |
| 39 | end |
| 40 | if pafis s e tpfttl |
| 41 | ret 3 |
| 42 | end |
| 43 | if pafis s e tpfdsc |
| 44 | ret 4 |
| 45 | end |
| 46 | if pafis s e tpfmbx |
| 47 | ret 5 |
| 48 | end |
| 49 | if pafis s e tpfsig |
| 50 | ret 6 |
| 51 | end |
| 52 | ret 9 |
| 53 | end |
| 54 | note pafend s n c: the first index in [s, n) holding c or &, else n |
| 55 | fn pafend s ulong n ulong c uchar to ulong |
| 56 | j = s |
| 57 | while lt j n bound 4194304 |
| 58 | b = get fbig j |
| 59 | if or eq b c eq b 38 |
| 60 | ret j |
| 61 | end |
| 62 | j = add j 1 |
| 63 | end |
| 64 | ret n |
| 65 | end |
| 66 | note paparse: split fbig into the fields; false for a pair without =, an unknown name or a name twice |
| 67 | fn paparse to bool |
| 68 | for k ulong 8 |
| 69 | put gpfo k 0 |
| 70 | put gpfn k 0 |
| 71 | put gpfs k 0 |
| 72 | end |
| 73 | n = get fbigl 0 |
| 74 | s = ulong 0 |
| 75 | for it ulong 16 |
| 76 | if ge s n |
| 77 | ret true |
| 78 | end |
| 79 | e = pafend s n 61 |
| 80 | if ge e n |
| 81 | ret false |
| 82 | end |
| 83 | if ne get fbig e 61 |
| 84 | ret false |
| 85 | end |
| 86 | a = pafend add e 1 n 38 |
| 87 | k = pafidx s e |
| 88 | if eq k 9 |
| 89 | ret false |
| 90 | end |
| 91 | if ne get gpfs k 0 |
| 92 | ret false |
| 93 | end |
| 94 | put gpfo k add e 1 |
| 95 | put gpfn k sub a add e 1 |
| 96 | put gpfs k 1 |
| 97 | s = add a 1 |
| 98 | end |
| 99 | ret false |
| 100 | end |
| 101 | note pafall: all seven fields are present |
| 102 | fn pafall to bool |
| 103 | for k ulong 7 |
| 104 | if eq get gpfs k 0 |
| 105 | ret false |
| 106 | end |
| 107 | end |
| 108 | ret true |
| 109 | end |
| 110 | note padec k cap: field k percent-decoded (+ is a space) into gpfv (65536 bytes); |
| 111 | note the length, or -1 when an escape is malformed or the value is longer than cap (cap <= 65536) |
| 112 | fn padec k ulong cap ulong to long |
| 113 | o = get gpfo k |
| 114 | n = get gpfn k |
| 115 | j = ulong 0 |
| 116 | m = ulong 0 |
| 117 | while lt j n bound 4194304 |
| 118 | c = get fbig add o j |
| 119 | j = add j 1 |
| 120 | if eq c 43 |
| 121 | c = uchar 32 |
| 122 | end |
| 123 | if eq c 37 |
| 124 | if gt add j 2 n |
| 125 | ret -1 |
| 126 | end |
| 127 | h = fhexv get fbig add o j |
| 128 | l = fhexv get fbig add add o j 1 |
| 129 | if or lt h 0 lt l 0 |
| 130 | ret -1 |
| 131 | end |
| 132 | c = uchar add mul h 16 l |
| 133 | j = add j 2 |
| 134 | end |
| 135 | if ge m cap |
| 136 | ret -1 |
| 137 | end |
| 138 | put gpfv m c |
| 139 | m = add m 1 |
| 140 | end |
| 141 | ret long m |
| 142 | end |
| 143 | note ---- the write gate: the gate of every forge write (social/40auth.om sowpre: CSRF token 403, |
| 144 | note store off 503, 30 writes a minute an address and a key 429), with two differences an uploading |
| 145 | note program needs: signed out is 403 here, not a redirect to the sign-in page, and every answer is |
| 146 | note one line of text. sowpre reads the token from the form body fbody, which is empty on this route |
| 147 | note (the body is in fbig), so the csrf field is handed to it there. |
| 148 | fn pacsrf to bool |
| 149 | if eq get gpfs 0 0 |
| 150 | ret false |
| 151 | end |
| 152 | if ne get gpfn 0 32 |
| 153 | ret false |
| 154 | end |
| 155 | o = get gpfo 0 |
| 156 | for i ulong len tpcsrfe |
| 157 | put fbody i get tpcsrfe i |
| 158 | end |
| 159 | for i ulong 32 |
| 160 | put fbody add len tpcsrfe i get fbig add o i |
| 161 | end |
| 162 | put fbodyl 0 add len tpcsrfe 32 |
| 163 | ret true |
| 164 | end |
| 165 | fn pagate to long |
| 166 | if eq get gscur 0 0 |
| 167 | ret parefu 403 tpm403 |
| 168 | end |
| 169 | if not paparse |
| 170 | ret parefu 400 tpmform |
| 171 | end |
| 172 | if not pacsrf |
| 173 | ret parefu 403 tpmcsrf |
| 174 | end |
| 175 | r = sowpre |
| 176 | put fbodyl 0 0 |
| 177 | if eq r 0 |
| 178 | soratev get gscur 0 get gsnow 0 |
| 179 | ret 0 |
| 180 | end |
| 181 | if eq r 429 |
| 182 | ret parefu 429 tpmrate |
| 183 | end |
| 184 | if eq r 503 |
| 185 | ret parefu 503 tpmoff |
| 186 | end |
| 187 | ret parefu 403 tpmcsrf |
| 188 | end |
| 189 | note paunl: POST /<owner>/<x>/patches/new where x is not a public repository (server/route.om asks |
| 190 | note before its 404): the gate first, then 404 in text. 0 = not that request. |
| 191 | fn paunl to long |
| 192 | if ne get fmeth 0 3 |
| 193 | ret 0 |
| 194 | end |
| 195 | if not frsteq tppnew |
| 196 | ret 0 |
| 197 | end |
| 198 | r = pagate |
| 199 | if ne r 0 |
| 200 | ret r |
| 201 | end |
| 202 | ret parefu 404 tpmnorp |
| 203 | end |
| 204 | note ---- field checks (after the gate) |
| 205 | note parepok: the repo field is exactly <owner>/<name> of the request path |
| 206 | fn parepok to bool |
| 207 | n = padec 1 256 |
| 208 | if lt n 0 |
| 209 | ret false |
| 210 | end |
| 211 | i = get fridx 0 |
| 212 | l = get fanl i |
| 213 | w = len tfowner |
| 214 | if ne ulong n add add w 1 l |
| 215 | ret false |
| 216 | end |
| 217 | for k ulong w bound 64 |
| 218 | if ne get gpfv k get tfowner k |
| 219 | ret false |
| 220 | end |
| 221 | end |
| 222 | if ne get gpfv w 47 |
| 223 | ret false |
| 224 | end |
| 225 | for k ulong l bound 128 |
| 226 | if ne get gpfv add add w 1 k frnbyte i k |
| 227 | ret false |
| 228 | end |
| 229 | end |
| 230 | ret true |
| 231 | end |
| 232 | note pabasef: the base field is 40 lowercase hex digits; copied into gpbase |
| 233 | fn pabasef to bool |
| 234 | if ne get gpfn 2 40 |
| 235 | ret false |
| 236 | end |
| 237 | o = get gpfo 2 |
| 238 | for i ulong 40 |
| 239 | c = get fbig add o i |
| 240 | if lt sohexv c 0 |
| 241 | ret false |
| 242 | end |
| 243 | put gpbase i c |
| 244 | end |
| 245 | ret true |
| 246 | end |
| 247 | note patitle: 1..256 bytes, UTF-8, one line, no control byte (the text rules of docs/collab-write.md |
| 248 | note section 2, checked and never rewritten: the bytes are what the author's key signed) |
| 249 | fn patitle to bool |
| 250 | n = padec 3 256 |
| 251 | if lt n 1 |
| 252 | ret false |
| 253 | end |
| 254 | if not pawutf8 ulong n false |
| 255 | ret false |
| 256 | end |
| 257 | for i ulong ulong n bound 256 |
| 258 | put gpttl i get gpfv i |
| 259 | end |
| 260 | put gpttll 0 ulong n |
| 261 | ret true |
| 262 | end |
| 263 | note padescr: 0..60000 bytes, UTF-8, tab and line feed the only control bytes |
| 264 | fn padescr to bool |
| 265 | n = padec 4 60000 |
| 266 | if lt n 0 |
| 267 | ret false |
| 268 | end |
| 269 | if not pawutf8 ulong n true |
| 270 | ret false |
| 271 | end |
| 272 | for i ulong ulong n bound 60000 |
| 273 | put gpdsc i get gpfv i |
| 274 | end |
| 275 | put gpdscl 0 ulong n |
| 276 | ret true |
| 277 | end |
| 278 | note ---- the mbox field: base64url without padding, canonical (unused bits zero) |
| 279 | fn pab64v c uchar to long |
| 280 | if and ge c 65 le c 90 |
| 281 | ret long sub c 65 |
| 282 | end |
| 283 | if and ge c 97 le c 122 |
| 284 | ret long sub c 71 |
| 285 | end |
| 286 | if and ge c 48 le c 57 |
| 287 | ret long add c 4 |
| 288 | end |
| 289 | if eq c 45 |
| 290 | ret 62 |
| 291 | end |
| 292 | if eq c 95 |
| 293 | ret 63 |
| 294 | end |
| 295 | ret -1 |
| 296 | end |
| 297 | note pabq p k: the k digits at fbig[p] as one number, -1 when one is not a base64url digit |
| 298 | fn pabq p ulong k ulong to long |
| 299 | v = long 0 |
| 300 | for i ulong k bound 4 |
| 301 | d = pab64v get fbig add p i |
| 302 | if lt d 0 |
| 303 | ret -1 |
| 304 | end |
| 305 | v = add mul v 64 d |
| 306 | end |
| 307 | ret v |
| 308 | end |
| 309 | note pambtail: the last 2 or 3 digits (1 or 2 bytes) at fbig[p], written at gpmb[q]; false when bad |
| 310 | fn pambtail p ulong k ulong q ulong to bool |
| 311 | v = pabq p k |
| 312 | if lt v 0 |
| 313 | ret false |
| 314 | end |
| 315 | if eq k 2 |
| 316 | if ne and v 15 0 |
| 317 | ret false |
| 318 | end |
| 319 | put gpmb q uchar shr v 4 |
| 320 | ret true |
| 321 | end |
| 322 | if ne and v 3 0 |
| 323 | ret false |
| 324 | end |
| 325 | put gpmb q uchar shr v 10 |
| 326 | put gpmb add q 1 uchar and shr v 2 255 |
| 327 | ret true |
| 328 | end |
| 329 | note pambdec: decode the mbox field into gpmb: 0, 413 over 2 MiB, 400 not base64url |
| 330 | fn pambdec to ulong |
| 331 | o = get gpfo 5 |
| 332 | n = get gpfn 5 |
| 333 | put gpmbl 0 0 |
| 334 | if gt n 2796203 |
| 335 | ret 413 |
| 336 | end |
| 337 | k = rem n 4 |
| 338 | if eq k 1 |
| 339 | ret 400 |
| 340 | end |
| 341 | q = ulong 0 |
| 342 | for t ulong div n 4 bound 1048576 |
| 343 | v = pabq add o mul t 4 4 |
| 344 | if lt v 0 |
| 345 | ret 400 |
| 346 | end |
| 347 | put gpmb q uchar shr v 16 |
| 348 | put gpmb add q 1 uchar and shr v 8 255 |
| 349 | put gpmb add q 2 uchar and v 255 |
| 350 | q = add q 3 |
| 351 | end |
| 352 | if ne k 0 |
| 353 | if not pambtail add o sub n k k q |
| 354 | ret 400 |
| 355 | end |
| 356 | q = add q sub k 1 |
| 357 | end |
| 358 | put gpmbl 0 q |
| 359 | ret 0 |
| 360 | end |
| 361 | note ---- the sig field: an armored SSH signature, at most 4000 bytes; kept with one final line feed |
| 362 | fn pasigin to bool |
| 363 | n = padec 6 4000 |
| 364 | if lt n 0 |
| 365 | ret false |
| 366 | end |
| 367 | s = new uchar 4096 0 |
| 368 | k = ulong 0 |
| 369 | for i ulong ulong n bound 4096 |
| 370 | c = get gpfv i |
| 371 | if ne c 13 |
| 372 | if not sosigc c |
| 373 | ret false |
| 374 | end |
| 375 | put s k c |
| 376 | k = add k 1 |
| 377 | end |
| 378 | end |
| 379 | for it ulong 4096 |
| 380 | if eq k 0 |
| 381 | break |
| 382 | end |
| 383 | c = get s sub k 1 |
| 384 | if and ne c 10 ne c 32 |
| 385 | break |
| 386 | end |
| 387 | k = sub k 1 |
| 388 | end |
| 389 | if lt k add len tssigb len tssige |
| 390 | ret false |
| 391 | end |
| 392 | if not soarmok part s 0 k |
| 393 | ret false |
| 394 | end |
| 395 | for i ulong k bound 4096 |
| 396 | put gpsig i get s i |
| 397 | end |
| 398 | put gpsig k 10 |
| 399 | put gpsigl 0 add k 1 |
| 400 | ret true |
| 401 | end |