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
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
+213
@@ -1542,6 +1542,219 @@ custom-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_middleware_smoke() {
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
assert_out(
|
||||
"tests/ext/web-middleware-smoke.mq.md",
|
||||
"cors-origin-ok
|
||||
cors-method-ok
|
||||
cors-expose-ok
|
||||
cors-cred-ok
|
||||
security-frame-ok
|
||||
security-csp-ok
|
||||
compress-ok
|
||||
body-limit-ok
|
||||
json-get-ok
|
||||
json-table-ok
|
||||
json-order-ok
|
||||
json-limit-ok
|
||||
json-post-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_middleware_live() {
|
||||
// Boots a real HTTP server and verifies CORS / security headers /
|
||||
// gzip / JSON API / body limit over the wire.
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
|
||||
let script = "tests/ext/web-middleware-live-server.mq.md";
|
||||
let bin = env!("CARGO_BIN_EXE_marqdo");
|
||||
let mut child = Command::new(bin)
|
||||
.args(["run", script])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("spawn live server");
|
||||
|
||||
// Wait for the listener to accept connections (up to ~10s).
|
||||
let base = "http://127.0.0.1:18099";
|
||||
let ready = std::time::Instant::now();
|
||||
loop {
|
||||
if ready.elapsed().as_secs() > 10 {
|
||||
let _ = child.kill();
|
||||
panic!("live server did not start in time");
|
||||
}
|
||||
let out = Command::new("curl")
|
||||
.args(["-s", "-o", "/dev/null", "-w", "%{http_code}", &format!("{base}/")])
|
||||
.output()
|
||||
.expect("curl probe");
|
||||
if String::from_utf8_lossy(&out.stdout) == "200" {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(300));
|
||||
}
|
||||
|
||||
let code = |args: &[&str]| {
|
||||
Command::new("curl")
|
||||
.args(args)
|
||||
.output()
|
||||
.expect("curl")
|
||||
};
|
||||
|
||||
// JSON API: rows are served as application/json.
|
||||
let out = code(&["-s", &format!("{base}/api/posts")]);
|
||||
let body = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
body.contains("\"title\":\"Hello\"") && body.contains("\"rows\""),
|
||||
"json api body={body}"
|
||||
);
|
||||
|
||||
// CORS on the JSON endpoint (matching origin → allow headers).
|
||||
let out = code(&[
|
||||
"-s", "-D", "-", "-o", "/dev/null", &format!("{base}/api/posts"),
|
||||
"-H", "Origin: https://a.example",
|
||||
]);
|
||||
let headers = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
headers.contains("access-control-allow-origin: https://a.example"),
|
||||
"cors allow-origin headers={headers}"
|
||||
);
|
||||
assert!(
|
||||
headers.contains("access-control-allow-credentials: true"),
|
||||
"cors credentials headers={headers}"
|
||||
);
|
||||
assert!(
|
||||
headers.contains("access-control-expose-headers: x-total"),
|
||||
"cors expose headers={headers}"
|
||||
);
|
||||
|
||||
// OPTIONS preflight.
|
||||
let out = code(&[
|
||||
"-s", "-D", "-", "-o", "/dev/null", "-X", "OPTIONS",
|
||||
&format!("{base}/api/posts"),
|
||||
"-H", "Origin: https://a.example",
|
||||
"-H", "Access-Control-Request-Method: GET",
|
||||
]);
|
||||
let headers = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
headers.contains("access-control-allow-methods: GET"),
|
||||
"cors preflight headers={headers}"
|
||||
);
|
||||
assert!(
|
||||
headers.contains("access-control-allow-origin"),
|
||||
"cors preflight origin headers={headers}"
|
||||
);
|
||||
|
||||
// Security response headers on a page render.
|
||||
let out = code(&["-s", "-D", "-", "-o", "/dev/null", &format!("{base}/")]);
|
||||
let headers = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
headers.contains("x-frame-options: DENY"),
|
||||
"security headers={headers}"
|
||||
);
|
||||
assert!(
|
||||
headers.contains("x-content-type-options: nosniff"),
|
||||
"security headers={headers}"
|
||||
);
|
||||
|
||||
// gzip compression.
|
||||
let out = code(&[
|
||||
"-s", "-D", "-", "-o", "/dev/null", &format!("{base}/api/posts"),
|
||||
"-H", "Accept-Encoding: gzip",
|
||||
]);
|
||||
let headers = String::from_utf8_lossy(&out.stdout);
|
||||
assert!(
|
||||
headers.contains("content-encoding: gzip"),
|
||||
"gzip headers={headers}"
|
||||
);
|
||||
|
||||
// Body limit: small POST ok, large POST → 413.
|
||||
let out = code(&[
|
||||
"-s", "-o", "/dev/null", "-w", "%{http_code}", "-X", "POST",
|
||||
&format!("{base}/api/publish"), "-H", "Content-Type: application/json",
|
||||
"-d", r#"{"title":"t1","body":"b1"}"#,
|
||||
]);
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&out.stdout).trim(),
|
||||
"200",
|
||||
"small POST should pass body limit"
|
||||
);
|
||||
let big = "x".repeat(2000);
|
||||
let out = code(&[
|
||||
"-s", "-o", "/dev/null", "-w", "%{http_code}", "-X", "POST",
|
||||
&format!("{base}/api/publish"), "-H", "Content-Type: application/json",
|
||||
"-d", &big,
|
||||
]);
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&out.stdout).trim(),
|
||||
"413",
|
||||
"large POST should exceed body limit"
|
||||
);
|
||||
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_security_smoke() {
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
assert_out(
|
||||
"tests/ext/web-security-smoke.mq.md",
|
||||
"hash-ok
|
||||
auth-login-ok
|
||||
auth-bad-reject-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_web_db_w2_smoke() {
|
||||
// W2 data layer: transactions, connection pooling, pagination, query
|
||||
// expressiveness (IN/BETWEEN/OR), and row counting.
|
||||
let status = Command::new("cargo")
|
||||
.args(["build", "-p", "marqdo_plugin_web"])
|
||||
.current_dir(env!("CARGO_MANIFEST_DIR"))
|
||||
.status()
|
||||
.expect("build web plugin");
|
||||
assert!(status.success(), "failed to build marqdo_plugin_web");
|
||||
|
||||
let db = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/ext/web-fixtures/data/w2-smoke.db");
|
||||
for suffix in ["", "-wal", "-shm"] {
|
||||
let _ = std::fs::remove_file(format!("{}{}", db.display(), suffix));
|
||||
}
|
||||
assert_out(
|
||||
"tests/ext/web-db-w2-smoke.mq.md",
|
||||
"count-ok
|
||||
count-where-ok
|
||||
page-ok
|
||||
page2-ok
|
||||
in-ok
|
||||
between-ok
|
||||
or-ok
|
||||
query-ok
|
||||
txn-commit-ok
|
||||
txn-rollback-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_quantum_bell_smoke() {
|
||||
let status = Command::new("cargo")
|
||||
|
||||
Reference in new issue
Block a user