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