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 / omega-projects / lib.om

880 lines · 16177 bytes Raw Blame History
1note omega-projects: pure Omega project validator, TSV metadata parser, and HTML index emitter.
2note Ported from upstream omega-projects (.github/check_projects.py and index.html).
3note Compose with si/directives/om2.om before this file.
4
5const OP_MAX_SLUG ulong 40
6const OP_MAX_TITLE ulong 120
7const OP_MAX_DESC ulong 400
8const OP_MAX_SAVED ulong 32
9
10struct OpMeta h_off ulong h_len ulong s_off ulong s_len ulong t_off ulong t_len ulong d_off ulong d_len ulong v_off ulong v_len ulong valid ulong end
11
12text op_t_anon "anon-"
13text op_t_projects "projects/"
14text op_t_idxhtml "index.html"
15text op_t_readme "README.md"
16text op_t_metajson "meta.json"
17text op_t_amp "&"
18text op_t_lt "<"
19text op_t_gt ">"
20text op_t_quot """
21text op_t_apos "'"
22
23text op_t_ghp "ghp_"
24text op_t_gho "gho_"
25text op_t_ghu "ghu_"
26text op_t_ghs "ghs_"
27text op_t_ghr "ghr_"
28text op_t_ghpat "github_pat_"
29text op_t_sk "sk-"
30text op_t_akia "AKIA"
31text op_t_aiza "AIza"
32text op_t_privkey "PRIVATE KEY-----"
33text op_t_jwt "eyJ"
34
35text op_t_ext_script "<script"
36text op_t_ext_iframe "<iframe"
37text op_t_ext_link "<link"
38text op_t_ext_object "<object"
39text op_t_ext_embed "<embed"
40text op_t_ext_base "<base"
41text op_t_ext_http "http://"
42text op_t_ext_https "https://"
43text op_t_ext_fetch "fetch("
44text op_t_ext_xhr "xmlhttprequest"
45text op_t_ext_ws "websocket"
46text op_t_ext_beacon "sendbeacon"
47
48text op_t_page_head "<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'self'; script-src 'none'; style-src 'unsafe-inline'\">\n<title>Omega projects</title>\n<style>\nbody{font:16px/1.5 system-ui,sans-serif;max-width:44rem;margin:2rem auto;padding:0 1rem;color:#1b1b1b}\na{color:#0b57d0}\n</style>\n</head>\n<body>\n<h1>Omega projects</h1>\n<ul id=\"projects\">\n"
49text op_t_page_empty "<li>No projects are live yet.</li>\n"
50text op_t_page_foot "</ul>\n<p>Each live project is served at <code>projects/&lt;handle&gt;/&lt;project&gt;/</code>.</p>\n</body>\n</html>\n"
51
52text op_t_card_a "<li class=\"project-card\" data-handle=\""
53text op_t_card_b "\" data-slug=\""
54text op_t_card_c "\"><a href=\"projects/"
55text op_t_card_d "/\"><strong>"
56text op_t_card_e "</strong></a> <span class=\"meta\">by "
57text op_t_card_f " ("
58text op_t_card_g ")</span><p>"
59text op_t_card_h "</p></li>\n"
60
61fn op_is_lower c uchar to bool
62if lt c 97
63ret false
64end
65if gt c 122
66ret false
67end
68ret true
69end
70
71fn op_is_digit c uchar to bool
72if lt c 48
73ret false
74end
75if gt c 57
76ret false
77end
78ret true
79end
80
81fn op_is_alnum_lower c uchar to bool
82if op_is_lower c
83ret true
84end
85ret op_is_digit c
86end
87
88fn op_is_hex_lower c uchar to bool
89if op_is_digit c
90ret true
91end
92if lt c 97
93ret false
94end
95if gt c 102
96ret false
97end
98ret true
99end
100
101fn op_to_lower c uchar to uchar
102if ge c 65
103if le c 90
104ret add c 32
105end
106end
107ret c
108end
109
110fn op_slice_eq a view uchar * aoff ulong alen ulong b view uchar * to bool
111if ne alen len b
112ret false
113end
114if gt add aoff alen len a
115ret false
116end
117i = ulong 0
118while lt i alen bound 65536
119if ne get a add aoff i get b i
120ret false
121end
122i = add i 1
123end
124ret true
125end
126
127fn op_match_at_ci hay view uchar * pos ulong ned view uchar * to bool
128nlen = len ned
129j = ulong 0
130while lt j nlen bound 256
131hc = op_to_lower get hay add pos j
132nc = op_to_lower get ned j
133if ne hc nc
134ret false
135end
136j = add j 1
137end
138ret true
139end
140
141fn op_has_substr_ci hay view uchar * hoff ulong hlen ulong ned view uchar * to bool
142nlen = len ned
143if eq nlen 0
144ret true
145end
146if lt hlen nlen
147ret false
148end
149if gt add hoff hlen len hay
150ret false
151end
152limit = add sub hlen nlen 1
153i = ulong 0
154while lt i limit bound 65536
155if op_match_at_ci hay add hoff i ned
156ret true
157end
158i = add i 1
159end
160ret false
161end
162
163fn op_match_at hay view uchar * pos ulong ned view uchar * to bool
164nlen = len ned
165j = ulong 0
166while lt j nlen bound 256
167if ne get hay add pos j get ned j
168ret false
169end
170j = add j 1
171end
172ret true
173end
174
175fn op_has_substr hay view uchar * hoff ulong hlen ulong ned view uchar * to bool
176nlen = len ned
177if eq nlen 0
178ret true
179end
180if lt hlen nlen
181ret false
182end
183if gt add hoff hlen len hay
184ret false
185end
186limit = add sub hlen nlen 1
187i = ulong 0
188while lt i limit bound 65536
189if op_match_at hay add hoff i ned
190ret true
191end
192i = add i 1
193end
194ret false
195end
196
197note Check control characters: refuse < 32 except tab(9), lf(10), cr(13), and refuse DEL(127).
198fn op_has_bad_text buf view uchar * off ulong n ulong to bool
199if gt add off n len buf
200ret true
201end
202i = ulong 0
203while lt i n bound 65536
204c = get buf add off i
205if eq c 127
206ret true
207end
208if lt c 32
209if ne c 9
210if ne c 10
211if ne c 13
212ret true
213end
214end
215end
216end
217i = add i 1
218end
219ret false
220end
221
222note Validate project slug: [a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])? (1..40 bytes).
223note Refuses path traversal (.., /), uppercase, spaces, control chars, HTML chars.
224fn op_valid_slug buf view uchar * off ulong n ulong to bool
225if eq n 0
226ret false
227end
228if gt n OP_MAX_SLUG
229ret false
230end
231if gt add off n len buf
232ret false
233end
234first = get buf off
235if not op_is_alnum_lower first
236ret false
237end
238last = get buf sub add off n 1
239if not op_is_alnum_lower last
240ret false
241end
242i = ulong 0
243while lt i n bound 64
244c = get buf add off i
245if not op_is_alnum_lower c
246if ne c 45
247ret false
248end
249end
250i = add i 1
251end
252ret true
253end
254
255note Validate builder handle:
256note anon-[0-9a-f]{10} OR [a-z](?:[a-z0-9]|-(?=[a-z0-9])){1,23}-[0-9a-f]{8}
257fn op_valid_handle buf view uchar * off ulong n ulong to bool
258if gt add off n len buf
259ret false
260end
261if eq n 15
262if op_slice_eq buf off 5 op_t_anon
263i = ulong 5
264while lt i 15 bound 16
265if not op_is_hex_lower get buf add off i
266ret false
267end
268i = add i 1
269end
270ret true
271end
272end
273if lt n 11
274ret false
275end
276if gt n 33
277ret false
278end
279if not op_is_lower get buf off
280ret false
281end
282dash_pos = sub n 9
283if ne get buf add off dash_pos 45
284ret false
285end
286j = ulong 1
287prev_dash = false
288while lt j dash_pos bound 32
289c = get buf add off j
290if eq c 45
291if prev_dash
292ret false
293end
294if eq add j 1 dash_pos
295ret false
296end
297prev_dash = true
298else
299if not op_is_alnum_lower c
300ret false
301end
302prev_dash = false
303end
304j = add j 1
305end
306k = add dash_pos 1
307while lt k n bound 16
308if not op_is_hex_lower get buf add off k
309ret false
310end
311k = add k 1
312end
313ret true
314end
315
316note Validate project file name:
317note index.html | README.md | meta.json | [a-z0-9][a-z0-9_-]{0,39}.(om|js|css|md|txt|patch)
318fn op_valid_ext buf view uchar * off ulong n ulong to bool
319if eq n 2
320c0 = get buf off
321c1 = get buf add off 1
322if eq c0 111
323if eq c1 109
324ret true
325end
326end
327if eq c0 106
328if eq c1 115
329ret true
330end
331end
332if eq c0 109
333if eq c1 100
334ret true
335end
336end
337ret false
338end
339if eq n 3
340c0 = get buf off
341c1 = get buf add off 1
342c2 = get buf add off 2
343if eq c0 99
344if eq c1 115
345if eq c2 115
346ret true
347end
348end
349end
350if eq c0 116
351if eq c1 120
352if eq c2 116
353ret true
354end
355end
356end
357ret false
358end
359if eq n 5
360if eq get buf off 112
361if eq get buf add off 1 97
362if eq get buf add off 2 116
363if eq get buf add off 3 99
364if eq get buf add off 4 104
365ret true
366end
367end
368end
369end
370end
371end
372ret false
373end
374
375fn op_valid_filename buf view uchar * off ulong n ulong to bool
376if eq n 0
377ret false
378end
379if gt n 46
380ret false
381end
382if gt add off n len buf
383ret false
384end
385if op_slice_eq buf off n op_t_idxhtml
386ret true
387end
388if op_slice_eq buf off n op_t_readme
389ret true
390end
391if op_slice_eq buf off n op_t_metajson
392ret true
393end
394dot = ulong 0
395found_dot = false
396i = ulong 0
397while lt i n bound 64
398c = get buf add off i
399if eq c 46
400if found_dot
401ret false
402end
403dot = i
404found_dot = true
405end
406i = add i 1
407end
408if not found_dot
409ret false
410end
411if eq dot 0
412ret false
413end
414if gt dot 40
415ret false
416end
417if not op_is_alnum_lower get buf off
418ret false
419end
420j = ulong 1
421while lt j dot bound 64
422c = get buf add off j
423if not op_is_alnum_lower c
424if ne c 45
425if ne c 95
426ret false
427end
428end
429end
430j = add j 1
431end
432ext_off = add add off dot 1
433ext_len = sub sub n dot 1
434ret op_valid_ext buf ext_off ext_len
435end
436
437note Validate full repository path: projects/<handle>/<slug>/<filename>
438fn op_valid_project_path buf view uchar * off ulong n ulong to bool
439if lt n 12
440ret false
441end
442if gt add off n len buf
443ret false
444end
445if not op_slice_eq buf off 9 op_t_projects
446ret false
447end
448p1 = ulong 9
449slash1 = ulong 0
450found1 = false
451i = p1
452while lt i n bound 256
453if eq get buf add off i 47
454slash1 = i
455found1 = true
456break
457end
458i = add i 1
459end
460if not found1
461ret false
462end
463if not op_valid_handle buf add off p1 sub slash1 p1
464ret false
465end
466p2 = add slash1 1
467slash2 = ulong 0
468found2 = false
469j = p2
470while lt j n bound 256
471if eq get buf add off j 47
472slash2 = j
473found2 = true
474break
475end
476j = add j 1
477end
478if not found2
479ret false
480end
481if not op_valid_slug buf add off p2 sub slash2 p2
482ret false
483end
484p3 = add slash2 1
485if ge p3 n
486ret false
487end
488ret op_valid_filename buf add off p3 sub n p3
489end
490
491note Detect secret patterns matching upstream check_projects.py
492fn op_has_secret buf view uchar * off ulong n ulong to bool
493if op_has_substr buf off n op_t_ghp
494ret true
495end
496if op_has_substr buf off n op_t_gho
497ret true
498end
499if op_has_substr buf off n op_t_ghu
500ret true
501end
502if op_has_substr buf off n op_t_ghs
503ret true
504end
505if op_has_substr buf off n op_t_ghr
506ret true
507end
508if op_has_substr buf off n op_t_ghpat
509ret true
510end
511if op_has_substr buf off n op_t_sk
512ret true
513end
514if op_has_substr buf off n op_t_akia
515ret true
516end
517if op_has_substr buf off n op_t_aiza
518ret true
519end
520if op_has_substr buf off n op_t_privkey
521ret true
522end
523if op_has_substr buf off n op_t_jwt
524ret true
525end
526ret false
527end
528
529note Detect external web references matching upstream check_projects.py
530fn op_has_external_web buf view uchar * off ulong n ulong to bool
531if op_has_substr_ci buf off n op_t_ext_script
532ret true
533end
534if op_has_substr_ci buf off n op_t_ext_iframe
535ret true
536end
537if op_has_substr_ci buf off n op_t_ext_link
538ret true
539end
540if op_has_substr_ci buf off n op_t_ext_object
541ret true
542end
543if op_has_substr_ci buf off n op_t_ext_embed
544ret true
545end
546if op_has_substr_ci buf off n op_t_ext_base
547ret true
548end
549if op_has_substr_ci buf off n op_t_ext_http
550ret true
551end
552if op_has_substr_ci buf off n op_t_ext_https
553ret true
554end
555if op_has_substr_ci buf off n op_t_ext_fetch
556ret true
557end
558if op_has_substr_ci buf off n op_t_ext_xhr
559ret true
560end
561if op_has_substr_ci buf off n op_t_ext_ws
562ret true
563end
564if op_has_substr_ci buf off n op_t_ext_beacon
565ret true
566end
567ret false
568end
569
570note Validate saved timestamp field (e.g. 2026-11-18 or 2026-11-18T12:00:00Z).
571fn op_valid_saved buf view uchar * off ulong n ulong to bool
572if lt n 4
573ret false
574end
575if gt n OP_MAX_SAVED
576ret false
577end
578if gt add off n len buf
579ret false
580end
581i = ulong 0
582while lt i n bound 64
583c = get buf add off i
584ok = op_is_digit c
585if eq c 45
586ok = true
587end
588if eq c 58
589ok = true
590end
591if eq c 84
592ok = true
593end
594if eq c 90
595ok = true
596end
597if eq c 46
598ok = true
599end
600if not ok
601ret false
602end
603i = add i 1
604end
605ret true
606end
607
608fn op_validate_meta src view uchar * meta edit OpMeta to int
609if not op_valid_handle src get meta h_off get meta h_len
610ret -2
611end
612if not op_valid_slug src get meta s_off get meta s_len
613ret -3
614end
615tlen = get meta t_len
616if eq tlen 0
617ret -4
618end
619if gt tlen OP_MAX_TITLE
620ret -4
621end
622dlen = get meta d_len
623if gt dlen OP_MAX_DESC
624ret -5
625end
626if not op_valid_saved src get meta v_off get meta v_len
627ret -6
628end
629if op_has_bad_text src get meta t_off tlen
630ret -7
631end
632if op_has_bad_text src get meta d_off dlen
633ret -7
634end
635if op_has_secret src get meta t_off tlen
636ret -7
637end
638if op_has_secret src get meta d_off dlen
639ret -7
640end
641put meta valid ulong 1
642ret 0
643end
644
645note Parse TSV project metadata row: handle\tslug\ttitle\tdescription\tsaved
646note Returns 0 on success, negative error code on refusal:
647note -1: bad column count (must be 5)
648note -2: invalid handle
649note -3: invalid slug
650note -4: invalid title (empty or too long)
651note -5: invalid description (too long)
652note -6: invalid saved timestamp
653note -7: control character or secret detected
654fn op_parse_tsv_line src view uchar * off ulong n ulong meta edit OpMeta to int
655put meta valid ulong 0
656if gt add off n len src
657ret -1
658end
659eff = n
660if gt eff 0
661if eq get src sub add off eff 1 10
662eff = sub eff 1
663end
664end
665if gt eff 0
666if eq get src sub add off eff 1 13
667eff = sub eff 1
668end
669end
670tabs = new ulong 5 0
671tcnt = ulong 0
672i = ulong 0
673while lt i eff bound 16384
674if eq get src add off i 9
675if lt tcnt 5
676put tabs tcnt i
677end
678tcnt = add tcnt 1
679end
680i = add i 1
681end
682if ne tcnt 4
683ret -1
684end
685put meta h_off off
686put meta h_len get tabs 0
687put meta s_off add add off get tabs 0 1
688put meta s_len sub sub get tabs 1 get tabs 0 1
689put meta t_off add add off get tabs 1 1
690put meta t_len sub sub get tabs 2 get tabs 1 1
691put meta d_off add add off get tabs 2 1
692put meta d_len sub sub get tabs 3 get tabs 2 1
693put meta v_off add add off get tabs 3 1
694put meta v_len sub sub eff get tabs 3 1
695ret op_validate_meta src meta
696end
697
698note Append slice src[0..len src] to dst at pos. Returns new pos or -1 on overflow.
699fn op_append_raw dst edit uchar * pos ulong src view uchar * to long
700n = len src
701if gt add pos n len dst
702ret -1
703end
704i = ulong 0
705while lt i n bound 65536
706put dst add pos i get src i
707i = add i 1
708end
709ret long add pos n
710end
711
712note Strict HTML escape: &, <, >, ", ' -> HTML entities. Refuses control bytes (<32 except \t\n\r, or 127).
713note Returns new pos (>= 0), -1 on buffer overflow, -2 on forbidden control byte.
714fn op_escape_html dst edit uchar * pos ulong src view uchar * to long
715n = len src
716p = pos
717i = ulong 0
718while lt i n bound 65536
719c = get src i
720if eq c 127
721ret -2
722end
723if lt c 32
724if ne c 9
725if ne c 10
726if ne c 13
727ret -2
728end
729end
730end
731end
732if eq c 38
733np = op_append_raw dst p op_t_amp
734if lt np 0
735ret -1
736end
737p = ulong np
738else
739if eq c 60
740np = op_append_raw dst p op_t_lt
741if lt np 0
742ret -1
743end
744p = ulong np
745else
746if eq c 62
747np = op_append_raw dst p op_t_gt
748if lt np 0
749ret -1
750end
751p = ulong np
752else
753if eq c 34
754np = op_append_raw dst p op_t_quot
755if lt np 0
756ret -1
757end
758p = ulong np
759else
760if eq c 39
761np = op_append_raw dst p op_t_apos
762if lt np 0
763ret -1
764end
765p = ulong np
766else
767if ge p len dst
768ret -1
769end
770put dst p c
771p = add p 1
772end
773end
774end
775end
776end
777i = add i 1
778end
779ret long p
780end
781
782fn op_emit_card_head dst edit uchar * pos ulong src view uchar * meta view OpMeta to long
783p = op_append_raw dst pos op_t_card_a
784if lt p 0
785ret -1
786end
787p = op_append_raw dst ulong p part src get meta h_off get meta h_len
788if lt p 0
789ret -1
790end
791p = op_append_raw dst ulong p op_t_card_b
792if lt p 0
793ret -1
794end
795p = op_append_raw dst ulong p part src get meta s_off get meta s_len
796if lt p 0
797ret -1
798end
799p = op_append_raw dst ulong p op_t_card_c
800if lt p 0
801ret -1
802end
803p = op_append_raw dst ulong p part src get meta h_off get meta h_len
804if lt p 0
805ret -1
806end
807if ge ulong p len dst
808ret -1
809end
810put dst ulong p uchar 47
811p = add p 1
812p = op_append_raw dst ulong p part src get meta s_off get meta s_len
813if lt p 0
814ret -1
815end
816ret op_append_raw dst ulong p op_t_card_d
817end
818
819fn op_emit_card_body dst edit uchar * pos ulong src view uchar * meta view OpMeta to long
820p = op_escape_html dst pos part src get meta t_off get meta t_len
821if lt p 0
822ret p
823end
824p = op_append_raw dst ulong p op_t_card_e
825if lt p 0
826ret -1
827end
828p = op_append_raw dst ulong p part src get meta h_off get meta h_len
829if lt p 0
830ret -1
831end
832p = op_append_raw dst ulong p op_t_card_f
833if lt p 0
834ret -1
835end
836p = op_append_raw dst ulong p part src get meta v_off get meta v_len
837if lt p 0
838ret -1
839end
840p = op_append_raw dst ulong p op_t_card_g
841if lt p 0
842ret -1
843end
844p = op_escape_html dst ulong p part src get meta d_off get meta d_len
845if lt p 0
846ret p
847end
848ret op_append_raw dst ulong p op_t_card_h
849end
850
851note Emit one deterministic HTML index card with strict HTML escaping.
852note Re-validates handle and slug before emitting. Returns new pos or negative error.
853fn op_emit_card dst edit uchar * pos ulong src view uchar * meta view OpMeta to long
854if ne get meta valid ulong 1
855ret -2
856end
857if not op_valid_handle src get meta h_off get meta h_len
858ret -2
859end
860if not op_valid_slug src get meta s_off get meta s_len
861ret -3
862end
863p = op_emit_card_head dst pos src meta
864if lt p 0
865ret p
866end
867ret op_emit_card_body dst ulong p src meta
868end
869
870fn op_emit_index_header dst edit uchar * pos ulong to long
871ret op_append_raw dst pos op_t_page_head
872end
873
874fn op_emit_index_empty dst edit uchar * pos ulong to long
875ret op_append_raw dst pos op_t_page_empty
876end
877
878fn op_emit_index_footer dst edit uchar * pos ulong to long
879ret op_append_raw dst pos op_t_page_foot
880end