tina4-nodejs 3.13.159 → 3.13.160
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +2 -2
- package/package.json +1 -1
- package/packages/cli/dist/bin.js +58 -20
- package/packages/core/dist/index.js +58 -20
- package/packages/core/src/authGate.ts +37 -24
- package/packages/core/src/middleware.ts +18 -6
- package/packages/core/src/response.ts +60 -2
- package/packages/core/src/server.ts +8 -2
- package/packages/orm/dist/index.js +58 -20
- package/types/core/src/authGate.d.ts +22 -7
- package/types/core/src/response.d.ts +17 -1
package/CLAUDE.md
CHANGED
|
@@ -14,13 +14,13 @@ Even if the skill text is not currently loaded, these are non-negotiable:
|
|
|
14
14
|
|
|
15
15
|
The full discipline lives in `.claude/skills/tina4-maintainer/SKILL.md`; this block is the always-on floor.
|
|
16
16
|
|
|
17
|
-
# CLAUDE.md - AI Developer Guide for tina4-nodejs (v3.13.
|
|
17
|
+
# CLAUDE.md - AI Developer Guide for tina4-nodejs (v3.13.160)
|
|
18
18
|
|
|
19
19
|
> This file helps AI assistants (Claude, Copilot, Cursor, etc.) understand and work on this codebase effectively.
|
|
20
20
|
|
|
21
21
|
## What This Project Is
|
|
22
22
|
|
|
23
|
-
Tina4 for Node.js/TypeScript v3.13.
|
|
23
|
+
Tina4 for Node.js/TypeScript v3.13.160 - The Intelligent Native Application 4ramework. A convention-over-configuration structural paradigm. The developer writes TypeScript; Tina4 is invisible infrastructure.
|
|
24
24
|
|
|
25
25
|
The philosophy: zero ceremony, batteries included, file system as source of truth.
|
|
26
26
|
|
package/package.json
CHANGED
package/packages/cli/dist/bin.js
CHANGED
|
@@ -22192,6 +22192,41 @@ function negotiatedErrorBody(code, message, requestId) {
|
|
|
22192
22192
|
body.request_id = requestId;
|
|
22193
22193
|
return body;
|
|
22194
22194
|
}
|
|
22195
|
+
async function renderNegotiatedError(req2, res, code, message) {
|
|
22196
|
+
const { Log: Log2 } = await Promise.resolve().then(() => (init_logger(), logger_exports));
|
|
22197
|
+
const requestId = Log2.getRequestId() ?? "";
|
|
22198
|
+
const writeJson = () => {
|
|
22199
|
+
const body = negotiatedErrorBody(code, message, requestId);
|
|
22200
|
+
res.raw.statusCode = code;
|
|
22201
|
+
res.raw.setHeader("Content-Type", "application/json");
|
|
22202
|
+
res.raw.end(JSON.stringify(body));
|
|
22203
|
+
};
|
|
22204
|
+
if (wantsJson(req2)) {
|
|
22205
|
+
writeJson();
|
|
22206
|
+
return;
|
|
22207
|
+
}
|
|
22208
|
+
const data = { path: req2?.path ?? "", error_message: "", request_id: requestId, status_code: code };
|
|
22209
|
+
const template = `errors/${code}.twig`;
|
|
22210
|
+
let html = null;
|
|
22211
|
+
try {
|
|
22212
|
+
html = (await getFrond()).render(template, data);
|
|
22213
|
+
} catch {
|
|
22214
|
+
}
|
|
22215
|
+
if (!html) {
|
|
22216
|
+
try {
|
|
22217
|
+
const fw = await getFrameworkFrond();
|
|
22218
|
+
html = fw ? fw.render(template, data) : null;
|
|
22219
|
+
} catch {
|
|
22220
|
+
html = null;
|
|
22221
|
+
}
|
|
22222
|
+
}
|
|
22223
|
+
if (html) {
|
|
22224
|
+
res.raw.writeHead(code, { "Content-Type": "text/html; charset=utf-8" });
|
|
22225
|
+
res.raw.end(html);
|
|
22226
|
+
return;
|
|
22227
|
+
}
|
|
22228
|
+
writeJson();
|
|
22229
|
+
}
|
|
22195
22230
|
var HEADER_TOKEN, _frondCache, _defaultTemplatesDir, _globalFrond, _frameworkFrond, ERROR_CODE_NAMES;
|
|
22196
22231
|
var init_response = __esm({
|
|
22197
22232
|
"../core/src/response.ts"() {
|
|
@@ -22203,6 +22238,7 @@ var init_response = __esm({
|
|
|
22203
22238
|
_globalFrond = null;
|
|
22204
22239
|
_frameworkFrond = null;
|
|
22205
22240
|
ERROR_CODE_NAMES = {
|
|
22241
|
+
401: "UNAUTHORIZED",
|
|
22206
22242
|
403: "FORBIDDEN",
|
|
22207
22243
|
404: "NOT_FOUND",
|
|
22208
22244
|
405: "METHOD_NOT_ALLOWED",
|
|
@@ -22271,7 +22307,7 @@ async function interpretHookResult(result, req2, res) {
|
|
|
22271
22307
|
}
|
|
22272
22308
|
return [req2, res, false];
|
|
22273
22309
|
}
|
|
22274
|
-
function middleware500(res, mwClass, methodName, error2) {
|
|
22310
|
+
async function middleware500(res, mwClass, methodName, error2, req2) {
|
|
22275
22311
|
const clsName = mwClass?.name ?? mwClass?.constructor?.name ?? "Middleware";
|
|
22276
22312
|
const err = error2;
|
|
22277
22313
|
const type2 = err?.name ?? error2?.constructor?.name ?? "Error";
|
|
@@ -22280,6 +22316,10 @@ function middleware500(res, mwClass, methodName, error2) {
|
|
|
22280
22316
|
Log.error(`Middleware ${clsName}.${methodName} raised ${type2}: ${message}`);
|
|
22281
22317
|
} catch {
|
|
22282
22318
|
}
|
|
22319
|
+
if (req2 && res?.raw && typeof res.raw.writeHead === "function" && !res.raw.writableEnded) {
|
|
22320
|
+
await renderNegotiatedError(req2, res, 500, "Internal Server Error");
|
|
22321
|
+
return res;
|
|
22322
|
+
}
|
|
22283
22323
|
if (typeof res.json === "function") {
|
|
22284
22324
|
res.json({ error: "Internal Server Error", status: 500 }, 500);
|
|
22285
22325
|
} else if (typeof res === "function") {
|
|
@@ -22580,7 +22620,7 @@ var init_middleware = __esm({
|
|
|
22580
22620
|
res = nextRes;
|
|
22581
22621
|
if (stop2) return [req2, res, false];
|
|
22582
22622
|
} catch (error2) {
|
|
22583
|
-
res = middleware500(res, cls, method, error2);
|
|
22623
|
+
res = await middleware500(res, cls, method, error2, req2);
|
|
22584
22624
|
return [req2, res, false];
|
|
22585
22625
|
}
|
|
22586
22626
|
if (res.raw.statusCode >= 400 || res.raw.writableEnded) {
|
|
@@ -22624,7 +22664,7 @@ var init_middleware = __esm({
|
|
|
22624
22664
|
res = nextRes;
|
|
22625
22665
|
if (stop2) return [req2, res];
|
|
22626
22666
|
} catch (error2) {
|
|
22627
|
-
res = middleware500(res, cls, method, error2);
|
|
22667
|
+
res = await middleware500(res, cls, method, error2, req2);
|
|
22628
22668
|
continue;
|
|
22629
22669
|
}
|
|
22630
22670
|
}
|
|
@@ -23628,7 +23668,7 @@ var init_ssoIdentity = __esm({
|
|
|
23628
23668
|
// ../core/src/authGate.ts
|
|
23629
23669
|
function enforceRouteAuth(req2, res, match) {
|
|
23630
23670
|
if (!(match.secure === true && match.noAuth !== true)) {
|
|
23631
|
-
return
|
|
23671
|
+
return null;
|
|
23632
23672
|
}
|
|
23633
23673
|
const authHeader = req2.headers.authorization ?? "";
|
|
23634
23674
|
const headerToken = authHeader.startsWith("Bearer ") ? authHeader.slice(7) : "";
|
|
@@ -23658,7 +23698,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
23658
23698
|
const identity = liveSessionIdentity(req2.session?.get?.("_tina4_sso"));
|
|
23659
23699
|
if (identity) {
|
|
23660
23700
|
req2.user = identity;
|
|
23661
|
-
return rbacForbidden(match, identity
|
|
23701
|
+
return rbacForbidden(match, identity);
|
|
23662
23702
|
}
|
|
23663
23703
|
const sessionToken = req2.session?.get?.("token");
|
|
23664
23704
|
if (sessionToken) {
|
|
@@ -23671,9 +23711,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
23671
23711
|
}
|
|
23672
23712
|
}
|
|
23673
23713
|
if (!payload) {
|
|
23674
|
-
|
|
23675
|
-
res.raw.end(JSON.stringify({ error: "Unauthorized" }));
|
|
23676
|
-
return true;
|
|
23714
|
+
return { status: 401, message: "Unauthorized" };
|
|
23677
23715
|
}
|
|
23678
23716
|
req2.user = payload;
|
|
23679
23717
|
if (tokenSource === "body") {
|
|
@@ -23682,7 +23720,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
23682
23720
|
res.header("FreshToken", fresh);
|
|
23683
23721
|
}
|
|
23684
23722
|
}
|
|
23685
|
-
return rbacForbidden(match, req2.user
|
|
23723
|
+
return rbacForbidden(match, req2.user);
|
|
23686
23724
|
}
|
|
23687
23725
|
function rbacClaimList(subject, key, legacy) {
|
|
23688
23726
|
const coerce = (v) => {
|
|
@@ -23699,27 +23737,23 @@ function rbacPermGranted(granted, required) {
|
|
|
23699
23737
|
(g) => g === "*" || g === required || g.endsWith(".*") && required.startsWith(g.slice(0, -1))
|
|
23700
23738
|
);
|
|
23701
23739
|
}
|
|
23702
|
-
function rbacForbidden(match, payload
|
|
23740
|
+
function rbacForbidden(match, payload) {
|
|
23703
23741
|
const requiredRoles = match.requiredRoles ?? [];
|
|
23704
23742
|
const requiredPerms = match.requiredPerms ?? [];
|
|
23705
23743
|
if (requiredRoles.length === 0 && requiredPerms.length === 0) {
|
|
23706
|
-
return
|
|
23744
|
+
return null;
|
|
23707
23745
|
}
|
|
23708
23746
|
const subject = payload && typeof payload === "object" ? payload : {};
|
|
23747
|
+
const forbidden = { status: 403, message: "Forbidden" };
|
|
23709
23748
|
const roles = rbacClaimList(subject, "roles", "role");
|
|
23710
23749
|
for (const group of requiredRoles) {
|
|
23711
|
-
if (!group.some((r) => roles.includes(r))) return
|
|
23750
|
+
if (!group.some((r) => roles.includes(r))) return forbidden;
|
|
23712
23751
|
}
|
|
23713
23752
|
const perms = rbacClaimList(subject, "permissions");
|
|
23714
23753
|
for (const group of requiredPerms) {
|
|
23715
|
-
if (!group.some((p) => rbacPermGranted(perms, p))) return
|
|
23754
|
+
if (!group.some((p) => rbacPermGranted(perms, p))) return forbidden;
|
|
23716
23755
|
}
|
|
23717
|
-
return
|
|
23718
|
-
}
|
|
23719
|
-
function writeForbidden(res) {
|
|
23720
|
-
res.raw.writeHead(403, { "Content-Type": "application/json" });
|
|
23721
|
-
res.raw.end(JSON.stringify({ error: "Forbidden" }));
|
|
23722
|
-
return true;
|
|
23756
|
+
return null;
|
|
23723
23757
|
}
|
|
23724
23758
|
var init_authGate = __esm({
|
|
23725
23759
|
"../core/src/authGate.ts"() {
|
|
@@ -43789,7 +43823,11 @@ async function runMatchedRoute(ctx) {
|
|
|
43789
43823
|
const { req: req2, res, match, postMatchMiddleware } = ctx;
|
|
43790
43824
|
req2.params = match.params;
|
|
43791
43825
|
if (await runGlobalMiddlewarePass(postMatchMiddleware, req2, res)) return;
|
|
43792
|
-
|
|
43826
|
+
const authReject = enforceRouteAuth(req2, res, match);
|
|
43827
|
+
if (authReject) {
|
|
43828
|
+
await renderNegotiatedError(req2, res, authReject.status, authReject.message);
|
|
43829
|
+
return;
|
|
43830
|
+
}
|
|
43793
43831
|
const routeMiddlewareClasses = (match.middlewares ?? []).filter(isMiddlewareClass);
|
|
43794
43832
|
let handlerSkipped = false;
|
|
43795
43833
|
if (match.middlewares && match.middlewares.length > 0) {
|
|
@@ -22191,6 +22191,41 @@ function negotiatedErrorBody(code, message, requestId) {
|
|
|
22191
22191
|
body.request_id = requestId;
|
|
22192
22192
|
return body;
|
|
22193
22193
|
}
|
|
22194
|
+
async function renderNegotiatedError(req2, res, code, message) {
|
|
22195
|
+
const { Log: Log2 } = await Promise.resolve().then(() => (init_logger(), logger_exports));
|
|
22196
|
+
const requestId = Log2.getRequestId() ?? "";
|
|
22197
|
+
const writeJson = () => {
|
|
22198
|
+
const body = negotiatedErrorBody(code, message, requestId);
|
|
22199
|
+
res.raw.statusCode = code;
|
|
22200
|
+
res.raw.setHeader("Content-Type", "application/json");
|
|
22201
|
+
res.raw.end(JSON.stringify(body));
|
|
22202
|
+
};
|
|
22203
|
+
if (wantsJson(req2)) {
|
|
22204
|
+
writeJson();
|
|
22205
|
+
return;
|
|
22206
|
+
}
|
|
22207
|
+
const data = { path: req2?.path ?? "", error_message: "", request_id: requestId, status_code: code };
|
|
22208
|
+
const template = `errors/${code}.twig`;
|
|
22209
|
+
let html = null;
|
|
22210
|
+
try {
|
|
22211
|
+
html = (await getFrond()).render(template, data);
|
|
22212
|
+
} catch {
|
|
22213
|
+
}
|
|
22214
|
+
if (!html) {
|
|
22215
|
+
try {
|
|
22216
|
+
const fw = await getFrameworkFrond();
|
|
22217
|
+
html = fw ? fw.render(template, data) : null;
|
|
22218
|
+
} catch {
|
|
22219
|
+
html = null;
|
|
22220
|
+
}
|
|
22221
|
+
}
|
|
22222
|
+
if (html) {
|
|
22223
|
+
res.raw.writeHead(code, { "Content-Type": "text/html; charset=utf-8" });
|
|
22224
|
+
res.raw.end(html);
|
|
22225
|
+
return;
|
|
22226
|
+
}
|
|
22227
|
+
writeJson();
|
|
22228
|
+
}
|
|
22194
22229
|
var HEADER_TOKEN, _frondCache, _defaultTemplatesDir, _globalFrond, _frameworkFrond, ERROR_CODE_NAMES;
|
|
22195
22230
|
var init_response = __esm({
|
|
22196
22231
|
"src/response.ts"() {
|
|
@@ -22202,6 +22237,7 @@ var init_response = __esm({
|
|
|
22202
22237
|
_globalFrond = null;
|
|
22203
22238
|
_frameworkFrond = null;
|
|
22204
22239
|
ERROR_CODE_NAMES = {
|
|
22240
|
+
401: "UNAUTHORIZED",
|
|
22205
22241
|
403: "FORBIDDEN",
|
|
22206
22242
|
404: "NOT_FOUND",
|
|
22207
22243
|
405: "METHOD_NOT_ALLOWED",
|
|
@@ -22270,7 +22306,7 @@ async function interpretHookResult(result, req2, res) {
|
|
|
22270
22306
|
}
|
|
22271
22307
|
return [req2, res, false];
|
|
22272
22308
|
}
|
|
22273
|
-
function middleware500(res, mwClass, methodName, error2) {
|
|
22309
|
+
async function middleware500(res, mwClass, methodName, error2, req2) {
|
|
22274
22310
|
const clsName = mwClass?.name ?? mwClass?.constructor?.name ?? "Middleware";
|
|
22275
22311
|
const err = error2;
|
|
22276
22312
|
const type2 = err?.name ?? error2?.constructor?.name ?? "Error";
|
|
@@ -22279,6 +22315,10 @@ function middleware500(res, mwClass, methodName, error2) {
|
|
|
22279
22315
|
Log.error(`Middleware ${clsName}.${methodName} raised ${type2}: ${message}`);
|
|
22280
22316
|
} catch {
|
|
22281
22317
|
}
|
|
22318
|
+
if (req2 && res?.raw && typeof res.raw.writeHead === "function" && !res.raw.writableEnded) {
|
|
22319
|
+
await renderNegotiatedError(req2, res, 500, "Internal Server Error");
|
|
22320
|
+
return res;
|
|
22321
|
+
}
|
|
22282
22322
|
if (typeof res.json === "function") {
|
|
22283
22323
|
res.json({ error: "Internal Server Error", status: 500 }, 500);
|
|
22284
22324
|
} else if (typeof res === "function") {
|
|
@@ -22579,7 +22619,7 @@ var init_middleware = __esm({
|
|
|
22579
22619
|
res = nextRes;
|
|
22580
22620
|
if (stop2) return [req2, res, false];
|
|
22581
22621
|
} catch (error2) {
|
|
22582
|
-
res = middleware500(res, cls, method, error2);
|
|
22622
|
+
res = await middleware500(res, cls, method, error2, req2);
|
|
22583
22623
|
return [req2, res, false];
|
|
22584
22624
|
}
|
|
22585
22625
|
if (res.raw.statusCode >= 400 || res.raw.writableEnded) {
|
|
@@ -22623,7 +22663,7 @@ var init_middleware = __esm({
|
|
|
22623
22663
|
res = nextRes;
|
|
22624
22664
|
if (stop2) return [req2, res];
|
|
22625
22665
|
} catch (error2) {
|
|
22626
|
-
res = middleware500(res, cls, method, error2);
|
|
22666
|
+
res = await middleware500(res, cls, method, error2, req2);
|
|
22627
22667
|
continue;
|
|
22628
22668
|
}
|
|
22629
22669
|
}
|
|
@@ -23627,7 +23667,7 @@ var init_ssoIdentity = __esm({
|
|
|
23627
23667
|
// src/authGate.ts
|
|
23628
23668
|
function enforceRouteAuth(req2, res, match) {
|
|
23629
23669
|
if (!(match.secure === true && match.noAuth !== true)) {
|
|
23630
|
-
return
|
|
23670
|
+
return null;
|
|
23631
23671
|
}
|
|
23632
23672
|
const authHeader = req2.headers.authorization ?? "";
|
|
23633
23673
|
const headerToken = authHeader.startsWith("Bearer ") ? authHeader.slice(7) : "";
|
|
@@ -23657,7 +23697,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
23657
23697
|
const identity = liveSessionIdentity(req2.session?.get?.("_tina4_sso"));
|
|
23658
23698
|
if (identity) {
|
|
23659
23699
|
req2.user = identity;
|
|
23660
|
-
return rbacForbidden(match, identity
|
|
23700
|
+
return rbacForbidden(match, identity);
|
|
23661
23701
|
}
|
|
23662
23702
|
const sessionToken = req2.session?.get?.("token");
|
|
23663
23703
|
if (sessionToken) {
|
|
@@ -23670,9 +23710,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
23670
23710
|
}
|
|
23671
23711
|
}
|
|
23672
23712
|
if (!payload) {
|
|
23673
|
-
|
|
23674
|
-
res.raw.end(JSON.stringify({ error: "Unauthorized" }));
|
|
23675
|
-
return true;
|
|
23713
|
+
return { status: 401, message: "Unauthorized" };
|
|
23676
23714
|
}
|
|
23677
23715
|
req2.user = payload;
|
|
23678
23716
|
if (tokenSource === "body") {
|
|
@@ -23681,7 +23719,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
23681
23719
|
res.header("FreshToken", fresh);
|
|
23682
23720
|
}
|
|
23683
23721
|
}
|
|
23684
|
-
return rbacForbidden(match, req2.user
|
|
23722
|
+
return rbacForbidden(match, req2.user);
|
|
23685
23723
|
}
|
|
23686
23724
|
function rbacClaimList(subject, key, legacy) {
|
|
23687
23725
|
const coerce = (v) => {
|
|
@@ -23698,27 +23736,23 @@ function rbacPermGranted(granted, required) {
|
|
|
23698
23736
|
(g) => g === "*" || g === required || g.endsWith(".*") && required.startsWith(g.slice(0, -1))
|
|
23699
23737
|
);
|
|
23700
23738
|
}
|
|
23701
|
-
function rbacForbidden(match, payload
|
|
23739
|
+
function rbacForbidden(match, payload) {
|
|
23702
23740
|
const requiredRoles = match.requiredRoles ?? [];
|
|
23703
23741
|
const requiredPerms = match.requiredPerms ?? [];
|
|
23704
23742
|
if (requiredRoles.length === 0 && requiredPerms.length === 0) {
|
|
23705
|
-
return
|
|
23743
|
+
return null;
|
|
23706
23744
|
}
|
|
23707
23745
|
const subject = payload && typeof payload === "object" ? payload : {};
|
|
23746
|
+
const forbidden = { status: 403, message: "Forbidden" };
|
|
23708
23747
|
const roles = rbacClaimList(subject, "roles", "role");
|
|
23709
23748
|
for (const group of requiredRoles) {
|
|
23710
|
-
if (!group.some((r) => roles.includes(r))) return
|
|
23749
|
+
if (!group.some((r) => roles.includes(r))) return forbidden;
|
|
23711
23750
|
}
|
|
23712
23751
|
const perms = rbacClaimList(subject, "permissions");
|
|
23713
23752
|
for (const group of requiredPerms) {
|
|
23714
|
-
if (!group.some((p) => rbacPermGranted(perms, p))) return
|
|
23753
|
+
if (!group.some((p) => rbacPermGranted(perms, p))) return forbidden;
|
|
23715
23754
|
}
|
|
23716
|
-
return
|
|
23717
|
-
}
|
|
23718
|
-
function writeForbidden(res) {
|
|
23719
|
-
res.raw.writeHead(403, { "Content-Type": "application/json" });
|
|
23720
|
-
res.raw.end(JSON.stringify({ error: "Forbidden" }));
|
|
23721
|
-
return true;
|
|
23755
|
+
return null;
|
|
23722
23756
|
}
|
|
23723
23757
|
var init_authGate = __esm({
|
|
23724
23758
|
"src/authGate.ts"() {
|
|
@@ -43745,7 +43779,11 @@ async function runMatchedRoute(ctx) {
|
|
|
43745
43779
|
const { req: req2, res, match, postMatchMiddleware } = ctx;
|
|
43746
43780
|
req2.params = match.params;
|
|
43747
43781
|
if (await runGlobalMiddlewarePass(postMatchMiddleware, req2, res)) return;
|
|
43748
|
-
|
|
43782
|
+
const authReject = enforceRouteAuth(req2, res, match);
|
|
43783
|
+
if (authReject) {
|
|
43784
|
+
await renderNegotiatedError(req2, res, authReject.status, authReject.message);
|
|
43785
|
+
return;
|
|
43786
|
+
}
|
|
43749
43787
|
const routeMiddlewareClasses = (match.middlewares ?? []).filter(isMiddlewareClass);
|
|
43750
43788
|
let handlerSkipped = false;
|
|
43751
43789
|
if (match.middlewares && match.middlewares.length > 0) {
|
|
@@ -30,15 +30,31 @@ export interface AuthGateRoute {
|
|
|
30
30
|
requiredPerms?: string[][];
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* A rejection the caller must RENDER (ERR-DEC-02, #342). The gate decides the
|
|
35
|
+
* status and message but does NOT render — the template engine stays out of the
|
|
36
|
+
* auth path's import graph (the auth path imports only node: builtins; see
|
|
37
|
+
* test/authRs256OptIn.test.ts). server.ts, which already depends on Frond,
|
|
38
|
+
* renders this through the shared negotiated renderer.
|
|
39
|
+
*/
|
|
40
|
+
export interface AuthReject {
|
|
41
|
+
status: number;
|
|
42
|
+
message: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
33
45
|
/**
|
|
34
46
|
* Enforce auth for a matched route.
|
|
35
47
|
*
|
|
36
|
-
* Returns `
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
48
|
+
* Returns an `AuthReject` (status + message) when the request is REJECTED — the
|
|
49
|
+
* caller must render it and stop (not run the handler). Returns `null` when the
|
|
50
|
+
* route is public OR a valid token was presented (in which case `req.user` is
|
|
51
|
+
* populated and, for a body formToken, a `FreshToken` header is set). Auth is
|
|
52
|
+
* enforced only for a route that is `secure` and not `noAuth` (the dev
|
|
53
|
+
* dashboard registers its own routes `noAuth`).
|
|
54
|
+
*
|
|
55
|
+
* The gate deliberately does NOT render the rejection: rendering needs the
|
|
56
|
+
* template engine, and the auth path's import graph must stay node:-builtins
|
|
57
|
+
* only (#342). server.ts renders what this returns.
|
|
42
58
|
*
|
|
43
59
|
* Token sources, in priority order: `Authorization: Bearer` header, a
|
|
44
60
|
* `formToken` in the parsed body, then a session token.
|
|
@@ -47,9 +63,9 @@ export function enforceRouteAuth(
|
|
|
47
63
|
req: Tina4Request,
|
|
48
64
|
res: Tina4Response,
|
|
49
65
|
match: AuthGateRoute,
|
|
50
|
-
):
|
|
66
|
+
): AuthReject | null {
|
|
51
67
|
if (!(match.secure === true && match.noAuth !== true)) {
|
|
52
|
-
return
|
|
68
|
+
return null;
|
|
53
69
|
}
|
|
54
70
|
|
|
55
71
|
// Token slots in priority order: Bearer header, body formToken (frond.js
|
|
@@ -86,7 +102,7 @@ export function enforceRouteAuth(
|
|
|
86
102
|
if (identity) {
|
|
87
103
|
req.user = identity;
|
|
88
104
|
// RBAC guards apply to the SSO identity too (Feature 138).
|
|
89
|
-
return rbacForbidden(match, identity
|
|
105
|
+
return rbacForbidden(match, identity);
|
|
90
106
|
}
|
|
91
107
|
const sessionToken = (req as any).session?.get?.("token") as string | undefined;
|
|
92
108
|
if (sessionToken) {
|
|
@@ -96,9 +112,9 @@ export function enforceRouteAuth(
|
|
|
96
112
|
}
|
|
97
113
|
|
|
98
114
|
if (!payload) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return
|
|
115
|
+
// ERR-DEC-02 (#342): the caller negotiates — a browser gets errors/401.twig,
|
|
116
|
+
// an API client the JSON envelope. Was an unconditional raw JSON write here.
|
|
117
|
+
return { status: 401, message: "Unauthorized" };
|
|
102
118
|
}
|
|
103
119
|
|
|
104
120
|
req.user = payload;
|
|
@@ -114,7 +130,7 @@ export function enforceRouteAuth(
|
|
|
114
130
|
// ── RBAC guards (Feature 138): authorization AFTER authentication ──
|
|
115
131
|
// Auth has passed (401 ruled out above). If the route carries role/permission
|
|
116
132
|
// guards, the verified payload must satisfy them, else 403.
|
|
117
|
-
return rbacForbidden(match, req.user
|
|
133
|
+
return rbacForbidden(match, req.user);
|
|
118
134
|
}
|
|
119
135
|
|
|
120
136
|
/** Read a claim as a list of strings; coerce a legacy singular string. */
|
|
@@ -144,28 +160,25 @@ function rbacPermGranted(granted: string[], required: string): boolean {
|
|
|
144
160
|
* the verified payload; return `false` (no write) when authorised or unguarded.
|
|
145
161
|
* AND across guard groups, OR within a group. Feature 138 / ADR-0058.
|
|
146
162
|
*/
|
|
147
|
-
function rbacForbidden(match: AuthGateRoute, payload: unknown
|
|
163
|
+
function rbacForbidden(match: AuthGateRoute, payload: unknown): AuthReject | null {
|
|
148
164
|
const requiredRoles = match.requiredRoles ?? [];
|
|
149
165
|
const requiredPerms = match.requiredPerms ?? [];
|
|
150
166
|
if (requiredRoles.length === 0 && requiredPerms.length === 0) {
|
|
151
|
-
return
|
|
167
|
+
return null;
|
|
152
168
|
}
|
|
153
169
|
const subject =
|
|
154
170
|
payload && typeof payload === "object" ? (payload as Record<string, unknown>) : {};
|
|
155
171
|
|
|
172
|
+
// ERR-DEC-02 (#342): the caller negotiates the 403 (HTML page vs JSON
|
|
173
|
+
// envelope) — the gate only decides the refusal.
|
|
174
|
+
const forbidden: AuthReject = { status: 403, message: "Forbidden" };
|
|
156
175
|
const roles = rbacClaimList(subject, "roles", "role");
|
|
157
176
|
for (const group of requiredRoles) {
|
|
158
|
-
if (!group.some((r) => roles.includes(r))) return
|
|
177
|
+
if (!group.some((r) => roles.includes(r))) return forbidden;
|
|
159
178
|
}
|
|
160
179
|
const perms = rbacClaimList(subject, "permissions");
|
|
161
180
|
for (const group of requiredPerms) {
|
|
162
|
-
if (!group.some((p) => rbacPermGranted(perms, p))) return
|
|
181
|
+
if (!group.some((p) => rbacPermGranted(perms, p))) return forbidden;
|
|
163
182
|
}
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function writeForbidden(res: Tina4Response): boolean {
|
|
168
|
-
res.raw.writeHead(403, { "Content-Type": "application/json" });
|
|
169
|
-
res.raw.end(JSON.stringify({ error: "Forbidden" }));
|
|
170
|
-
return true;
|
|
183
|
+
return null;
|
|
171
184
|
}
|
|
@@ -14,7 +14,7 @@ import { isTruthy } from "./dotenv.js";
|
|
|
14
14
|
import { defaultRouter, type Router } from "./router.js";
|
|
15
15
|
import { resolveClientIp } from "./trustedProxy.js";
|
|
16
16
|
import { currentCspNonce, resolveCspHeader } from "./csp.js";
|
|
17
|
-
import { getFrond, getFrameworkFrond, wantsJson, negotiatedErrorBody } from "./response.js";
|
|
17
|
+
import { getFrond, getFrameworkFrond, wantsJson, negotiatedErrorBody, renderNegotiatedError } from "./response.js";
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Whether to emit a per-request log line (v3.13.14). TINA4_LOG_REQUESTS is
|
|
@@ -210,12 +210,13 @@ async function interpretHookResult(
|
|
|
210
210
|
* all — that name belonged to its dispatcher, back when its orchestrator had no
|
|
211
211
|
* exception handling to mirror.
|
|
212
212
|
*/
|
|
213
|
-
function middleware500(
|
|
213
|
+
async function middleware500(
|
|
214
214
|
res: Tina4Response,
|
|
215
215
|
mwClass: any,
|
|
216
216
|
methodName: string,
|
|
217
217
|
error: unknown,
|
|
218
|
-
|
|
218
|
+
req?: Tina4Request,
|
|
219
|
+
): Promise<Tina4Response> {
|
|
219
220
|
const clsName = mwClass?.name ?? mwClass?.constructor?.name ?? "Middleware";
|
|
220
221
|
const err = error as { name?: string; message?: string };
|
|
221
222
|
const type = err?.name ?? (error as object)?.constructor?.name ?? "Error";
|
|
@@ -225,7 +226,18 @@ function middleware500(
|
|
|
225
226
|
} catch {
|
|
226
227
|
/* never let a broken logger swallow the 500 */
|
|
227
228
|
}
|
|
228
|
-
//
|
|
229
|
+
// ERR-DEC-02 (#342): answer through the SAME negotiation every other framework
|
|
230
|
+
// error uses — errors/500.twig for a browser, the JSON envelope
|
|
231
|
+
// {error, code, message, status, request_id} for an API client. The real
|
|
232
|
+
// exception stays in the log, never in the body (CWE-209). It used to write an
|
|
233
|
+
// unconditional {error:"Internal Server Error",status:500}. `req` is optional
|
|
234
|
+
// only for backward safety; the runner always passes it, and with a raw
|
|
235
|
+
// response available we negotiate.
|
|
236
|
+
if (req && res?.raw && typeof res.raw.writeHead === "function" && !res.raw.writableEnded) {
|
|
237
|
+
await renderNegotiatedError(req, res, 500, "Internal Server Error");
|
|
238
|
+
return res;
|
|
239
|
+
}
|
|
240
|
+
// Fallback (no request / no raw response): the bare JSON body, as before.
|
|
229
241
|
if (typeof (res as any).json === "function") {
|
|
230
242
|
(res as any).json({ error: "Internal Server Error", status: 500 }, 500);
|
|
231
243
|
} else if (typeof (res as any) === "function") {
|
|
@@ -386,7 +398,7 @@ export class MiddlewareRunner {
|
|
|
386
398
|
if (stop) return [req, res, false];
|
|
387
399
|
} catch (error) {
|
|
388
400
|
// Throw → logged clean 500, skip the handler (deterministic).
|
|
389
|
-
res = middleware500(res, cls, method, error);
|
|
401
|
+
res = await middleware500(res, cls, method, error, req);
|
|
390
402
|
return [req, res, false];
|
|
391
403
|
}
|
|
392
404
|
// LEGACY COMPAT PATH — retained, but NOT the main mechanism. A hook
|
|
@@ -443,7 +455,7 @@ export class MiddlewareRunner {
|
|
|
443
455
|
if (stop) return [req, res];
|
|
444
456
|
} catch (error) {
|
|
445
457
|
// Throw → logged clean 500, but remaining after* STILL run.
|
|
446
|
-
res = middleware500(res, cls, method, error);
|
|
458
|
+
res = await middleware500(res, cls, method, error, req);
|
|
447
459
|
continue;
|
|
448
460
|
}
|
|
449
461
|
}
|
|
@@ -641,11 +641,12 @@ export function wantsJson(req: { headers?: Record<string, string | string[] | un
|
|
|
641
641
|
}
|
|
642
642
|
|
|
643
643
|
const ERROR_CODE_NAMES: Record<number, string> = {
|
|
644
|
-
|
|
644
|
+
401: "UNAUTHORIZED", 403: "FORBIDDEN", 404: "NOT_FOUND",
|
|
645
|
+
405: "METHOD_NOT_ALLOWED", 500: "INTERNAL_SERVER_ERROR",
|
|
645
646
|
};
|
|
646
647
|
|
|
647
648
|
/**
|
|
648
|
-
* The ONE JSON error envelope for a negotiated 403/404/500 (ERR-DEC-02).
|
|
649
|
+
* The ONE JSON error envelope for a negotiated 401/403/404/500 (ERR-DEC-02).
|
|
649
650
|
*
|
|
650
651
|
* Reuses errorResponse() (error: true, code, message, status) already shared
|
|
651
652
|
* by app-level response.error() calls, plus request_id for correlation
|
|
@@ -656,3 +657,60 @@ export function negotiatedErrorBody(code: number, message: string, requestId: st
|
|
|
656
657
|
body.request_id = requestId;
|
|
657
658
|
return body;
|
|
658
659
|
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Write a negotiated error straight to the raw response (ERR-DEC-02, #342): a
|
|
663
|
+
* browser gets errors/{code}.twig (app override first, then the framework
|
|
664
|
+
* default), an API client the JSON envelope, both carrying request_id. The
|
|
665
|
+
* ONE cross-module negotiated writer, used by the auth/RBAC gate (authGate.ts)
|
|
666
|
+
* and the throwing-middleware 500 (middleware.ts), which own no renderer of
|
|
667
|
+
* their own; server.ts's 404/500 and middleware.ts's renderForbidden keep their
|
|
668
|
+
* own copies. error_message is ALWAYS empty here (CWE-209 for the 500).
|
|
669
|
+
*
|
|
670
|
+
* Lives in response.ts (a leaf module) so authGate/middleware can import it
|
|
671
|
+
* without the circular dependency a server.ts renderer would create.
|
|
672
|
+
*/
|
|
673
|
+
export async function renderNegotiatedError(
|
|
674
|
+
req: { headers?: Record<string, string | string[] | undefined>; path?: string },
|
|
675
|
+
res: Tina4Response,
|
|
676
|
+
code: number,
|
|
677
|
+
message: string,
|
|
678
|
+
): Promise<void> {
|
|
679
|
+
const { Log } = await import("./logger.js");
|
|
680
|
+
const requestId = Log.getRequestId() ?? "";
|
|
681
|
+
|
|
682
|
+
const writeJson = (): void => {
|
|
683
|
+
const body = negotiatedErrorBody(code, message, requestId);
|
|
684
|
+
res.raw.statusCode = code;
|
|
685
|
+
res.raw.setHeader("Content-Type", "application/json");
|
|
686
|
+
res.raw.end(JSON.stringify(body));
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
if (wantsJson(req)) {
|
|
690
|
+
writeJson();
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const data = { path: req?.path ?? "", error_message: "", request_id: requestId, status_code: code };
|
|
695
|
+
const template = `errors/${code}.twig`;
|
|
696
|
+
let html: string | null = null;
|
|
697
|
+
try {
|
|
698
|
+
html = (await getFrond()).render(template, data);
|
|
699
|
+
} catch {
|
|
700
|
+
/* fall through to the framework default */
|
|
701
|
+
}
|
|
702
|
+
if (!html) {
|
|
703
|
+
try {
|
|
704
|
+
const fw = await getFrameworkFrond();
|
|
705
|
+
html = fw ? fw.render(template, data) : null;
|
|
706
|
+
} catch {
|
|
707
|
+
html = null;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (html) {
|
|
711
|
+
res.raw.writeHead(code, { "Content-Type": "text/html; charset=utf-8" });
|
|
712
|
+
res.raw.end(html);
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
writeJson();
|
|
716
|
+
}
|
|
@@ -39,7 +39,7 @@ import {
|
|
|
39
39
|
sessionAutoStart,
|
|
40
40
|
} from "./dispatchPipeline.js";
|
|
41
41
|
import { runWithFormTokenSession, currentFormTokenSessionId } from "./session.js";
|
|
42
|
-
import { createResponse, setDefaultTemplatesDir, wantsJson, negotiatedErrorBody } from "./response.js";
|
|
42
|
+
import { createResponse, setDefaultTemplatesDir, wantsJson, negotiatedErrorBody, renderNegotiatedError } from "./response.js";
|
|
43
43
|
import { MiddlewareChain, MiddlewareRunner, cors, requestLogger, isMiddlewareClass, attachCsrfFromEnv, SecurityHeadersMiddleware } from "./middleware.js";
|
|
44
44
|
import { tryServeStatic } from "./static.js";
|
|
45
45
|
import { loadEnv, isTruthy } from "./dotenv.js";
|
|
@@ -1205,7 +1205,13 @@ async function runMatchedRoute(ctx: MatchedRouteContext): Promise<void> {
|
|
|
1205
1205
|
// The dev dashboard's own routes are public because DevAdmin registers them
|
|
1206
1206
|
// noAuth, not because of their path: a "/__dev" prefix test also opened any
|
|
1207
1207
|
// write route of the application's own named /__developer or /__dev/anything.
|
|
1208
|
-
|
|
1208
|
+
// The gate decides; server.ts renders the rejection so the auth path stays
|
|
1209
|
+
// free of the template engine (ERR-DEC-02, #342).
|
|
1210
|
+
const authReject = enforceRouteAuth(req, res, match as never);
|
|
1211
|
+
if (authReject) {
|
|
1212
|
+
await renderNegotiatedError(req, res, authReject.status, authReject.message);
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1209
1215
|
|
|
1210
1216
|
// The route's OWN class middleware: its beforeX hooks run inside
|
|
1211
1217
|
// runRouteMiddlewares, its afterX hooks join the after pass below - one
|
|
@@ -9074,6 +9074,41 @@ function negotiatedErrorBody(code, message, requestId) {
|
|
|
9074
9074
|
body.request_id = requestId;
|
|
9075
9075
|
return body;
|
|
9076
9076
|
}
|
|
9077
|
+
async function renderNegotiatedError(req2, res, code, message) {
|
|
9078
|
+
const { Log: Log2 } = await Promise.resolve().then(() => (init_logger(), logger_exports));
|
|
9079
|
+
const requestId = Log2.getRequestId() ?? "";
|
|
9080
|
+
const writeJson = () => {
|
|
9081
|
+
const body = negotiatedErrorBody(code, message, requestId);
|
|
9082
|
+
res.raw.statusCode = code;
|
|
9083
|
+
res.raw.setHeader("Content-Type", "application/json");
|
|
9084
|
+
res.raw.end(JSON.stringify(body));
|
|
9085
|
+
};
|
|
9086
|
+
if (wantsJson(req2)) {
|
|
9087
|
+
writeJson();
|
|
9088
|
+
return;
|
|
9089
|
+
}
|
|
9090
|
+
const data = { path: req2?.path ?? "", error_message: "", request_id: requestId, status_code: code };
|
|
9091
|
+
const template = `errors/${code}.twig`;
|
|
9092
|
+
let html = null;
|
|
9093
|
+
try {
|
|
9094
|
+
html = (await getFrond()).render(template, data);
|
|
9095
|
+
} catch {
|
|
9096
|
+
}
|
|
9097
|
+
if (!html) {
|
|
9098
|
+
try {
|
|
9099
|
+
const fw = await getFrameworkFrond();
|
|
9100
|
+
html = fw ? fw.render(template, data) : null;
|
|
9101
|
+
} catch {
|
|
9102
|
+
html = null;
|
|
9103
|
+
}
|
|
9104
|
+
}
|
|
9105
|
+
if (html) {
|
|
9106
|
+
res.raw.writeHead(code, { "Content-Type": "text/html; charset=utf-8" });
|
|
9107
|
+
res.raw.end(html);
|
|
9108
|
+
return;
|
|
9109
|
+
}
|
|
9110
|
+
writeJson();
|
|
9111
|
+
}
|
|
9077
9112
|
var HEADER_TOKEN, _frondCache, _defaultTemplatesDir, _globalFrond, _frameworkFrond, ERROR_CODE_NAMES;
|
|
9078
9113
|
var init_response = __esm({
|
|
9079
9114
|
"../core/src/response.ts"() {
|
|
@@ -9085,6 +9120,7 @@ var init_response = __esm({
|
|
|
9085
9120
|
_globalFrond = null;
|
|
9086
9121
|
_frameworkFrond = null;
|
|
9087
9122
|
ERROR_CODE_NAMES = {
|
|
9123
|
+
401: "UNAUTHORIZED",
|
|
9088
9124
|
403: "FORBIDDEN",
|
|
9089
9125
|
404: "NOT_FOUND",
|
|
9090
9126
|
405: "METHOD_NOT_ALLOWED",
|
|
@@ -9153,7 +9189,7 @@ async function interpretHookResult(result, req2, res) {
|
|
|
9153
9189
|
}
|
|
9154
9190
|
return [req2, res, false];
|
|
9155
9191
|
}
|
|
9156
|
-
function middleware500(res, mwClass, methodName, error2) {
|
|
9192
|
+
async function middleware500(res, mwClass, methodName, error2, req2) {
|
|
9157
9193
|
const clsName = mwClass?.name ?? mwClass?.constructor?.name ?? "Middleware";
|
|
9158
9194
|
const err = error2;
|
|
9159
9195
|
const type2 = err?.name ?? error2?.constructor?.name ?? "Error";
|
|
@@ -9162,6 +9198,10 @@ function middleware500(res, mwClass, methodName, error2) {
|
|
|
9162
9198
|
Log.error(`Middleware ${clsName}.${methodName} raised ${type2}: ${message}`);
|
|
9163
9199
|
} catch {
|
|
9164
9200
|
}
|
|
9201
|
+
if (req2 && res?.raw && typeof res.raw.writeHead === "function" && !res.raw.writableEnded) {
|
|
9202
|
+
await renderNegotiatedError(req2, res, 500, "Internal Server Error");
|
|
9203
|
+
return res;
|
|
9204
|
+
}
|
|
9165
9205
|
if (typeof res.json === "function") {
|
|
9166
9206
|
res.json({ error: "Internal Server Error", status: 500 }, 500);
|
|
9167
9207
|
} else if (typeof res === "function") {
|
|
@@ -9462,7 +9502,7 @@ var init_middleware = __esm({
|
|
|
9462
9502
|
res = nextRes;
|
|
9463
9503
|
if (stop2) return [req2, res, false];
|
|
9464
9504
|
} catch (error2) {
|
|
9465
|
-
res = middleware500(res, cls, method, error2);
|
|
9505
|
+
res = await middleware500(res, cls, method, error2, req2);
|
|
9466
9506
|
return [req2, res, false];
|
|
9467
9507
|
}
|
|
9468
9508
|
if (res.raw.statusCode >= 400 || res.raw.writableEnded) {
|
|
@@ -9506,7 +9546,7 @@ var init_middleware = __esm({
|
|
|
9506
9546
|
res = nextRes;
|
|
9507
9547
|
if (stop2) return [req2, res];
|
|
9508
9548
|
} catch (error2) {
|
|
9509
|
-
res = middleware500(res, cls, method, error2);
|
|
9549
|
+
res = await middleware500(res, cls, method, error2, req2);
|
|
9510
9550
|
continue;
|
|
9511
9551
|
}
|
|
9512
9552
|
}
|
|
@@ -10510,7 +10550,7 @@ var init_ssoIdentity = __esm({
|
|
|
10510
10550
|
// ../core/src/authGate.ts
|
|
10511
10551
|
function enforceRouteAuth(req2, res, match) {
|
|
10512
10552
|
if (!(match.secure === true && match.noAuth !== true)) {
|
|
10513
|
-
return
|
|
10553
|
+
return null;
|
|
10514
10554
|
}
|
|
10515
10555
|
const authHeader = req2.headers.authorization ?? "";
|
|
10516
10556
|
const headerToken = authHeader.startsWith("Bearer ") ? authHeader.slice(7) : "";
|
|
@@ -10540,7 +10580,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
10540
10580
|
const identity = liveSessionIdentity(req2.session?.get?.("_tina4_sso"));
|
|
10541
10581
|
if (identity) {
|
|
10542
10582
|
req2.user = identity;
|
|
10543
|
-
return rbacForbidden(match, identity
|
|
10583
|
+
return rbacForbidden(match, identity);
|
|
10544
10584
|
}
|
|
10545
10585
|
const sessionToken = req2.session?.get?.("token");
|
|
10546
10586
|
if (sessionToken) {
|
|
@@ -10553,9 +10593,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
10553
10593
|
}
|
|
10554
10594
|
}
|
|
10555
10595
|
if (!payload) {
|
|
10556
|
-
|
|
10557
|
-
res.raw.end(JSON.stringify({ error: "Unauthorized" }));
|
|
10558
|
-
return true;
|
|
10596
|
+
return { status: 401, message: "Unauthorized" };
|
|
10559
10597
|
}
|
|
10560
10598
|
req2.user = payload;
|
|
10561
10599
|
if (tokenSource === "body") {
|
|
@@ -10564,7 +10602,7 @@ function enforceRouteAuth(req2, res, match) {
|
|
|
10564
10602
|
res.header("FreshToken", fresh);
|
|
10565
10603
|
}
|
|
10566
10604
|
}
|
|
10567
|
-
return rbacForbidden(match, req2.user
|
|
10605
|
+
return rbacForbidden(match, req2.user);
|
|
10568
10606
|
}
|
|
10569
10607
|
function rbacClaimList(subject, key, legacy) {
|
|
10570
10608
|
const coerce = (v) => {
|
|
@@ -10581,27 +10619,23 @@ function rbacPermGranted(granted, required) {
|
|
|
10581
10619
|
(g) => g === "*" || g === required || g.endsWith(".*") && required.startsWith(g.slice(0, -1))
|
|
10582
10620
|
);
|
|
10583
10621
|
}
|
|
10584
|
-
function rbacForbidden(match, payload
|
|
10622
|
+
function rbacForbidden(match, payload) {
|
|
10585
10623
|
const requiredRoles = match.requiredRoles ?? [];
|
|
10586
10624
|
const requiredPerms = match.requiredPerms ?? [];
|
|
10587
10625
|
if (requiredRoles.length === 0 && requiredPerms.length === 0) {
|
|
10588
|
-
return
|
|
10626
|
+
return null;
|
|
10589
10627
|
}
|
|
10590
10628
|
const subject = payload && typeof payload === "object" ? payload : {};
|
|
10629
|
+
const forbidden = { status: 403, message: "Forbidden" };
|
|
10591
10630
|
const roles = rbacClaimList(subject, "roles", "role");
|
|
10592
10631
|
for (const group of requiredRoles) {
|
|
10593
|
-
if (!group.some((r) => roles.includes(r))) return
|
|
10632
|
+
if (!group.some((r) => roles.includes(r))) return forbidden;
|
|
10594
10633
|
}
|
|
10595
10634
|
const perms = rbacClaimList(subject, "permissions");
|
|
10596
10635
|
for (const group of requiredPerms) {
|
|
10597
|
-
if (!group.some((p) => rbacPermGranted(perms, p))) return
|
|
10636
|
+
if (!group.some((p) => rbacPermGranted(perms, p))) return forbidden;
|
|
10598
10637
|
}
|
|
10599
|
-
return
|
|
10600
|
-
}
|
|
10601
|
-
function writeForbidden(res) {
|
|
10602
|
-
res.raw.writeHead(403, { "Content-Type": "application/json" });
|
|
10603
|
-
res.raw.end(JSON.stringify({ error: "Forbidden" }));
|
|
10604
|
-
return true;
|
|
10638
|
+
return null;
|
|
10605
10639
|
}
|
|
10606
10640
|
var init_authGate = __esm({
|
|
10607
10641
|
"../core/src/authGate.ts"() {
|
|
@@ -30628,7 +30662,11 @@ async function runMatchedRoute(ctx) {
|
|
|
30628
30662
|
const { req: req2, res, match, postMatchMiddleware } = ctx;
|
|
30629
30663
|
req2.params = match.params;
|
|
30630
30664
|
if (await runGlobalMiddlewarePass(postMatchMiddleware, req2, res)) return;
|
|
30631
|
-
|
|
30665
|
+
const authReject = enforceRouteAuth(req2, res, match);
|
|
30666
|
+
if (authReject) {
|
|
30667
|
+
await renderNegotiatedError(req2, res, authReject.status, authReject.message);
|
|
30668
|
+
return;
|
|
30669
|
+
}
|
|
30632
30670
|
const routeMiddlewareClasses = (match.middlewares ?? []).filter(isMiddlewareClass);
|
|
30633
30671
|
let handlerSkipped = false;
|
|
30634
30672
|
if (match.middlewares && match.middlewares.length > 0) {
|
|
@@ -7,17 +7,32 @@ export interface AuthGateRoute {
|
|
|
7
7
|
requiredRoles?: string[][];
|
|
8
8
|
requiredPerms?: string[][];
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* A rejection the caller must RENDER (ERR-DEC-02, #342). The gate decides the
|
|
12
|
+
* status and message but does NOT render — the template engine stays out of the
|
|
13
|
+
* auth path's import graph (the auth path imports only node: builtins; see
|
|
14
|
+
* test/authRs256OptIn.test.ts). server.ts, which already depends on Frond,
|
|
15
|
+
* renders this through the shared negotiated renderer.
|
|
16
|
+
*/
|
|
17
|
+
export interface AuthReject {
|
|
18
|
+
status: number;
|
|
19
|
+
message: string;
|
|
20
|
+
}
|
|
10
21
|
/**
|
|
11
22
|
* Enforce auth for a matched route.
|
|
12
23
|
*
|
|
13
|
-
* Returns `
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
24
|
+
* Returns an `AuthReject` (status + message) when the request is REJECTED — the
|
|
25
|
+
* caller must render it and stop (not run the handler). Returns `null` when the
|
|
26
|
+
* route is public OR a valid token was presented (in which case `req.user` is
|
|
27
|
+
* populated and, for a body formToken, a `FreshToken` header is set). Auth is
|
|
28
|
+
* enforced only for a route that is `secure` and not `noAuth` (the dev
|
|
29
|
+
* dashboard registers its own routes `noAuth`).
|
|
30
|
+
*
|
|
31
|
+
* The gate deliberately does NOT render the rejection: rendering needs the
|
|
32
|
+
* template engine, and the auth path's import graph must stay node:-builtins
|
|
33
|
+
* only (#342). server.ts renders what this returns.
|
|
19
34
|
*
|
|
20
35
|
* Token sources, in priority order: `Authorization: Bearer` header, a
|
|
21
36
|
* `formToken` in the parsed body, then a session token.
|
|
22
37
|
*/
|
|
23
|
-
export declare function enforceRouteAuth(req: Tina4Request, res: Tina4Response, match: AuthGateRoute):
|
|
38
|
+
export declare function enforceRouteAuth(req: Tina4Request, res: Tina4Response, match: AuthGateRoute): AuthReject | null;
|
|
@@ -61,10 +61,26 @@ export declare function wantsJson(req: {
|
|
|
61
61
|
headers?: Record<string, string | string[] | undefined>;
|
|
62
62
|
}): boolean;
|
|
63
63
|
/**
|
|
64
|
-
* The ONE JSON error envelope for a negotiated 403/404/500 (ERR-DEC-02).
|
|
64
|
+
* The ONE JSON error envelope for a negotiated 401/403/404/500 (ERR-DEC-02).
|
|
65
65
|
*
|
|
66
66
|
* Reuses errorResponse() (error: true, code, message, status) already shared
|
|
67
67
|
* by app-level response.error() calls, plus request_id for correlation
|
|
68
68
|
* (feature 43, ERR-404-REQUESTID) - the SAME shape Python/PHP/Ruby build.
|
|
69
69
|
*/
|
|
70
70
|
export declare function negotiatedErrorBody(code: number, message: string, requestId: string): Record<string, unknown>;
|
|
71
|
+
/**
|
|
72
|
+
* Write a negotiated error straight to the raw response (ERR-DEC-02, #342): a
|
|
73
|
+
* browser gets errors/{code}.twig (app override first, then the framework
|
|
74
|
+
* default), an API client the JSON envelope, both carrying request_id. The
|
|
75
|
+
* ONE cross-module negotiated writer, used by the auth/RBAC gate (authGate.ts)
|
|
76
|
+
* and the throwing-middleware 500 (middleware.ts), which own no renderer of
|
|
77
|
+
* their own; server.ts's 404/500 and middleware.ts's renderForbidden keep their
|
|
78
|
+
* own copies. error_message is ALWAYS empty here (CWE-209 for the 500).
|
|
79
|
+
*
|
|
80
|
+
* Lives in response.ts (a leaf module) so authGate/middleware can import it
|
|
81
|
+
* without the circular dependency a server.ts renderer would create.
|
|
82
|
+
*/
|
|
83
|
+
export declare function renderNegotiatedError(req: {
|
|
84
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
85
|
+
path?: string;
|
|
86
|
+
}, res: Tina4Response, code: number, message: string): Promise<void>;
|