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 / apps / core / site_forge.om

297 lines · 5790 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note apps/core/site_forge.om: pure-Omega port of aienos.com static site/consent/CSP
3note verifier and interplane-forge/hop ref, archive allowlist, and frame-bound validators.
4note Compose after si/directives/om2.om and om/tls/sha2.om.
5
6const APP_OK ulong 0
7const APP_ERR_CSP_MISSING_DEFAULT ulong 1
8const APP_ERR_CSP_UNSAFE ulong 2
9const APP_ERR_BAD_ROUTE ulong 3
10const APP_ERR_CONSENT_VIOLATION ulong 4
11const APP_ERR_BAD_REF ulong 5
12const APP_ERR_BAD_EXT ulong 6
13const APP_ERR_FRAME_KIND ulong 7
14const APP_ERR_FRAME_CAP ulong 8
15const APP_ERR_FRAME_SEQ ulong 9
16
17const HOP_KIND_DATA ulong 1
18const HOP_KIND_PING ulong 2
19const HOP_KIND_ACK ulong 3
20const HOP_KIND_CLOSE ulong 4
21
22text app_csp_def "default-src 'self'"
23text app_csp_ui "'unsafe-inline'"
24text app_csp_ue "'unsafe-eval'"
25text app_csp_http "http:"
26text app_ext_tgz ".tar.gz"
27text app_ext_zip ".zip"
28text app_ref_lock ".lock"
29
30note --- Basic helpers ---
31
32fn app_slice_eq a view uchar * b view uchar * to bool
33na = len a
34nb = len b
35if ne na nb
36ret false
37end
38if gt na ulong 4096
39ret false
40end
41i = ulong 0
42while lt i na bound 4096
43if ne get a i get b i
44ret false
45end
46i = add i ulong 1
47end
48ret true
49end
50
51fn app_contains hay view uchar * needle view uchar * to bool
52nh = len hay
53nn = len needle
54if eq nn ulong 0
55ret true
56end
57if lt nh nn
58ret false
59end
60if gt nh ulong 4096
61ret false
62end
63lim = sub nh nn
64i = ulong 0
65while le i lim bound 4097
66if app_slice_eq part hay i nn needle
67ret true
68end
69i = add i ulong 1
70end
71ret false
72end
73
74fn app_ends_with s view uchar * sfx view uchar * to bool
75ns = len s
76nx = len sfx
77if lt ns nx
78ret false
79end
80ret app_slice_eq part s sub ns nx nx sfx
81end
82
83fn app_write_all fd int buf view uchar * to bool
84n = len buf
85if eq n ulong 0
86ret true
87end
88if gt n ulong 1048576
89ret false
90end
91off = ulong 0
92while lt off n bound 1048576
93remn = sub n off
94k = write fd buf off remn
95if le k long 0
96ret false
97end
98off = add off ulong k
99end
100ret true
101end
102
103fn app_digest_eq a view uchar 32 b view uchar 32 to bool
104for i ulong 32
105if ne get a i get b i
106ret false
107end
108end
109ret true
110end
111
112note --- aienos.com CSP, Route & Consent Verifiers ---
113
114fn app_validate_csp hdr view uchar * to ulong
115n = len hdr
116if eq n ulong 0
117ret APP_ERR_CSP_MISSING_DEFAULT
118end
119if gt n ulong 2048
120ret APP_ERR_CSP_UNSAFE
121end
122i = ulong 0
123while lt i n bound 2048
124b = get hdr i
125if or lt b uchar 32 ge b uchar 127
126ret APP_ERR_CSP_UNSAFE
127end
128if eq b uchar 42
129ret APP_ERR_CSP_UNSAFE
130end
131i = add i ulong 1
132end
133if not app_contains hdr app_csp_def
134ret APP_ERR_CSP_MISSING_DEFAULT
135end
136if app_contains hdr app_csp_ui
137ret APP_ERR_CSP_UNSAFE
138end
139if app_contains hdr app_csp_ue
140ret APP_ERR_CSP_UNSAFE
141end
142if app_contains hdr app_csp_http
143ret APP_ERR_CSP_UNSAFE
144end
145ret APP_OK
146end
147
148fn app_is_path_ch b uchar to bool
149if and ge b uchar 97 le b uchar 122
150ret true
151end
152if and ge b uchar 65 le b uchar 90
153ret true
154end
155if and ge b uchar 48 le b uchar 57
156ret true
157end
158if eq b uchar 47
159ret true
160end
161if eq b uchar 95
162ret true
163end
164if eq b uchar 45
165ret true
166end
167if eq b uchar 46
168ret true
169end
170ret false
171end
172
173note Strict route path: starts with '/', [a-zA-Z0-9/_.-], no "..", no "//", no "/." hidden file, no '%'
174fn app_validate_route path view uchar * to ulong
175n = len path
176if eq n ulong 0
177ret APP_ERR_BAD_ROUTE
178end
179if gt n ulong 512
180ret APP_ERR_BAD_ROUTE
181end
182if ne get path 0 uchar 47
183ret APP_ERR_BAD_ROUTE
184end
185prev = uchar 0
186i = ulong 0
187while lt i n bound 512
188b = get path i
189if not app_is_path_ch b
190ret APP_ERR_BAD_ROUTE
191end
192if and eq prev uchar 46 eq b uchar 46
193ret APP_ERR_BAD_ROUTE
194end
195if and eq prev uchar 47 eq b uchar 47
196ret APP_ERR_BAD_ROUTE
197end
198if and eq prev uchar 47 eq b uchar 46
199ret APP_ERR_BAD_ROUTE
200end
201prev = b
202i = add i ulong 1
203end
204ret APP_OK
205end
206
207note Consent/DNT fail-closed gate: analytics_enabled requires user_consented=true AND dnt_header=false.
208fn app_check_consent analytics_enabled bool user_consented bool dnt_header bool to ulong
209if analytics_enabled
210if dnt_header
211ret APP_ERR_CONSENT_VIOLATION
212end
213if not user_consented
214ret APP_ERR_CONSENT_VIOLATION
215end
216end
217ret APP_OK
218end
219
220note --- interplane-forge Git Ref & Archive Extension Validators ---
221note Rejects leading '-' (option injection), "..", "@{", "~", "^", ":", spaces, control chars, trailing ".lock".
222
223fn forge_validate_ref r view uchar * to ulong
224n = len r
225if eq n ulong 0
226ret APP_ERR_BAD_REF
227end
228if gt n ulong 128
229ret APP_ERR_BAD_REF
230end
231first = get r 0
232last = get r sub n ulong 1
233if or eq first uchar 45 or eq first uchar 46 eq first uchar 47
234ret APP_ERR_BAD_REF
235end
236if or eq last uchar 46 eq last uchar 47
237ret APP_ERR_BAD_REF
238end
239if app_ends_with r app_ref_lock
240ret APP_ERR_BAD_REF
241end
242prev = uchar 0
243i = ulong 0
244while lt i n bound 128
245b = get r i
246if not app_is_path_ch b
247ret APP_ERR_BAD_REF
248end
249if and eq prev uchar 46 eq b uchar 46
250ret APP_ERR_BAD_REF
251end
252if and eq prev uchar 47 eq b uchar 47
253ret APP_ERR_BAD_REF
254end
255prev = b
256i = add i ulong 1
257end
258ret APP_OK
259end
260
261note Archive download allowlist: only ".tar.gz" and ".zip" permitted.
262fn forge_validate_ext ext view uchar * to ulong
263if app_slice_eq ext app_ext_tgz
264ret APP_OK
265end
266if app_slice_eq ext app_ext_zip
267ret APP_OK
268end
269ret APP_ERR_BAD_EXT
270end
271
272note --- hop Frame & Sequence Validator ---
273
274fn hop_validate_frame kind ulong payload_len ulong max_cap ulong prev_seq ulong seq ulong to ulong
275if or lt kind HOP_KIND_DATA gt kind HOP_KIND_CLOSE
276ret APP_ERR_FRAME_KIND
277end
278if gt payload_len max_cap
279ret APP_ERR_FRAME_CAP
280end
281if le seq prev_seq
282ret APP_ERR_FRAME_SEQ
283end
284ret APP_OK
285end
286
287note Canonical SHA-256 manifest digest over (route, csp_hdr).
288fn app_manifest_digest route view uchar * csp_hdr view uchar * dst edit uchar 32 to void
289ctx = new ulong 40 0
290sep = new uchar 1 0
291put sep 0 uchar 10
292sha256_init ctx
293sha256_update ctx route
294sha256_update ctx sep
295sha256_update ctx csp_hdr
296sha256_final ctx dst
297end