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 / site_builder / script_policy.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note apps/site_builder/script_policy.om: pure-Omega zero-external-script policy verifier |
| 3 | note Enforces strict static HTML safety for aienos.com and interplane-forge: |
| 4 | note forbids <script>, inline event handlers, javascript:/data:text/html URIs, |
| 5 | note active embeds (<iframe>/<object>/<embed>/<applet>), external <link> stylesheets, |
| 6 | note and unsafe control bytes. |
| 7 | note Compose after si/directives/om2.om and apps/site_builder/html_feed.om. |
| 8 | |
| 9 | const SB_POL_OK ulong 0 |
| 10 | const SB_POL_ERR_EMPTY ulong 1 |
| 11 | const SB_POL_ERR_SCRIPT_TAG ulong 2 |
| 12 | const SB_POL_ERR_EVENT_HANDLER ulong 3 |
| 13 | const SB_POL_ERR_JS_URI ulong 4 |
| 14 | const SB_POL_ERR_EXT_LINK ulong 5 |
| 15 | const SB_POL_ERR_CTRL_BYTE ulong 6 |
| 16 | |
| 17 | text sb_pat_script "<script" |
| 18 | text sb_pat_onload "onload=" |
| 19 | text sb_pat_onerror "onerror=" |
| 20 | text sb_pat_onclick "onclick=" |
| 21 | text sb_pat_onfocus "onfocus=" |
| 22 | text sb_pat_onhover "onmouseover=" |
| 23 | text sb_pat_jsuri "javascript:" |
| 24 | text sb_pat_datahtml "data:text/html" |
| 25 | text sb_pat_iframe "<iframe" |
| 26 | text sb_pat_object "<object" |
| 27 | text sb_pat_embed "<embed" |
| 28 | text sb_pat_applet "<applet" |
| 29 | text sb_pat_ext_http "href=\"http://" |
| 30 | text sb_pat_ext_https "href=\"https://" |
| 31 | text sb_pat_ext_proto "href=\"//" |
| 32 | text sb_pat_src_http "src=\"http://" |
| 33 | text sb_pat_src_https "src=\"https://" |
| 34 | text sb_pat_src_proto "src=\"//" |
| 35 | |
| 36 | fn sb_ascii_lower b uchar to uchar |
| 37 | if and ge b uchar 65 le b uchar 90 |
| 38 | ret add b uchar 32 |
| 39 | end |
| 40 | ret b |
| 41 | end |
| 42 | |
| 43 | fn sb_contains_ci hay view uchar * needle view uchar * to bool |
| 44 | nh = len hay |
| 45 | nn = len needle |
| 46 | if eq nn ulong 0 |
| 47 | ret true |
| 48 | end |
| 49 | if lt nh nn |
| 50 | ret false |
| 51 | end |
| 52 | if gt nh ulong 65536 |
| 53 | ret false |
| 54 | end |
| 55 | lim = sub nh nn |
| 56 | i = ulong 0 |
| 57 | while le i lim bound 65537 |
| 58 | matched = true |
| 59 | j = ulong 0 |
| 60 | while lt j nn bound 64 |
| 61 | ch = sb_ascii_lower get hay add i j |
| 62 | cn = sb_ascii_lower get needle j |
| 63 | if ne ch cn |
| 64 | matched = false |
| 65 | break |
| 66 | end |
| 67 | j = add j ulong 1 |
| 68 | end |
| 69 | if matched |
| 70 | ret true |
| 71 | end |
| 72 | i = add i ulong 1 |
| 73 | end |
| 74 | ret false |
| 75 | end |
| 76 | |
| 77 | fn sb_check_ctrl_bytes html view uchar * to bool |
| 78 | n = len html |
| 79 | if gt n ulong 65536 |
| 80 | ret false |
| 81 | end |
| 82 | i = ulong 0 |
| 83 | while lt i n bound 65536 |
| 84 | b = get html i |
| 85 | if lt b uchar 32 |
| 86 | if and ne b uchar 9 and ne b uchar 10 ne b uchar 13 |
| 87 | ret false |
| 88 | end |
| 89 | end |
| 90 | if eq b uchar 127 |
| 91 | ret false |
| 92 | end |
| 93 | i = add i ulong 1 |
| 94 | end |
| 95 | ret true |
| 96 | end |
| 97 | |
| 98 | fn sb_check_events html view uchar * to bool |
| 99 | if sb_contains_ci html sb_pat_onload |
| 100 | ret false |
| 101 | end |
| 102 | if sb_contains_ci html sb_pat_onerror |
| 103 | ret false |
| 104 | end |
| 105 | if sb_contains_ci html sb_pat_onclick |
| 106 | ret false |
| 107 | end |
| 108 | if sb_contains_ci html sb_pat_onfocus |
| 109 | ret false |
| 110 | end |
| 111 | if sb_contains_ci html sb_pat_onhover |
| 112 | ret false |
| 113 | end |
| 114 | ret true |
| 115 | end |
| 116 | |
| 117 | fn sb_check_ext_embeds html view uchar * to bool |
| 118 | if sb_contains_ci html sb_pat_iframe |
| 119 | ret false |
| 120 | end |
| 121 | if sb_contains_ci html sb_pat_object |
| 122 | ret false |
| 123 | end |
| 124 | if sb_contains_ci html sb_pat_embed |
| 125 | ret false |
| 126 | end |
| 127 | if sb_contains_ci html sb_pat_applet |
| 128 | ret false |
| 129 | end |
| 130 | if sb_contains_ci html sb_pat_ext_http |
| 131 | ret false |
| 132 | end |
| 133 | if sb_contains_ci html sb_pat_ext_https |
| 134 | ret false |
| 135 | end |
| 136 | if sb_contains_ci html sb_pat_ext_proto |
| 137 | ret false |
| 138 | end |
| 139 | if sb_contains_ci html sb_pat_src_http |
| 140 | ret false |
| 141 | end |
| 142 | if sb_contains_ci html sb_pat_src_https |
| 143 | ret false |
| 144 | end |
| 145 | if sb_contains_ci html sb_pat_src_proto |
| 146 | ret false |
| 147 | end |
| 148 | ret true |
| 149 | end |
| 150 | |
| 151 | fn sb_verify_zero_external_script html view uchar * to ulong |
| 152 | n = len html |
| 153 | if eq n ulong 0 |
| 154 | ret SB_POL_ERR_EMPTY |
| 155 | end |
| 156 | if not sb_check_ctrl_bytes html |
| 157 | ret SB_POL_ERR_CTRL_BYTE |
| 158 | end |
| 159 | if sb_contains_ci html sb_pat_script |
| 160 | ret SB_POL_ERR_SCRIPT_TAG |
| 161 | end |
| 162 | if not sb_check_events html |
| 163 | ret SB_POL_ERR_EVENT_HANDLER |
| 164 | end |
| 165 | if or sb_contains_ci html sb_pat_jsuri sb_contains_ci html sb_pat_datahtml |
| 166 | ret SB_POL_ERR_JS_URI |
| 167 | end |
| 168 | if not sb_check_ext_embeds html |
| 169 | ret SB_POL_ERR_EXT_LINK |
| 170 | end |
| 171 | ret SB_POL_OK |
| 172 | end |