interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / patch / p3gate.om

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