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 / site_builder / script_policy.om

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