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:
1 parent
da9fc75b1e
commit
be6223f1fb
22 files changed
+3081
-343
No files matched your search
+153
-7
@@ -139,9 +139,116 @@ Style tables live as `##` exports in site style modules; compose resolves them b
|
||||
## insert
|
||||
+ `table`
|
||||
+ `rows`
|
||||
+ `txn`=None
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_insert url=`url` table=`table` rows=`rows`**
|
||||
**> web_db_insert url=`url` table=`table` rows=`rows` txn=`txn`**
|
||||
|
||||
## select
|
||||
+ `table`
|
||||
+ `where`=None
|
||||
+ `limit`=200
|
||||
+ `order`=None
|
||||
+ `txn`=None
|
||||
|
||||
Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作|值|` (`=` `!=` `>` `>=` `<` `<=` `like` `in` `between` `is null`; add `|或|` = `是` to join a row with `OR`). `order` is a column name with optional `-` prefix for descending (`"created_at"`, `"-created_at"`), comma-separated for multiple keys. Pass `txn` to read inside an open transaction. For pages (with a total), use `paginate`.
|
||||
|
||||
*url = self[^url]*
|
||||
*r = > web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order` offset=None txn=`txn`*
|
||||
**r[^rows]**
|
||||
|
||||
## paginate
|
||||
+ `table`
|
||||
+ `where`=None
|
||||
+ `limit`=200
|
||||
+ `order`=None
|
||||
+ `跳过`=0
|
||||
+ `txn`=None
|
||||
|
||||
Like `select` but returns `{ rows, total }` — the total counts rows matching `where` regardless of `limit`/`跳过`, so you can render `上一页 / 下一页`. Set `跳过` to the number of rows to skip (e.g. page 2 with 10 per page ⇒ `跳过`=10).
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order` offset=`跳过` txn=`txn`**
|
||||
|
||||
## get
|
||||
+ `table`
|
||||
+ `id`
|
||||
+ `txn`=None
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_get url=`url` table=`table` id=`id` txn=`txn`**
|
||||
|
||||
## update
|
||||
+ `table`
|
||||
+ `id`
|
||||
+ `row`
|
||||
+ `txn`=None
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_update url=`url` table=`table` id=`id` row=`row` txn=`txn`**
|
||||
|
||||
## delete
|
||||
+ `table`
|
||||
+ `id`
|
||||
+ `txn`=None
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_delete url=`url` table=`table` id=`id` txn=`txn`**
|
||||
|
||||
## exec
|
||||
+ `sql`
|
||||
+ `args`=None
|
||||
+ `txn`=None
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_exec url=`url` sql=`sql` args=`args` txn=`txn`**
|
||||
|
||||
## query
|
||||
+ `sql`
|
||||
+ `args`=None
|
||||
+ `txn`=None
|
||||
|
||||
Run bare SQL and return the result set — count / join / group / subqueries. Returns `{ rows, count }`.
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_query url=`url` sql=`sql` args=`args` txn=`txn`**
|
||||
|
||||
## count
|
||||
+ `table`
|
||||
+ `where`=None
|
||||
+ `txn`=None
|
||||
|
||||
Count rows matching a `where` filter (same syntax as `select`). Returns a number.
|
||||
|
||||
*url = self[^url]*
|
||||
*r = > web_db_count url=`url` table=`table` where=`where` txn=`txn`*
|
||||
**r[^count]**
|
||||
|
||||
## 事务
|
||||
|
||||
Begin a transaction: borrows the pooled connection exclusively and returns a
|
||||
`txn` handle. Write inside it, then `提交` (commit) or `回滚` (roll back).
|
||||
Every statement runs on the same connection, so a batch is atomic.
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_begin url=`url`**
|
||||
|
||||
# txn
|
||||
+ `txn`
|
||||
+ `url`
|
||||
|
||||
A transaction handle from `db.事务`. All CRUD here runs on the transaction's
|
||||
connection; finish with `提交` or `回滚`.
|
||||
|
||||
**self**
|
||||
|
||||
## insert
|
||||
+ `table`
|
||||
+ `rows`
|
||||
|
||||
*url = self[^url]*
|
||||
*txn = self[^txn]*
|
||||
**> web_db_insert url=`url` table=`table` rows=`rows` txn=`txn`**
|
||||
|
||||
## select
|
||||
+ `table`
|
||||
@@ -149,10 +256,11 @@ Style tables live as `##` exports in site style modules; compose resolves them b
|
||||
+ `limit`=200
|
||||
+ `order`=None
|
||||
|
||||
Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作|值|` (`=` `!=` `>` `>=` `<` `<=` `like`). `order` is a column name with optional `-` prefix for descending (`"created_at"`, `"-created_at"`), comma-separated for multiple keys.
|
||||
Same filters as `db.select`; runs inside the transaction.
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order`**
|
||||
*txn = self[^txn]*
|
||||
*r = > web_db_select url=`url` table=`table` where=`where` limit=`limit` order=`order` offset=None txn=`txn`*
|
||||
**r[^rows]**
|
||||
|
||||
## get
|
||||
@@ -160,7 +268,8 @@ Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作
|
||||
+ `id`
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_get url=`url` table=`table` id=`id`**
|
||||
*txn = self[^txn]*
|
||||
**> web_db_get url=`url` table=`table` id=`id` txn=`txn`**
|
||||
|
||||
## update
|
||||
+ `table`
|
||||
@@ -168,21 +277,38 @@ Simple filters: one-row map of column→value (AND `=`), or rows `|字段|操作
|
||||
+ `row`
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_update url=`url` table=`table` id=`id` row=`row`**
|
||||
*txn = self[^txn]*
|
||||
**> web_db_update url=`url` table=`table` id=`id` row=`row` txn=`txn`**
|
||||
|
||||
## delete
|
||||
+ `table`
|
||||
+ `id`
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_delete url=`url` table=`table` id=`id`**
|
||||
*txn = self[^txn]*
|
||||
**> web_db_delete url=`url` table=`table` id=`id` txn=`txn`**
|
||||
|
||||
## exec
|
||||
+ `sql`
|
||||
+ `args`=None
|
||||
|
||||
*url = self[^url]*
|
||||
**> web_db_exec url=`url` sql=`sql` args=`args`**
|
||||
*txn = self[^txn]*
|
||||
**> web_db_exec url=`url` sql=`sql` args=`args` txn=`txn`**
|
||||
|
||||
## 提交
|
||||
|
||||
Commit the transaction and return its connection to the pool.
|
||||
|
||||
*txn = self[^txn]*
|
||||
**> web_db_commit txn=`txn`**
|
||||
|
||||
## 回滚
|
||||
|
||||
Roll the transaction back (undo every write) and return its connection.
|
||||
|
||||
*txn = self[^txn]*
|
||||
**> web_db_rollback txn=`txn`**
|
||||
|
||||
# form
|
||||
+ `table`=None
|
||||
@@ -258,6 +384,19 @@ Serve files from `dir` under `mount` (default `/static`). Path is resolved from
|
||||
|
||||
**> web_app_static app=`self` dir=`dir` mount=`mount`**
|
||||
|
||||
## configure
|
||||
+ `cors`=None
|
||||
+ `security`=None
|
||||
+ `compress`=None
|
||||
+ `body_limit`=None
|
||||
+ `json`=None
|
||||
|
||||
Each capability is declared as a data table and assembled at listen time. The `cors` parameter takes a `|允许来源|方法|头|暴露头|凭证|` table (one row per origin; an empty `允许来源` column means any origin). The `security` parameter takes a `|头|值|` response-header table (e.g. `X-Frame-Options`, `Content-Security-Policy`, `X-Content-Type-Options`, `Referrer-Policy`, `Strict-Transport-Security`). Set `compress` to `True` to gzip response bodies. `body_limit` is the max request body bytes (e.g. `1048576`). The `json` parameter takes a `|路径|方法|表|条件|排序|上限|` table of JSON API endpoints backed by DB queries (each returns `application/json`).
|
||||
|
||||
Tables stay as data; `configure` assembles them. 配置即数据、装配即函数.
|
||||
|
||||
**> web_app_middleware app=`self` cors=`cors` security=`security` compress=`compress` body_limit=`body_limit` json_routes=`json`**
|
||||
|
||||
## listen
|
||||
|
||||
Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes), `/_form/{id}` (from mounts + page embeds), optional `/static` (or custom mount), and optional `/admin`.
|
||||
@@ -313,6 +452,13 @@ Destroy the session.
|
||||
|
||||
**> web_auth_logout session_id=`session_id`**
|
||||
|
||||
## hash_password
|
||||
+ `password`
|
||||
|
||||
Hash a plaintext password for storage in admin user tables (argon2id). Store the returned `hash` in the `password` column; login verifies automatically.
|
||||
|
||||
**> web_password_hash password=`password`**
|
||||
|
||||
# ws
|
||||
+ `timeout_sec`=30
|
||||
|
||||
|
||||
Reference in new issue
Block a user