interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / sovereign-core / om / serve / http_auth.om

457 lines · 10313 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/sovereign-core/om/serve/http_auth.om
3note Pure OM-2 conversion of aien-serve-http HTTP/1.1 request parser, bounded read caps,
4note anti-smuggling header validation, and constant-time Authorization: Bearer enforcement.
5
6const SRV_MAX_REQ_BYTES ulong 4096
7const SRV_MAX_HDR_BYTES ulong 1024
8const SRV_MAX_BODY_BYTES ulong 2048
9const SRV_MAX_HEADERS ulong 24
10const SRV_MAX_PATH_LEN ulong 128
11const SRV_MAX_AUTH_LEN ulong 128
12const SRV_MIN_TOKEN_LEN ulong 16
13const SRV_MAX_TOKEN_LEN ulong 64
14
15note HTTP Methods
16const HTTP_M_UNKNOWN ulong 0
17const HTTP_M_GET ulong 1
18const HTTP_M_HEAD ulong 2
19const HTTP_M_POST ulong 3
20
21note Parse result codes (directly map to HTTP status on error)
22const PARSE_OK ulong 0
23const PARSE_ERR_BAD_REQ ulong 400
24const PARSE_ERR_METHOD ulong 405
25const PARSE_ERR_TOO_LARGE ulong 413
26const PARSE_ERR_HDR_TOO_LARGE ulong 431
27
28note Auth config: [0]=configured(0/1), [1]=tok_len, [2]=allow_cnt, [3]=deny_cnt
29global srv_auth_meta ulong 8
30global srv_auth_tok uchar 64
31
32note Parsed HTTP request state:
33note srv_req_meta: [0]=method, [1]=path_len, [2]=auth_len, [3]=has_auth, [4]=has_clen, [5]=clen_val, [6]=body_len, [7]=hdr_cnt
34global srv_req_meta ulong 8
35global srv_req_path uchar 128
36global srv_req_auth uchar 128
37global srv_req_body uchar 2048
38
39text srv_tok_get "GET"
40text srv_tok_head "HEAD"
41text srv_tok_post "POST"
42text srv_tok_h11 "HTTP/1.1"
43text srv_tok_h10 "HTTP/1.0"
44text srv_tok_bearer "Bearer "
45text srv_hdr_auth "authorization"
46text srv_hdr_clen "content-length"
47text srv_hdr_te "transfer-encoding"
48
49note ---- Auth (fail-closed, constant-time) ----
50
51fn srv_auth_reset to void
52for i ulong 8
53put srv_auth_meta i 0
54end
55for i ulong 64
56put srv_auth_tok i uchar 0
57end
58end
59
60fn srv_auth_is_configured to bool
61ret ne get srv_auth_meta 0 0
62end
63
64fn srv_auth_allow_count to ulong
65ret get srv_auth_meta 2
66end
67
68fn srv_auth_deny_count to ulong
69ret get srv_auth_meta 3
70end
71
72note Configure operator bearer token; refuses < 16 bytes, > 64 bytes, or whitespace/control chars.
73fn srv_auth_set_token tok view uchar * to bool
74n = len tok
75if or lt n SRV_MIN_TOKEN_LEN gt n SRV_MAX_TOKEN_LEN
76ret false
77end
78for i ulong n bound 64
79b = get tok i
80if or le b uchar 32 ge b uchar 127
81ret false
82end
83end
84for i ulong 64
85put srv_auth_tok i uchar 0
86end
87for i ulong n bound 64
88put srv_auth_tok i get tok i
89end
90put srv_auth_meta 0 1
91put srv_auth_meta 1 n
92ret true
93end
94
95note Constant-time check of Authorization header value against configured Bearer token.
96note Fail-closed: returns false if unconfigured, missing "Bearer " prefix, or any mismatch.
97fn srv_auth_verify_header val view uchar * to bool
98if eq get srv_auth_meta 0 0
99put srv_auth_meta 3 add get srv_auth_meta 3 1
100ret false
101end
102vlen = len val
103plen = len srv_tok_bearer
104if lt vlen plen
105put srv_auth_meta 3 add get srv_auth_meta 3 1
106ret false
107end
108pdiff = uchar 0
109for i ulong plen bound 16
110pdiff = or pdiff xor get val i get srv_tok_bearer i
111end
112tlen = get srv_auth_meta 1
113cand_len = sub vlen plen
114len_ok = eq cand_len tlen
115tdiff = uchar 0
116for i ulong SRV_MAX_TOKEN_LEN
117in_tok = lt i tlen
118in_cand = lt i cand_len
119cb = uchar 0
120if in_cand
121cb = get val add plen i
122end
123tb = uchar 0
124if in_tok
125tb = get srv_auth_tok i
126end
127if in_tok
128tdiff = or tdiff xor cb tb
129end
130end
131ok = and eq pdiff uchar 0 and len_ok eq tdiff uchar 0
132if ok
133put srv_auth_meta 2 add get srv_auth_meta 2 1
134else
135put srv_auth_meta 3 add get srv_auth_meta 3 1
136end
137ret ok
138end
139
140note Verify the Authorization header captured in srv_req_auth.
141fn srv_auth_verify_req to bool
142if eq get srv_req_meta 3 0
143put srv_auth_meta 3 add get srv_auth_meta 3 1
144ret false
145end
146alen = get srv_req_meta 2
147buf = new uchar 128 0
148for i ulong alen bound 128
149put buf i get srv_req_auth i
150end
151ret srv_auth_verify_header part buf 0 alen
152end
153
154note ---- Byte/ASCII helpers ----
155
156fn srv_lower b uchar to uchar
157if and ge b uchar 65 le b uchar 90
158ret add b uchar 32
159end
160ret b
161end
162
163fn srv_slice_eq raw view uchar * off ulong slen ulong lit view uchar * to bool
164if ne slen len lit
165ret false
166end
167for i ulong slen bound 256
168if ne get raw add off i get lit i
169ret false
170end
171end
172ret true
173end
174
175fn srv_slice_ieq raw view uchar * off ulong slen ulong lit view uchar * to bool
176if ne slen len lit
177ret false
178end
179for i ulong slen bound 256
180if ne srv_lower get raw add off i get lit i
181ret false
182end
183end
184ret true
185end
186
187note Parse strict non-negative decimal integer in raw[off..off+slen); returns 18446744073709551615 on error.
188fn srv_parse_u64 raw view uchar * off ulong slen ulong to ulong
189if or eq slen 0 gt slen 12
190ret 18446744073709551615
191end
192acc = ulong 0
193for i ulong slen bound 16
194b = get raw add off i
195if or lt b uchar 48 gt b uchar 57
196ret 18446744073709551615
197end
198acc = add mul acc 10 ulong sub b uchar 48
199end
200ret acc
201end
202
203note Find \r\n\r\n header terminator in raw[0..lim); returns index of first \r or 18446744073709551615.
204fn srv_find_hdr_end raw view uchar * lim ulong to ulong
205if lt lim 4
206ret 18446744073709551615
207end
208end_i = sub lim 3
209for i ulong end_i bound 4096
210if eq get raw i uchar 13
211if eq get raw add i 1 uchar 10
212if eq get raw add i 2 uchar 13
213if eq get raw add i 3 uchar 10
214ret i
215end
216end
217end
218end
219end
220ret 18446744073709551615
221end
222
223note Find \r\n starting at off up to lim; returns index of \r or 18446744073709551615.
224fn srv_find_crlf raw view uchar * off ulong lim ulong to ulong
225if ge add off 1 lim
226ret 18446744073709551615
227end
228i = off
229while lt add i 1 lim bound 2048
230if eq get raw i uchar 13
231if eq get raw add i 1 uchar 10
232ret i
233end
234end
235i = add i 1
236end
237ret 18446744073709551615
238end
239
240note Parse request line: METHOD SP PATH SP HTTP/1.x
241fn srv_parse_req_line raw view uchar * line_end ulong to ulong
242sp1 = ulong 18446744073709551615
243sp2 = ulong 18446744073709551615
244for i ulong line_end bound 512
245b = get raw i
246if or eq b uchar 0 lt b uchar 32
247ret PARSE_ERR_BAD_REQ
248end
249if eq b uchar 32
250if eq sp1 18446744073709551615
251sp1 = i
252else
253if eq sp2 18446744073709551615
254sp2 = i
255else
256ret PARSE_ERR_BAD_REQ
257end
258end
259end
260end
261if or eq sp1 18446744073709551615 eq sp2 18446744073709551615
262ret PARSE_ERR_BAD_REQ
263end
264m = HTTP_M_UNKNOWN
265if srv_slice_eq raw 0 sp1 srv_tok_get
266m = HTTP_M_GET
267else
268if srv_slice_eq raw 0 sp1 srv_tok_head
269m = HTTP_M_HEAD
270else
271if srv_slice_eq raw 0 sp1 srv_tok_post
272m = HTTP_M_POST
273end
274end
275end
276if eq m HTTP_M_UNKNOWN
277ret PARSE_ERR_METHOD
278end
279put srv_req_meta 0 m
280poff = add sp1 1
281plen = sub sp2 poff
282if or eq plen 0 gt plen SRV_MAX_PATH_LEN
283ret PARSE_ERR_BAD_REQ
284end
285if ne get raw poff uchar 47
286ret PARSE_ERR_BAD_REQ
287end
288note Reject path traversal ("..") and control/space characters
289for i ulong plen bound 128
290b = get raw add poff i
291if or le b uchar 32 ge b uchar 127
292ret PARSE_ERR_BAD_REQ
293end
294if eq b uchar 46
295if lt add i 1 plen
296if eq get raw add poff add i 1 uchar 46
297ret PARSE_ERR_BAD_REQ
298end
299end
300end
301put srv_req_path i b
302end
303put srv_req_meta 1 plen
304voff = add sp2 1
305vlen = sub line_end voff
306v_ok = or srv_slice_eq raw voff vlen srv_tok_h11 srv_slice_eq raw voff vlen srv_tok_h10
307if not v_ok
308ret PARSE_ERR_BAD_REQ
309end
310ret PARSE_OK
311end
312
313note Parse one HTTP header line raw[off..lend). Enforces anti-smuggling: the body is framed by Content-Length
314note alone, so any Transfer-Encoding header (any case, any value, with or without Content-Length) is 400; a
315note second Content-Length (duplicate or conflicting) or a second Authorization is 400; whitespace inside the
316note field name (e.g. "Transfer-Encoding : chunked", or an obs-fold continuation line) is 400 (RFC 9112 5.1).
317fn srv_parse_one_hdr raw view uchar * off ulong lend ulong to ulong
318colon = ulong 18446744073709551615
319i = off
320while lt i lend bound 1024
321b = get raw i
322if or eq b uchar 0 and lt b uchar 32 ne b uchar 9
323ret PARSE_ERR_BAD_REQ
324end
325if and eq colon 18446744073709551615 eq b uchar 58
326colon = i
327end
328i = add i 1
329end
330if or eq colon 18446744073709551615 eq colon off
331ret PARSE_ERR_BAD_REQ
332end
333nlen = sub colon off
334for j ulong nlen bound 1024
335nb = get raw add off j
336if or eq nb uchar 32 eq nb uchar 9
337ret PARSE_ERR_BAD_REQ
338end
339end
340if srv_slice_ieq raw off nlen srv_hdr_te
341ret PARSE_ERR_BAD_REQ
342end
343vstart = add colon 1
344while lt vstart lend bound 1024
345vb = get raw vstart
346if or eq vb uchar 32 eq vb uchar 9
347vstart = add vstart 1
348else
349break
350end
351end
352vend = lend
353while gt vend vstart bound 1024
354eb = get raw sub vend 1
355if or eq eb uchar 32 eq eb uchar 9
356vend = sub vend 1
357else
358break
359end
360end
361vlen = sub vend vstart
362if srv_slice_ieq raw off nlen srv_hdr_auth
363if ne get srv_req_meta 3 0
364ret PARSE_ERR_BAD_REQ
365end
366if gt vlen SRV_MAX_AUTH_LEN
367ret PARSE_ERR_HDR_TOO_LARGE
368end
369for k ulong vlen bound 128
370put srv_req_auth k get raw add vstart k
371end
372put srv_req_meta 2 vlen
373put srv_req_meta 3 1
374ret PARSE_OK
375end
376if srv_slice_ieq raw off nlen srv_hdr_clen
377if ne get srv_req_meta 4 0
378ret PARSE_ERR_BAD_REQ
379end
380cl = srv_parse_u64 raw vstart vlen
381if eq cl 18446744073709551615
382ret PARSE_ERR_BAD_REQ
383end
384if gt cl SRV_MAX_BODY_BYTES
385ret PARSE_ERR_TOO_LARGE
386end
387put srv_req_meta 4 1
388put srv_req_meta 5 cl
389ret PARSE_OK
390end
391ret PARSE_OK
392end
393
394note Parse complete HTTP/1.1 request with strict read caps and anti-smuggling validation.
395fn srv_parse_http_req raw view uchar * to ulong
396for i ulong 8
397put srv_req_meta i 0
398end
399rlen = len raw
400if gt rlen SRV_MAX_REQ_BYTES
401ret PARSE_ERR_TOO_LARGE
402end
403hdr_end = srv_find_hdr_end raw rlen
404if eq hdr_end 18446744073709551615
405if gt rlen SRV_MAX_HDR_BYTES
406ret PARSE_ERR_HDR_TOO_LARGE
407end
408ret PARSE_ERR_BAD_REQ
409end
410if gt hdr_end SRV_MAX_HDR_BYTES
411ret PARSE_ERR_HDR_TOO_LARGE
412end
413line1_end = srv_find_crlf raw 0 add hdr_end 2
414if eq line1_end 18446744073709551615
415ret PARSE_ERR_BAD_REQ
416end
417st1 = srv_parse_req_line raw line1_end
418if ne st1 PARSE_OK
419ret st1
420end
421pos = add line1_end 2
422while lt pos hdr_end bound 64
423lend = srv_find_crlf raw pos add hdr_end 2
424if eq lend 18446744073709551615
425ret PARSE_ERR_BAD_REQ
426end
427hcnt = add get srv_req_meta 7 1
428if gt hcnt SRV_MAX_HEADERS
429ret PARSE_ERR_HDR_TOO_LARGE
430end
431put srv_req_meta 7 hcnt
432hst = srv_parse_one_hdr raw pos lend
433if ne hst PARSE_OK
434ret hst
435end
436pos = add lend 2
437end
438body_off = add hdr_end 4
439body_len = sub rlen body_off
440if gt body_len SRV_MAX_BODY_BYTES
441ret PARSE_ERR_TOO_LARGE
442end
443if ne get srv_req_meta 4 0
444if ne body_len get srv_req_meta 5
445ret PARSE_ERR_BAD_REQ
446end
447else
448if gt body_len 0
449ret PARSE_ERR_BAD_REQ
450end
451end
452for i ulong body_len bound 2048
453put srv_req_body i get raw add body_off i
454end
455put srv_req_meta 6 body_len
456ret PARSE_OK
457end