ext/web W1–W3: middleware, data layer, and security hardening

Deliver the interrupted network extension backlog: declarative middleware
(CORS, security headers, gzip, JSON API), SQLite pool/transactions/pagination,
and W3 security (argon2 passwords, CSRF on forms/login, persistent sessions,
CSPRNG ids, login rate limiting). Adds gold smokes and author-facing
hash_password on ext/web.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-28 09:35:39 +08:00
1 parent da9fc75b1e
commit be6223f1fb
22 files changed
+3081 -343

No files matched your search

+146
View File
@@ -0,0 +1,146 @@
---
title: web db W2 smoke — 事务 / 分页 / 原生查询 / 计数 / 查询表达力
import web:ext/web/web.mq.md
import json:lib/json.mq.md
---
# main
*store = > web.db url="sqlite:web-fixtures/data/w2-smoke.db"*
`字段` =
| 字段 | 类型 | 可空 |
|------|------|------|
| title | text | 否 |
| body | text | 是 |
| views | int | 是 |
> `store`.init name=posts fields=`字段`
> `store`.exec sql="DELETE FROM posts"
`数据` =
| title | body | views |
|-------|------|-------|
| Alpha | first | 10 |
| Beta | second | 20 |
| Gamma | third | 30 |
| Delta | fourth | 40 |
> `store`.insert table=posts rows=`数据`
*c = > `store`.count table="posts"*
1. `c` == 4
> print text=count-ok
2. *
> print text=count-fail
`视图条件` =
| 字段 | 操作 | 值 |
|------|------|-----|
| views | = | 30 |
*c2 = > `store`.count table="posts" where=`视图条件`*
1. `c2` == 1
> print text=count-where-ok
2. *
> print text=count-where-fail
*page1 = > `store`.paginate table="posts" limit=2 跳过=0*
*total1 = page1[^total]*
*p1r = page1[^rows]*
*n1 = > len value=`p1r`*
1. `total1` == 4
1. `n1` == 2
> print text=page-ok
2. *
> print text=page-fail
*page2 = > `store`.paginate table="posts" limit=2 跳过=2*
*p2r = page2[^rows]*
*n2 = > len value=`p2r`*
1. `n2` == 2
> print text=page2-ok
2. *
> print text=page2-fail
`in条件` =
| 字段 | 操作 | 值 |
|------|------|-----|
| title | in | "Alpha,Beta" |
*in_rows = > `store`.select table="posts" where=`in条件` limit=10*
*nin = > len value=`in_rows`*
1. `nin` == 2
> print text=in-ok
2. *
> print text=in-fail
`between条件` =
| 字段 | 操作 | 值 |
|------|------|-----|
| views | between | "20,35" |
*bt_rows = > `store`.select table="posts" where=`between条件` limit=10*
*nbt = > len value=`bt_rows`*
1. `nbt` == 2
> print text=between-ok
2. *
> print text=between-fail
`or条件` =
| 字段 | 操作 | 值 | 或 |
|------|------|-----|-----|
| title | = | Alpha | |
| title | = | Gamma | 是 |
*or_rows = > `store`.select table="posts" where=`or条件` limit=10*
*nor = > len value=`or_rows`*
1. `nor` == 2
> print text=or-ok
2. *
> print text=or-fail
*agg = > `store`.query sql="SELECT title, views FROM posts WHERE views >= 30 ORDER BY views DESC"*
*nagg = agg[^count]*
1. `nagg` == 2
> print text=query-ok
2. *
> print text=query-fail
*txn = > `store`.事务*
`更多` =
| title | body | views |
|-------|------|-------|
| Epsilon | fifth | 50 |
> `txn`.insert table=posts rows=`更多`
> `txn`.提交
*c5 = > `store`.count table="posts"*
1. `c5` == 5
> print text=txn-commit-ok
2. *
> print text=txn-commit-fail
*txn2 = > `store`.事务*
`更多2` =
| title | body | views |
|-------|------|-------|
| Zeta | sixth | 60 |
> `txn2`.insert table=posts rows=`更多2`
> `txn2`.回滚
*c6 = > `store`.count table="posts"*
1. `c6` == 5
> print text=txn-rollback-ok
2. *
> print text=txn-rollback-fail
@@ -0,0 +1,62 @@
---
title: web middleware live server
description: Offline-assembled app that listens with CORS/security/compress/body_limit/json.
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
# main
<!-- load web plugin so ABI primitives are registered -->
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
`schema` =
| 字段 | 类型 |
|------|------|
| title | text |
| body | text |
*store = > web.db url="sqlite:web-fixtures/data/mw-live.db"*
> `store`.init name=articles fields=`schema`
> `store`.exec sql="DELETE FROM articles"
`rows` =
| title | body |
|-------|------|
| Hello | first post |
> `store`.insert table=articles rows=`rows`
*pg = > web.page title="live"*
*app = > web.app page=`pg` db=`store` port=18099*
`cors` =
| 允许来源 | 方法 | 头 | 暴露头 | 凭证 |
|----------|------|----|--------|------|
| https://a.example | GET,POST | Content-Type | X-Total | true |
`sec` =
| 头 | 值 |
|----|----|
| X-Frame-Options | DENY |
| X-Content-Type-Options | nosniff |
`api` =
| 路径 | 方法 | 表 | 条件 | 排序 | 上限 |
|------|------|----|----|----|------|
| /api/posts | GET | articles | | | 10 |
| /api/publish | POST | articles | | | 10 |
*app = > `app`.configure cors=`cors` security=`sec` compress=True body_limit=1000 json=`api`*
> `app`.listen
+147
View File
@@ -0,0 +1,147 @@
---
title: web middleware smoke
description: Offline assemble of CORS/security/compress/json middleware (no listen).
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
# main
<!-- load web plugin so ABI primitives are registered -->
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
<!-- CORS table: |允许来源|方法|头|暴露头|凭证| -->
`cors` =
| 允许来源 | 方法 | 头 | 暴露头 | 凭证 |
|----------|------|----|--------|------|
| https://a.example | GET,POST | Content-Type,Authorization | X-Total | true |
| https://b.example | GET | Content-Type | | |
<!-- security table: |头|值| -->
`sec` =
| 头 | 值 |
|----|----|
| X-Frame-Options | DENY |
| X-Content-Type-Options | nosniff |
| Content-Security-Policy | default-src 'self' |
<!-- json routes table: |路径|方法|表|条件|排序|上限| -->
`api` =
| 路径 | 方法 | 表 | 条件 | 排序 | 上限 |
|------|------|----|----|----|------|
| /api/posts | GET | articles | | "-created_at" | 20 |
| /api/publish | POST | articles | | | 10 |
<!-- assemble via app.configure -->
*pg = > web.page title="mw"*
*app = > web.app page=`pg`*
*app = > `app`.configure cors=`cors` security=`sec` compress=True body_limit=1048576 json=`api`*
*mw = app[^middleware]*
<!-- cors -->
*cobj = mw[^cors]*
*origins = cobj[^allow_origins]*
*o0 = origins[^1]*
1. `o0` == "https://a.example"
> print text=cors-origin-ok
2. *
> print text=cors-origin-fail
*methods = cobj[^methods]*
*m1 = methods[^2]*
1. `m1` == "POST"
> print text=cors-method-ok
2. *
> print text=cors-method-fail
*expose = cobj[^expose_headers]*
*e0 = expose[^1]*
1. `e0` == "X-Total"
> print text=cors-expose-ok
2. *
> print text=cors-expose-fail
*cred = cobj[^credentials]*
1. `cred`
> print text=cors-cred-ok
2. *
> print text=cors-cred-fail
<!-- security -->
*secmap = mw[^security]*
*frame = secmap[^X-Frame-Options]*
1. `frame` == "DENY"
> print text=security-frame-ok
2. *
> print text=security-frame-fail
*csp = secmap[^Content-Security-Policy]*
1. `csp` == "default-src 'self'"
> print text=security-csp-ok
2. *
> print text=security-csp-fail
<!-- compress / body_limit -->
*comp = mw[^compress]*
1. `comp`
> print text=compress-ok
2. *
> print text=compress-fail
*bl = mw[^body_limit]*
1. `bl` == 1048576
> print text=body-limit-ok
2. *
> print text=body-limit-fail
<!-- json routes -->
*jmap = mw[^json_routes]*
*posts = jmap[^api/posts]*
*pok = posts[^method]*
1. `pok` == "GET"
> print text=json-get-ok
2. *
> print text=json-get-fail
*ptable = posts[^table]*
1. `ptable` == "articles"
> print text=json-table-ok
2. *
> print text=json-table-fail
*porder = posts[^order]*
1. `porder` == "-created_at"
> print text=json-order-ok
2. *
> print text=json-order-fail
*plimit = posts[^limit]*
1. `plimit` == 20
> print text=json-limit-ok
2. *
> print text=json-limit-fail
*pub = jmap[^api/publish]*
*pmethod = pub[^method]*
1. `pmethod` == "POST"
> print text=json-post-ok
2. *
> print text=json-post-fail
+48
View File
@@ -0,0 +1,48 @@
---
title: web security smoke (W3)
description: Password hash ABI + legacy plaintext login (offline).
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
# main
<!-- load web plugin -->
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
<!-- argon2 hash must be non-empty PHC string -->
*hash = > web_password_hash password="secret"*
*h = hash[^hash]*
1. `h` != ""
> print text=hash-ok
2. *
> print text=hash-fail
<!-- legacy plaintext login still works for dev/gold -->
`users` =
| 行 | 用户 | 密码 |
|----|------|------|
| 1 | admin | secret |
*login = > web_auth_login username="admin" password="secret" users=`users` session_ttl=120*
*lok = login[^ok]*
1. `lok`
> print text=auth-login-ok
2. *
> print text=auth-login-fail
*bad = > web_auth_login username="admin" password="wrong" users=`users` session_ttl=120*
*bok = bad[^ok]*
1. `bok`
> print text=auth-bad-should-fail
2. *
> print text=auth-bad-reject-ok