@contember/engine-panel 2.2.0-alpha.10
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 +29 -0
- package/dist/development/index.cjs +21 -0
- package/dist/development/index.cjs.map +1 -0
- package/dist/development/index.js +21 -0
- package/dist/development/index.js.map +1 -0
- package/dist/development/src/PanelAccessCheck.cjs +21 -0
- package/dist/development/src/PanelAccessCheck.cjs.map +1 -0
- package/dist/development/src/PanelAccessCheck.js +21 -0
- package/dist/development/src/PanelAccessCheck.js.map +1 -0
- package/dist/development/src/PanelApiController.cjs +60 -0
- package/dist/development/src/PanelApiController.cjs.map +1 -0
- package/dist/development/src/PanelApiController.js +60 -0
- package/dist/development/src/PanelApiController.js.map +1 -0
- package/dist/development/src/PanelAssets.cjs +57 -0
- package/dist/development/src/PanelAssets.cjs.map +1 -0
- package/dist/development/src/PanelAssets.js +57 -0
- package/dist/development/src/PanelAssets.js.map +1 -0
- package/dist/development/src/PanelController.cjs +108 -0
- package/dist/development/src/PanelController.cjs.map +1 -0
- package/dist/development/src/PanelController.js +108 -0
- package/dist/development/src/PanelController.js.map +1 -0
- package/dist/development/src/PanelMount.cjs +27 -0
- package/dist/development/src/PanelMount.cjs.map +1 -0
- package/dist/development/src/PanelMount.js +27 -0
- package/dist/development/src/PanelMount.js.map +1 -0
- package/dist/development/src/PanelPlugin.cjs +66 -0
- package/dist/development/src/PanelPlugin.cjs.map +1 -0
- package/dist/development/src/PanelPlugin.js +66 -0
- package/dist/development/src/PanelPlugin.js.map +1 -0
- package/dist/development/src/TtlCache.cjs +47 -0
- package/dist/development/src/TtlCache.cjs.map +1 -0
- package/dist/development/src/TtlCache.js +47 -0
- package/dist/development/src/TtlCache.js.map +1 -0
- package/dist/development/src/generated/assets.cjs +46 -0
- package/dist/development/src/generated/assets.cjs.map +1 -0
- package/dist/development/src/generated/assets.js +46 -0
- package/dist/development/src/generated/assets.js.map +1 -0
- package/dist/production/index.cjs +21 -0
- package/dist/production/index.cjs.map +1 -0
- package/dist/production/index.js +21 -0
- package/dist/production/index.js.map +1 -0
- package/dist/production/src/PanelAccessCheck.cjs +21 -0
- package/dist/production/src/PanelAccessCheck.cjs.map +1 -0
- package/dist/production/src/PanelAccessCheck.js +21 -0
- package/dist/production/src/PanelAccessCheck.js.map +1 -0
- package/dist/production/src/PanelApiController.cjs +60 -0
- package/dist/production/src/PanelApiController.cjs.map +1 -0
- package/dist/production/src/PanelApiController.js +60 -0
- package/dist/production/src/PanelApiController.js.map +1 -0
- package/dist/production/src/PanelAssets.cjs +57 -0
- package/dist/production/src/PanelAssets.cjs.map +1 -0
- package/dist/production/src/PanelAssets.js +57 -0
- package/dist/production/src/PanelAssets.js.map +1 -0
- package/dist/production/src/PanelController.cjs +108 -0
- package/dist/production/src/PanelController.cjs.map +1 -0
- package/dist/production/src/PanelController.js +108 -0
- package/dist/production/src/PanelController.js.map +1 -0
- package/dist/production/src/PanelMount.cjs +27 -0
- package/dist/production/src/PanelMount.cjs.map +1 -0
- package/dist/production/src/PanelMount.js +27 -0
- package/dist/production/src/PanelMount.js.map +1 -0
- package/dist/production/src/PanelPlugin.cjs +66 -0
- package/dist/production/src/PanelPlugin.cjs.map +1 -0
- package/dist/production/src/PanelPlugin.js +66 -0
- package/dist/production/src/PanelPlugin.js.map +1 -0
- package/dist/production/src/TtlCache.cjs +47 -0
- package/dist/production/src/TtlCache.cjs.map +1 -0
- package/dist/production/src/TtlCache.js +47 -0
- package/dist/production/src/TtlCache.js.map +1 -0
- package/dist/production/src/generated/assets.cjs +46 -0
- package/dist/production/src/generated/assets.cjs.map +1 -0
- package/dist/production/src/generated/assets.js +46 -0
- package/dist/production/src/generated/assets.js.map +1 -0
- package/dist/tests/cases/unit/panelController.test.d.ts +2 -0
- package/dist/tests/cases/unit/panelController.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/panelMount.test.d.ts +2 -0
- package/dist/tests/cases/unit/panelMount.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/ttlCache.test.d.ts +2 -0
- package/dist/tests/cases/unit/ttlCache.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -0
- package/dist/types/PanelAccessCheck.d.ts +16 -0
- package/dist/types/PanelAccessCheck.d.ts.map +1 -0
- package/dist/types/PanelApiController.d.ts +58 -0
- package/dist/types/PanelApiController.d.ts.map +1 -0
- package/dist/types/PanelAssets.d.ts +32 -0
- package/dist/types/PanelAssets.d.ts.map +1 -0
- package/dist/types/PanelController.d.ts +46 -0
- package/dist/types/PanelController.d.ts.map +1 -0
- package/dist/types/PanelMount.d.ts +26 -0
- package/dist/types/PanelMount.d.ts.map +1 -0
- package/dist/types/PanelPlugin.d.ts +6 -0
- package/dist/types/PanelPlugin.d.ts.map +1 -0
- package/dist/types/TtlCache.d.ts +16 -0
- package/dist/types/TtlCache.d.ts.map +1 -0
- package/dist/types/generated/assets.d.ts +3 -0
- package/dist/types/generated/assets.d.ts.map +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +40 -0
- package/scripts/buildAssets.ts +79 -0
- package/src/PanelAccessCheck.ts +24 -0
- package/src/PanelApiController.ts +126 -0
- package/src/PanelAssets.ts +69 -0
- package/src/PanelController.ts +166 -0
- package/src/PanelMount.ts +34 -0
- package/src/PanelPlugin.ts +77 -0
- package/src/TtlCache.ts +53 -0
- package/src/index.ts +11 -0
- package/src/tsconfig.json +9 -0
- package/tests/cases/unit/panelController.test.ts +159 -0
- package/tests/cases/unit/panelMount.test.ts +55 -0
- package/tests/cases/unit/ttlCache.test.ts +55 -0
- package/tests/tsconfig.json +9 -0
- package/tsconfig.json +8 -0
- package/tsconfig.settings.json +6 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const PanelPlugin = require("./src/PanelPlugin.cjs");
|
|
4
|
+
const PanelAccessCheck = require("./src/PanelAccessCheck.cjs");
|
|
5
|
+
const PanelApiController = require("./src/PanelApiController.cjs");
|
|
6
|
+
const PanelAssets = require("./src/PanelAssets.cjs");
|
|
7
|
+
const PanelController = require("./src/PanelController.cjs");
|
|
8
|
+
const PanelMount = require("./src/PanelMount.cjs");
|
|
9
|
+
const TtlCache = require("./src/TtlCache.cjs");
|
|
10
|
+
exports.PanelPlugin = PanelPlugin.PanelPlugin;
|
|
11
|
+
exports.default = PanelPlugin.PanelPlugin;
|
|
12
|
+
exports.ConfiguredPanelAccessCheck = PanelAccessCheck.ConfiguredPanelAccessCheck;
|
|
13
|
+
exports.PanelApiControllerFactory = PanelApiController.PanelApiControllerFactory;
|
|
14
|
+
exports.accessDeniedResponse = PanelApiController.accessDeniedResponse;
|
|
15
|
+
exports.panelErrorHeader = PanelApiController.panelErrorHeader;
|
|
16
|
+
exports.PanelAssetStore = PanelAssets.PanelAssetStore;
|
|
17
|
+
exports.indexAssetPath = PanelAssets.indexAssetPath;
|
|
18
|
+
exports.PanelController = PanelController.PanelController;
|
|
19
|
+
exports.PanelMount = PanelMount.PanelMount;
|
|
20
|
+
exports.TtlCache = TtlCache.TtlCache;
|
|
21
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PanelPlugin } from "./src/PanelPlugin.js";
|
|
2
|
+
import { ConfiguredPanelAccessCheck } from "./src/PanelAccessCheck.js";
|
|
3
|
+
import { PanelApiControllerFactory, accessDeniedResponse, panelErrorHeader } from "./src/PanelApiController.js";
|
|
4
|
+
import { PanelAssetStore, indexAssetPath } from "./src/PanelAssets.js";
|
|
5
|
+
import { PanelController } from "./src/PanelController.js";
|
|
6
|
+
import { PanelMount } from "./src/PanelMount.js";
|
|
7
|
+
import { TtlCache } from "./src/TtlCache.js";
|
|
8
|
+
export {
|
|
9
|
+
ConfiguredPanelAccessCheck,
|
|
10
|
+
PanelApiControllerFactory,
|
|
11
|
+
PanelAssetStore,
|
|
12
|
+
PanelController,
|
|
13
|
+
PanelMount,
|
|
14
|
+
PanelPlugin,
|
|
15
|
+
TtlCache,
|
|
16
|
+
accessDeniedResponse,
|
|
17
|
+
PanelPlugin as default,
|
|
18
|
+
indexAssetPath,
|
|
19
|
+
panelErrorHeader
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const TtlCache = require("./TtlCache.cjs");
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "", value);
|
|
7
|
+
class ConfiguredPanelAccessCheck {
|
|
8
|
+
constructor(ttlMs = 1e4) {
|
|
9
|
+
__publicField(this, "cache");
|
|
10
|
+
this.cache = new TtlCache.TtlCache(ttlMs);
|
|
11
|
+
}
|
|
12
|
+
async isAllowed({ projectGroup, authResult }) {
|
|
13
|
+
const { panelAccessResolver, databaseContext } = projectGroup.tenantContainer;
|
|
14
|
+
return await this.cache.resolve(
|
|
15
|
+
`${projectGroup.slug ?? ""}:${authResult.apiKeyId}`,
|
|
16
|
+
() => panelAccessResolver.isAllowed(databaseContext, authResult.identityId, authResult.roles)
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.ConfiguredPanelAccessCheck = ConfiguredPanelAccessCheck;
|
|
21
|
+
//# sourceMappingURL=PanelAccessCheck.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PanelAccessCheck.cjs","sources":["../../../../packages/engine-panel/src/PanelAccessCheck.ts"],"sourcesContent":["import type { AuthResult, ProjectGroupContainer } from '@contember/engine-http'\nimport type { PanelAccessCheck } from './PanelApiController.js'\nimport { TtlCache } from './TtlCache.js'\n\n/**\n * Evaluates the tenant-configured panel policy, memoized per api key so a page of parallel requests\n * costs one lookup rather than one per request. The TTL is deliberately short: revoking someone's\n * access should take effect in seconds, not on their next sign-in.\n */\nexport class ConfiguredPanelAccessCheck implements PanelAccessCheck {\n\tprivate readonly cache: TtlCache<boolean>\n\n\tconstructor(ttlMs: number = 10_000) {\n\t\tthis.cache = new TtlCache(ttlMs)\n\t}\n\n\tpublic async isAllowed({ projectGroup, authResult }: { projectGroup: ProjectGroupContainer; authResult: AuthResult }): Promise<boolean> {\n\t\tconst { panelAccessResolver, databaseContext } = projectGroup.tenantContainer\n\t\treturn await this.cache.resolve(\n\t\t\t`${projectGroup.slug ?? ''}:${authResult.apiKeyId}`,\n\t\t\t() => panelAccessResolver.isAllowed(databaseContext, authResult.identityId, authResult.roles),\n\t\t)\n\t}\n}\n"],"names":["TtlCache"],"mappings":";;;;;;AASO,MAAM,2BAAuD;AAAA,EAGnE,YAAY,QAAgB,KAAQ;AAFpC,kBAAA,MAAiB,OAAA;AAGhB,SAAK,QAAQ,IAAIA,SAAAA,SAAS,KAAK;AAAA,EAChC;AAAA,EAEA,MAAa,UAAU,EAAE,cAAc,cAAiG;AACvI,UAAM,EAAE,qBAAqB,gBAAA,IAAoB,aAAa;AAC9D,WAAO,MAAM,KAAK,MAAM;AAAA,MACvB,GAAG,aAAa,QAAQ,EAAE,IAAI,WAAW,QAAQ;AAAA,MACjD,MAAM,oBAAoB,UAAU,iBAAiB,WAAW,YAAY,WAAW,KAAK;AAAA,IAAA;AAAA,EAE9F;AACD;;"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TtlCache } from "./TtlCache.js";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, key + "", value);
|
|
5
|
+
class ConfiguredPanelAccessCheck {
|
|
6
|
+
constructor(ttlMs = 1e4) {
|
|
7
|
+
__publicField(this, "cache");
|
|
8
|
+
this.cache = new TtlCache(ttlMs);
|
|
9
|
+
}
|
|
10
|
+
async isAllowed({ projectGroup, authResult }) {
|
|
11
|
+
const { panelAccessResolver, databaseContext } = projectGroup.tenantContainer;
|
|
12
|
+
return await this.cache.resolve(
|
|
13
|
+
`${projectGroup.slug ?? ""}:${authResult.apiKeyId}`,
|
|
14
|
+
() => panelAccessResolver.isAllowed(databaseContext, authResult.identityId, authResult.roles)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
ConfiguredPanelAccessCheck
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=PanelAccessCheck.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PanelAccessCheck.js","sources":["../../../../packages/engine-panel/src/PanelAccessCheck.ts"],"sourcesContent":["import type { AuthResult, ProjectGroupContainer } from '@contember/engine-http'\nimport type { PanelAccessCheck } from './PanelApiController.js'\nimport { TtlCache } from './TtlCache.js'\n\n/**\n * Evaluates the tenant-configured panel policy, memoized per api key so a page of parallel requests\n * costs one lookup rather than one per request. The TTL is deliberately short: revoking someone's\n * access should take effect in seconds, not on their next sign-in.\n */\nexport class ConfiguredPanelAccessCheck implements PanelAccessCheck {\n\tprivate readonly cache: TtlCache<boolean>\n\n\tconstructor(ttlMs: number = 10_000) {\n\t\tthis.cache = new TtlCache(ttlMs)\n\t}\n\n\tpublic async isAllowed({ projectGroup, authResult }: { projectGroup: ProjectGroupContainer; authResult: AuthResult }): Promise<boolean> {\n\t\tconst { panelAccessResolver, databaseContext } = projectGroup.tenantContainer\n\t\treturn await this.cache.resolve(\n\t\t\t`${projectGroup.slug ?? ''}:${authResult.apiKeyId}`,\n\t\t\t() => panelAccessResolver.isAllowed(databaseContext, authResult.identityId, authResult.roles),\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;AASO,MAAM,2BAAuD;AAAA,EAGnE,YAAY,QAAgB,KAAQ;AAFpC,kBAAA,MAAiB,OAAA;AAGhB,SAAK,QAAQ,IAAI,SAAS,KAAK;AAAA,EAChC;AAAA,EAEA,MAAa,UAAU,EAAE,cAAc,cAAiG;AACvI,UAAM,EAAE,qBAAqB,gBAAA,IAAoB,aAAa;AAC9D,WAAO,MAAM,KAAK,MAAM;AAAA,MACvB,GAAG,aAAa,QAAQ,EAAE,IAAI,WAAW,QAAQ;AAAA,MACjD,MAAM,oBAAoB,UAAU,iBAAiB,WAAW,YAAY,WAAW,KAAK;AAAA,IAAA;AAAA,EAE9F;AACD;"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const engineHttp = require("@contember/engine-http");
|
|
4
|
+
class PanelApiControllerFactory {
|
|
5
|
+
constructor(access) {
|
|
6
|
+
this.access = access;
|
|
7
|
+
}
|
|
8
|
+
create(inner, { allowAnonymous }) {
|
|
9
|
+
return async (ctx) => {
|
|
10
|
+
if (ctx.authResult) {
|
|
11
|
+
if (!await this.access.isAllowed({ projectGroup: ctx.projectGroup, authResult: ctx.authResult })) {
|
|
12
|
+
return accessDeniedResponse();
|
|
13
|
+
}
|
|
14
|
+
return await inner(ctx);
|
|
15
|
+
}
|
|
16
|
+
if (!allowAnonymous) {
|
|
17
|
+
return new engineHttp.HttpErrorResponse(401, "Authentication required");
|
|
18
|
+
}
|
|
19
|
+
if (!isSameOriginBrowser(ctx.request.headers)) {
|
|
20
|
+
return crossOriginDeniedResponse();
|
|
21
|
+
}
|
|
22
|
+
const authResult = await ctx.projectGroup.authenticator.authenticate({
|
|
23
|
+
request: ctx.request,
|
|
24
|
+
timer: ctx.timer,
|
|
25
|
+
clientIp: ctx.clientIp,
|
|
26
|
+
fallbackIdentity: engineHttp.createUnpersistedLoginVerifyResult()
|
|
27
|
+
});
|
|
28
|
+
if (!authResult) {
|
|
29
|
+
return new engineHttp.HttpErrorResponse(401, "Authentication required");
|
|
30
|
+
}
|
|
31
|
+
return await inner({ ...ctx, authResult });
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const isSameOriginBrowser = (headers) => {
|
|
36
|
+
const secFetchSite = headerValue(headers["sec-fetch-site"]);
|
|
37
|
+
if (secFetchSite !== void 0) {
|
|
38
|
+
return secFetchSite === "same-origin" || secFetchSite === "none";
|
|
39
|
+
}
|
|
40
|
+
const origin = headerValue(headers["origin"]);
|
|
41
|
+
if (origin === void 0) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const host = headerValue(headers["host"]);
|
|
45
|
+
try {
|
|
46
|
+
return host !== void 0 && new URL(origin).hostname === new URL(`http://${host}`).hostname;
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const headerValue = (header) => Array.isArray(header) ? header[0] : header;
|
|
52
|
+
const crossOriginDeniedResponse = () => new engineHttp.HttpErrorResponse(403, "PANEL_CROSS_ORIGIN_DENIED: the panel API accepts anonymous calls from its own origin only.");
|
|
53
|
+
const panelErrorHeader = "X-Contember-Panel-Error";
|
|
54
|
+
const accessDeniedResponse = () => new engineHttp.HttpErrorResponse(403, "PANEL_ACCESS_DENIED: this identity is not allowed to use the management panel.", {
|
|
55
|
+
[panelErrorHeader]: "PANEL_ACCESS_DENIED"
|
|
56
|
+
});
|
|
57
|
+
exports.PanelApiControllerFactory = PanelApiControllerFactory;
|
|
58
|
+
exports.accessDeniedResponse = accessDeniedResponse;
|
|
59
|
+
exports.panelErrorHeader = panelErrorHeader;
|
|
60
|
+
//# sourceMappingURL=PanelApiController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PanelApiController.cjs","sources":["../../../../packages/engine-panel/src/PanelApiController.ts"],"sourcesContent":["import type { IncomingHttpHeaders } from 'node:http'\nimport type { AuthResult, HttpController, ProjectGroupContainer } from '@contember/engine-http'\nimport { createUnpersistedLoginVerifyResult, HttpErrorResponse } from '@contember/engine-http'\n\n/**\n * Decides whether a signed-in identity may use the management panel. This is an entry gate for the\n * console, not an authorization system — every API the panel calls still enforces the tenant ACL.\n */\nexport interface PanelAccessCheck {\n\tisAllowed(args: { projectGroup: ProjectGroupContainer; authResult: AuthResult }): Promise<boolean>\n}\n\nexport interface PanelApiControllerOptions {\n\t/**\n\t * Whether a request carrying no credentials may act as the virtual `login` identity. Only the\n\t * tenant mount has a pre-sign-in caller (the login screen); content and system do not, so they\n\t * answer 401 rather than widening the anonymous surface for nobody.\n\t */\n\tallowAnonymous: boolean\n}\n\n/**\n * Wraps an API controller (tenant / content / system) for the panel's own `/panel/api/*` mount.\n *\n * Two things happen here that do not happen on the public routes:\n *\n * 1. **Anonymous calls act as a virtual `login` identity.** The panel's sign-in screen has no\n * credentials of its own, and shipping a login token to the browser — which is what every\n * Contember admin app does today — would publish it. Instead the caller is given\n * `createUnpersistedLoginVerifyResult()`: no token exists anywhere, no `api_key` row is read or\n * written, and the roles are pinned in code, so a misconfigured `CONTEMBER_LOGIN_TOKEN` cannot\n * turn this into anything but `login`. Browser requests must be same-origin; direct clients can\n * reproduce the required headers. See {@link isSameOriginBrowser}.\n * 2. **The access gate runs.** Anything the caller authenticated itself is checked against the\n * configured panel policy before it reaches the API.\n */\nexport class PanelApiControllerFactory {\n\tconstructor(private readonly access: PanelAccessCheck) {}\n\n\tpublic create(inner: HttpController, { allowAnonymous }: PanelApiControllerOptions): HttpController {\n\t\treturn async ctx => {\n\t\t\tif (ctx.authResult) {\n\t\t\t\t// The caller presented its own credentials, so the panel gate applies.\n\t\t\t\tif (!await this.access.isAllowed({ projectGroup: ctx.projectGroup, authResult: ctx.authResult })) {\n\t\t\t\t\treturn accessDeniedResponse()\n\t\t\t\t}\n\t\t\t\treturn await inner(ctx)\n\t\t\t}\n\t\t\tif (!allowAnonymous) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\tif (!isSameOriginBrowser(ctx.request.headers)) {\n\t\t\t\treturn crossOriginDeniedResponse()\n\t\t\t}\n\t\t\tconst authResult = await ctx.projectGroup.authenticator.authenticate({\n\t\t\t\trequest: ctx.request,\n\t\t\t\ttimer: ctx.timer,\n\t\t\t\tclientIp: ctx.clientIp,\n\t\t\t\tfallbackIdentity: createUnpersistedLoginVerifyResult(),\n\t\t\t})\n\t\t\tif (!authResult) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\t// Nothing to gate yet: this is the pre-sign-in panel, not an identity.\n\t\t\treturn await inner({ ...ctx, authResult })\n\t\t}\n\t}\n}\n\n/**\n * Whether the request carries same-origin browser signals. This is a CSRF check, not client\n * authentication: a direct client can reproduce these headers.\n *\n * A cross-origin page is kept out: the app-wide CORS headers would otherwise let any site drive\n * sign-in, password reset and magic links from a visitor's browser, spreading the per-IP rate limits\n * across those visitors' addresses. Rate limits remain the protection against direct abuse.\n *\n * A browser signal is required. Current browsers send `Sec-Fetch-Site`; older browsers send `Origin`\n * on a POST, including a same-origin one.\n */\nconst isSameOriginBrowser = (headers: IncomingHttpHeaders): boolean => {\n\tconst secFetchSite = headerValue(headers['sec-fetch-site'])\n\tif (secFetchSite !== undefined) {\n\t\t// `none` is a user-initiated navigation (typed URL, bookmark); `fetch` never produces it.\n\t\treturn secFetchSite === 'same-origin' || secFetchSite === 'none'\n\t}\n\tconst origin = headerValue(headers['origin'])\n\tif (origin === undefined) {\n\t\treturn false\n\t}\n\tconst host = headerValue(headers['host'])\n\ttry {\n\t\t// Hostname, not host: a TLS-terminating proxy commonly rewrites the port into `Host`, and the\n\t\t// port is not what this check is defending.\n\t\treturn host !== undefined && new URL(origin).hostname === new URL(`http://${host}`).hostname\n\t} catch {\n\t\treturn false\n\t}\n}\n\nconst headerValue = (header: string | string[] | undefined): string | undefined => Array.isArray(header) ? header[0] : header\n\nconst crossOriginDeniedResponse = (): HttpErrorResponse =>\n\tnew HttpErrorResponse(403, 'PANEL_CROSS_ORIGIN_DENIED: the panel API accepts anonymous calls from its own origin only.')\n\n/**\n * Names the gate as the source of a 403, because the status alone does not: the system API answers\n * 403 for a project the caller is not a member of when the server runs in debug mode, and the content\n * API does the same for a rejected `assumeMembership`. Without this header the panel would read either\n * of those as \"you may not be here\" and throw the whole session away.\n *\n * `GraphQlClient` drops the body of a 403 but keeps the `Response`, so a header is what reaches the\n * client. The panel is served from this origin, so it is readable without `Access-Control-Expose-Headers`.\n * The client half is `isPanelAccessDenied` in `panel-ui/src/shell/accessDenied.ts`.\n */\nexport const panelErrorHeader = 'X-Contember-Panel-Error'\n\n/**\n * 403 rather than a GraphQL error, so the panel can tell \"you may not be here\" apart from \"you may\n * not see this particular thing\" (which the APIs report as a 200 with a `ForbiddenError` extension).\n * The message is for operators reading logs; the browser acts on {@link panelErrorHeader}.\n */\nexport const accessDeniedResponse = (): HttpErrorResponse =>\n\tnew HttpErrorResponse(403, 'PANEL_ACCESS_DENIED: this identity is not allowed to use the management panel.', {\n\t\t[panelErrorHeader]: 'PANEL_ACCESS_DENIED',\n\t})\n"],"names":["HttpErrorResponse","createUnpersistedLoginVerifyResult"],"mappings":";;;AAoCO,MAAM,0BAA0B;AAAA,EACtC,YAA6B,QAA0B;AAA1B,SAAA,SAAA;AAAA,EAA2B;AAAA,EAEjD,OAAO,OAAuB,EAAE,kBAA6D;AACnG,WAAO,OAAM,QAAO;AACnB,UAAI,IAAI,YAAY;AAEnB,YAAI,CAAC,MAAM,KAAK,OAAO,UAAU,EAAE,cAAc,IAAI,cAAc,YAAY,IAAI,WAAA,CAAY,GAAG;AACjG,iBAAO,qBAAA;AAAA,QACR;AACA,eAAO,MAAM,MAAM,GAAG;AAAA,MACvB;AACA,UAAI,CAAC,gBAAgB;AACpB,eAAO,IAAIA,WAAAA,kBAAkB,KAAK,yBAAyB;AAAA,MAC5D;AACA,UAAI,CAAC,oBAAoB,IAAI,QAAQ,OAAO,GAAG;AAC9C,eAAO,0BAAA;AAAA,MACR;AACA,YAAM,aAAa,MAAM,IAAI,aAAa,cAAc,aAAa;AAAA,QACpE,SAAS,IAAI;AAAA,QACb,OAAO,IAAI;AAAA,QACX,UAAU,IAAI;AAAA,QACd,kBAAkBC,WAAAA,mCAAA;AAAA,MAAmC,CACrD;AACD,UAAI,CAAC,YAAY;AAChB,eAAO,IAAID,WAAAA,kBAAkB,KAAK,yBAAyB;AAAA,MAC5D;AAEA,aAAO,MAAM,MAAM,EAAE,GAAG,KAAK,YAAY;AAAA,IAC1C;AAAA,EACD;AACD;AAaA,MAAM,sBAAsB,CAAC,YAA0C;AACtE,QAAM,eAAe,YAAY,QAAQ,gBAAgB,CAAC;AAC1D,MAAI,iBAAiB,QAAW;AAE/B,WAAO,iBAAiB,iBAAiB,iBAAiB;AAAA,EAC3D;AACA,QAAM,SAAS,YAAY,QAAQ,QAAQ,CAAC;AAC5C,MAAI,WAAW,QAAW;AACzB,WAAO;AAAA,EACR;AACA,QAAM,OAAO,YAAY,QAAQ,MAAM,CAAC;AACxC,MAAI;AAGH,WAAO,SAAS,UAAa,IAAI,IAAI,MAAM,EAAE,aAAa,IAAI,IAAI,UAAU,IAAI,EAAE,EAAE;AAAA,EACrF,QAAQ;AACP,WAAO;AAAA,EACR;AACD;AAEA,MAAM,cAAc,CAAC,WAA8D,MAAM,QAAQ,MAAM,IAAI,OAAO,CAAC,IAAI;AAEvH,MAAM,4BAA4B,MACjC,IAAIA,6BAAkB,KAAK,4FAA4F;AAYjH,MAAM,mBAAmB;AAOzB,MAAM,uBAAuB,MACnC,IAAIA,6BAAkB,KAAK,kFAAkF;AAAA,EAC5G,CAAC,gBAAgB,GAAG;AACrB,CAAC;;;;"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { HttpErrorResponse, createUnpersistedLoginVerifyResult } from "@contember/engine-http";
|
|
2
|
+
class PanelApiControllerFactory {
|
|
3
|
+
constructor(access) {
|
|
4
|
+
this.access = access;
|
|
5
|
+
}
|
|
6
|
+
create(inner, { allowAnonymous }) {
|
|
7
|
+
return async (ctx) => {
|
|
8
|
+
if (ctx.authResult) {
|
|
9
|
+
if (!await this.access.isAllowed({ projectGroup: ctx.projectGroup, authResult: ctx.authResult })) {
|
|
10
|
+
return accessDeniedResponse();
|
|
11
|
+
}
|
|
12
|
+
return await inner(ctx);
|
|
13
|
+
}
|
|
14
|
+
if (!allowAnonymous) {
|
|
15
|
+
return new HttpErrorResponse(401, "Authentication required");
|
|
16
|
+
}
|
|
17
|
+
if (!isSameOriginBrowser(ctx.request.headers)) {
|
|
18
|
+
return crossOriginDeniedResponse();
|
|
19
|
+
}
|
|
20
|
+
const authResult = await ctx.projectGroup.authenticator.authenticate({
|
|
21
|
+
request: ctx.request,
|
|
22
|
+
timer: ctx.timer,
|
|
23
|
+
clientIp: ctx.clientIp,
|
|
24
|
+
fallbackIdentity: createUnpersistedLoginVerifyResult()
|
|
25
|
+
});
|
|
26
|
+
if (!authResult) {
|
|
27
|
+
return new HttpErrorResponse(401, "Authentication required");
|
|
28
|
+
}
|
|
29
|
+
return await inner({ ...ctx, authResult });
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const isSameOriginBrowser = (headers) => {
|
|
34
|
+
const secFetchSite = headerValue(headers["sec-fetch-site"]);
|
|
35
|
+
if (secFetchSite !== void 0) {
|
|
36
|
+
return secFetchSite === "same-origin" || secFetchSite === "none";
|
|
37
|
+
}
|
|
38
|
+
const origin = headerValue(headers["origin"]);
|
|
39
|
+
if (origin === void 0) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const host = headerValue(headers["host"]);
|
|
43
|
+
try {
|
|
44
|
+
return host !== void 0 && new URL(origin).hostname === new URL(`http://${host}`).hostname;
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const headerValue = (header) => Array.isArray(header) ? header[0] : header;
|
|
50
|
+
const crossOriginDeniedResponse = () => new HttpErrorResponse(403, "PANEL_CROSS_ORIGIN_DENIED: the panel API accepts anonymous calls from its own origin only.");
|
|
51
|
+
const panelErrorHeader = "X-Contember-Panel-Error";
|
|
52
|
+
const accessDeniedResponse = () => new HttpErrorResponse(403, "PANEL_ACCESS_DENIED: this identity is not allowed to use the management panel.", {
|
|
53
|
+
[panelErrorHeader]: "PANEL_ACCESS_DENIED"
|
|
54
|
+
});
|
|
55
|
+
export {
|
|
56
|
+
PanelApiControllerFactory,
|
|
57
|
+
accessDeniedResponse,
|
|
58
|
+
panelErrorHeader
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=PanelApiController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PanelApiController.js","sources":["../../../../packages/engine-panel/src/PanelApiController.ts"],"sourcesContent":["import type { IncomingHttpHeaders } from 'node:http'\nimport type { AuthResult, HttpController, ProjectGroupContainer } from '@contember/engine-http'\nimport { createUnpersistedLoginVerifyResult, HttpErrorResponse } from '@contember/engine-http'\n\n/**\n * Decides whether a signed-in identity may use the management panel. This is an entry gate for the\n * console, not an authorization system — every API the panel calls still enforces the tenant ACL.\n */\nexport interface PanelAccessCheck {\n\tisAllowed(args: { projectGroup: ProjectGroupContainer; authResult: AuthResult }): Promise<boolean>\n}\n\nexport interface PanelApiControllerOptions {\n\t/**\n\t * Whether a request carrying no credentials may act as the virtual `login` identity. Only the\n\t * tenant mount has a pre-sign-in caller (the login screen); content and system do not, so they\n\t * answer 401 rather than widening the anonymous surface for nobody.\n\t */\n\tallowAnonymous: boolean\n}\n\n/**\n * Wraps an API controller (tenant / content / system) for the panel's own `/panel/api/*` mount.\n *\n * Two things happen here that do not happen on the public routes:\n *\n * 1. **Anonymous calls act as a virtual `login` identity.** The panel's sign-in screen has no\n * credentials of its own, and shipping a login token to the browser — which is what every\n * Contember admin app does today — would publish it. Instead the caller is given\n * `createUnpersistedLoginVerifyResult()`: no token exists anywhere, no `api_key` row is read or\n * written, and the roles are pinned in code, so a misconfigured `CONTEMBER_LOGIN_TOKEN` cannot\n * turn this into anything but `login`. Browser requests must be same-origin; direct clients can\n * reproduce the required headers. See {@link isSameOriginBrowser}.\n * 2. **The access gate runs.** Anything the caller authenticated itself is checked against the\n * configured panel policy before it reaches the API.\n */\nexport class PanelApiControllerFactory {\n\tconstructor(private readonly access: PanelAccessCheck) {}\n\n\tpublic create(inner: HttpController, { allowAnonymous }: PanelApiControllerOptions): HttpController {\n\t\treturn async ctx => {\n\t\t\tif (ctx.authResult) {\n\t\t\t\t// The caller presented its own credentials, so the panel gate applies.\n\t\t\t\tif (!await this.access.isAllowed({ projectGroup: ctx.projectGroup, authResult: ctx.authResult })) {\n\t\t\t\t\treturn accessDeniedResponse()\n\t\t\t\t}\n\t\t\t\treturn await inner(ctx)\n\t\t\t}\n\t\t\tif (!allowAnonymous) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\tif (!isSameOriginBrowser(ctx.request.headers)) {\n\t\t\t\treturn crossOriginDeniedResponse()\n\t\t\t}\n\t\t\tconst authResult = await ctx.projectGroup.authenticator.authenticate({\n\t\t\t\trequest: ctx.request,\n\t\t\t\ttimer: ctx.timer,\n\t\t\t\tclientIp: ctx.clientIp,\n\t\t\t\tfallbackIdentity: createUnpersistedLoginVerifyResult(),\n\t\t\t})\n\t\t\tif (!authResult) {\n\t\t\t\treturn new HttpErrorResponse(401, 'Authentication required')\n\t\t\t}\n\t\t\t// Nothing to gate yet: this is the pre-sign-in panel, not an identity.\n\t\t\treturn await inner({ ...ctx, authResult })\n\t\t}\n\t}\n}\n\n/**\n * Whether the request carries same-origin browser signals. This is a CSRF check, not client\n * authentication: a direct client can reproduce these headers.\n *\n * A cross-origin page is kept out: the app-wide CORS headers would otherwise let any site drive\n * sign-in, password reset and magic links from a visitor's browser, spreading the per-IP rate limits\n * across those visitors' addresses. Rate limits remain the protection against direct abuse.\n *\n * A browser signal is required. Current browsers send `Sec-Fetch-Site`; older browsers send `Origin`\n * on a POST, including a same-origin one.\n */\nconst isSameOriginBrowser = (headers: IncomingHttpHeaders): boolean => {\n\tconst secFetchSite = headerValue(headers['sec-fetch-site'])\n\tif (secFetchSite !== undefined) {\n\t\t// `none` is a user-initiated navigation (typed URL, bookmark); `fetch` never produces it.\n\t\treturn secFetchSite === 'same-origin' || secFetchSite === 'none'\n\t}\n\tconst origin = headerValue(headers['origin'])\n\tif (origin === undefined) {\n\t\treturn false\n\t}\n\tconst host = headerValue(headers['host'])\n\ttry {\n\t\t// Hostname, not host: a TLS-terminating proxy commonly rewrites the port into `Host`, and the\n\t\t// port is not what this check is defending.\n\t\treturn host !== undefined && new URL(origin).hostname === new URL(`http://${host}`).hostname\n\t} catch {\n\t\treturn false\n\t}\n}\n\nconst headerValue = (header: string | string[] | undefined): string | undefined => Array.isArray(header) ? header[0] : header\n\nconst crossOriginDeniedResponse = (): HttpErrorResponse =>\n\tnew HttpErrorResponse(403, 'PANEL_CROSS_ORIGIN_DENIED: the panel API accepts anonymous calls from its own origin only.')\n\n/**\n * Names the gate as the source of a 403, because the status alone does not: the system API answers\n * 403 for a project the caller is not a member of when the server runs in debug mode, and the content\n * API does the same for a rejected `assumeMembership`. Without this header the panel would read either\n * of those as \"you may not be here\" and throw the whole session away.\n *\n * `GraphQlClient` drops the body of a 403 but keeps the `Response`, so a header is what reaches the\n * client. The panel is served from this origin, so it is readable without `Access-Control-Expose-Headers`.\n * The client half is `isPanelAccessDenied` in `panel-ui/src/shell/accessDenied.ts`.\n */\nexport const panelErrorHeader = 'X-Contember-Panel-Error'\n\n/**\n * 403 rather than a GraphQL error, so the panel can tell \"you may not be here\" apart from \"you may\n * not see this particular thing\" (which the APIs report as a 200 with a `ForbiddenError` extension).\n * The message is for operators reading logs; the browser acts on {@link panelErrorHeader}.\n */\nexport const accessDeniedResponse = (): HttpErrorResponse =>\n\tnew HttpErrorResponse(403, 'PANEL_ACCESS_DENIED: this identity is not allowed to use the management panel.', {\n\t\t[panelErrorHeader]: 'PANEL_ACCESS_DENIED',\n\t})\n"],"names":[],"mappings":";AAoCO,MAAM,0BAA0B;AAAA,EACtC,YAA6B,QAA0B;AAA1B,SAAA,SAAA;AAAA,EAA2B;AAAA,EAEjD,OAAO,OAAuB,EAAE,kBAA6D;AACnG,WAAO,OAAM,QAAO;AACnB,UAAI,IAAI,YAAY;AAEnB,YAAI,CAAC,MAAM,KAAK,OAAO,UAAU,EAAE,cAAc,IAAI,cAAc,YAAY,IAAI,WAAA,CAAY,GAAG;AACjG,iBAAO,qBAAA;AAAA,QACR;AACA,eAAO,MAAM,MAAM,GAAG;AAAA,MACvB;AACA,UAAI,CAAC,gBAAgB;AACpB,eAAO,IAAI,kBAAkB,KAAK,yBAAyB;AAAA,MAC5D;AACA,UAAI,CAAC,oBAAoB,IAAI,QAAQ,OAAO,GAAG;AAC9C,eAAO,0BAAA;AAAA,MACR;AACA,YAAM,aAAa,MAAM,IAAI,aAAa,cAAc,aAAa;AAAA,QACpE,SAAS,IAAI;AAAA,QACb,OAAO,IAAI;AAAA,QACX,UAAU,IAAI;AAAA,QACd,kBAAkB,mCAAA;AAAA,MAAmC,CACrD;AACD,UAAI,CAAC,YAAY;AAChB,eAAO,IAAI,kBAAkB,KAAK,yBAAyB;AAAA,MAC5D;AAEA,aAAO,MAAM,MAAM,EAAE,GAAG,KAAK,YAAY;AAAA,IAC1C;AAAA,EACD;AACD;AAaA,MAAM,sBAAsB,CAAC,YAA0C;AACtE,QAAM,eAAe,YAAY,QAAQ,gBAAgB,CAAC;AAC1D,MAAI,iBAAiB,QAAW;AAE/B,WAAO,iBAAiB,iBAAiB,iBAAiB;AAAA,EAC3D;AACA,QAAM,SAAS,YAAY,QAAQ,QAAQ,CAAC;AAC5C,MAAI,WAAW,QAAW;AACzB,WAAO;AAAA,EACR;AACA,QAAM,OAAO,YAAY,QAAQ,MAAM,CAAC;AACxC,MAAI;AAGH,WAAO,SAAS,UAAa,IAAI,IAAI,MAAM,EAAE,aAAa,IAAI,IAAI,UAAU,IAAI,EAAE,EAAE;AAAA,EACrF,QAAQ;AACP,WAAO;AAAA,EACR;AACD;AAEA,MAAM,cAAc,CAAC,WAA8D,MAAM,QAAQ,MAAM,IAAI,OAAO,CAAC,IAAI;AAEvH,MAAM,4BAA4B,MACjC,IAAI,kBAAkB,KAAK,4FAA4F;AAYjH,MAAM,mBAAmB;AAOzB,MAAM,uBAAuB,MACnC,IAAI,kBAAkB,KAAK,kFAAkF;AAAA,EAC5G,CAAC,gBAAgB,GAAG;AACrB,CAAC;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const node_zlib = require("node:zlib");
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
const indexAssetPath = "index.html";
|
|
8
|
+
class PanelAssetStore {
|
|
9
|
+
constructor(assets) {
|
|
10
|
+
this.assets = assets;
|
|
11
|
+
__publicField(this, "gzip", /* @__PURE__ */ new Map());
|
|
12
|
+
__publicField(this, "identity", /* @__PURE__ */ new Map());
|
|
13
|
+
}
|
|
14
|
+
/** True when the panel was not built into this binary — see `scripts/buildAssets.ts`. */
|
|
15
|
+
isEmpty() {
|
|
16
|
+
return this.get(indexAssetPath) === void 0;
|
|
17
|
+
}
|
|
18
|
+
getContentType(path) {
|
|
19
|
+
return this.get(path)?.contentType;
|
|
20
|
+
}
|
|
21
|
+
/** Gzipped bytes, decoded from base64 on first use and kept for the process lifetime. */
|
|
22
|
+
getGzip(path) {
|
|
23
|
+
const asset = this.get(path);
|
|
24
|
+
if (!asset) {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
let decoded = this.gzip.get(path);
|
|
28
|
+
if (!decoded) {
|
|
29
|
+
decoded = Buffer.from(asset.gzip, "base64");
|
|
30
|
+
this.gzip.set(path, decoded);
|
|
31
|
+
}
|
|
32
|
+
return decoded;
|
|
33
|
+
}
|
|
34
|
+
/** Uncompressed bytes, decoded once for clients that do not accept gzip. */
|
|
35
|
+
getIdentity(path) {
|
|
36
|
+
const gzip = this.getGzip(path);
|
|
37
|
+
if (gzip === void 0) {
|
|
38
|
+
return void 0;
|
|
39
|
+
}
|
|
40
|
+
let identity = this.identity.get(path);
|
|
41
|
+
if (!identity) {
|
|
42
|
+
identity = node_zlib.gunzipSync(gzip);
|
|
43
|
+
this.identity.set(path, identity);
|
|
44
|
+
}
|
|
45
|
+
return identity;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The map is a plain object literal, so a request for `constructor` or `toString` would otherwise
|
|
49
|
+
* resolve an inherited member and blow up further down. The route is unauthenticated.
|
|
50
|
+
*/
|
|
51
|
+
get(path) {
|
|
52
|
+
return Object.hasOwn(this.assets, path) ? this.assets[path] : void 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.PanelAssetStore = PanelAssetStore;
|
|
56
|
+
exports.indexAssetPath = indexAssetPath;
|
|
57
|
+
//# sourceMappingURL=PanelAssets.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PanelAssets.cjs","sources":["../../../../packages/engine-panel/src/PanelAssets.ts"],"sourcesContent":["import { gunzipSync } from 'node:zlib'\n\n/**\n * A single file of the built panel UI. Every entry is stored gzip-compressed and base64-encoded so\n * the whole panel can be bundled into `dist/start.js` — the engine docker image contains nothing but\n * that one file, so there is no directory to serve from.\n */\nexport interface PanelAsset {\n\treadonly contentType: string\n\t/** gzip-compressed content, base64-encoded. */\n\treadonly gzip: string\n}\n\n/** Keys are paths relative to the panel root, without a leading slash (`assets/index-a1b2.js`). */\nexport type PanelAssetMap = Readonly<Record<string, PanelAsset>>\n\nexport const indexAssetPath = 'index.html'\n\nexport class PanelAssetStore {\n\tprivate readonly gzip = new Map<string, Buffer>()\n\tprivate readonly identity = new Map<string, Buffer>()\n\n\tconstructor(private readonly assets: PanelAssetMap) {}\n\n\t/** True when the panel was not built into this binary — see `scripts/buildAssets.ts`. */\n\tpublic isEmpty(): boolean {\n\t\treturn this.get(indexAssetPath) === undefined\n\t}\n\n\tpublic getContentType(path: string): string | undefined {\n\t\treturn this.get(path)?.contentType\n\t}\n\n\t/** Gzipped bytes, decoded from base64 on first use and kept for the process lifetime. */\n\tpublic getGzip(path: string): Buffer | undefined {\n\t\tconst asset = this.get(path)\n\t\tif (!asset) {\n\t\t\treturn undefined\n\t\t}\n\t\tlet decoded = this.gzip.get(path)\n\t\tif (!decoded) {\n\t\t\tdecoded = Buffer.from(asset.gzip, 'base64')\n\t\t\tthis.gzip.set(path, decoded)\n\t\t}\n\t\treturn decoded\n\t}\n\n\t/** Uncompressed bytes, decoded once for clients that do not accept gzip. */\n\tpublic getIdentity(path: string): Buffer | undefined {\n\t\tconst gzip = this.getGzip(path)\n\t\tif (gzip === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\tlet identity = this.identity.get(path)\n\t\tif (!identity) {\n\t\t\tidentity = gunzipSync(gzip)\n\t\t\tthis.identity.set(path, identity)\n\t\t}\n\t\treturn identity\n\t}\n\n\t/**\n\t * The map is a plain object literal, so a request for `constructor` or `toString` would otherwise\n\t * resolve an inherited member and blow up further down. The route is unauthenticated.\n\t */\n\tprivate get(path: string): PanelAsset | undefined {\n\t\treturn Object.hasOwn(this.assets, path) ? this.assets[path] : undefined\n\t}\n}\n"],"names":["gunzipSync"],"mappings":";;;;;;AAgBO,MAAM,iBAAiB;AAEvB,MAAM,gBAAgB;AAAA,EAI5B,YAA6B,QAAuB;AAAvB,SAAA,SAAA;AAH7B,kBAAA,MAAiB,4BAAW,IAAA,CAAoB;AAChD,kBAAA,MAAiB,gCAAe,IAAA,CAAoB;AAAA,EAEC;AAAA;AAAA,EAG9C,UAAmB;AACzB,WAAO,KAAK,IAAI,cAAc,MAAM;AAAA,EACrC;AAAA,EAEO,eAAe,MAAkC;AACvD,WAAO,KAAK,IAAI,IAAI,GAAG;AAAA,EACxB;AAAA;AAAA,EAGO,QAAQ,MAAkC;AAChD,UAAM,QAAQ,KAAK,IAAI,IAAI;AAC3B,QAAI,CAAC,OAAO;AACX,aAAO;AAAA,IACR;AACA,QAAI,UAAU,KAAK,KAAK,IAAI,IAAI;AAChC,QAAI,CAAC,SAAS;AACb,gBAAU,OAAO,KAAK,MAAM,MAAM,QAAQ;AAC1C,WAAK,KAAK,IAAI,MAAM,OAAO;AAAA,IAC5B;AACA,WAAO;AAAA,EACR;AAAA;AAAA,EAGO,YAAY,MAAkC;AACpD,UAAM,OAAO,KAAK,QAAQ,IAAI;AAC9B,QAAI,SAAS,QAAW;AACvB,aAAO;AAAA,IACR;AACA,QAAI,WAAW,KAAK,SAAS,IAAI,IAAI;AACrC,QAAI,CAAC,UAAU;AACd,iBAAWA,UAAAA,WAAW,IAAI;AAC1B,WAAK,SAAS,IAAI,MAAM,QAAQ;AAAA,IACjC;AACA,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,IAAI,MAAsC;AACjD,WAAO,OAAO,OAAO,KAAK,QAAQ,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI;AAAA,EAC/D;AACD;;;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { gunzipSync } from "node:zlib";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
const indexAssetPath = "index.html";
|
|
6
|
+
class PanelAssetStore {
|
|
7
|
+
constructor(assets) {
|
|
8
|
+
this.assets = assets;
|
|
9
|
+
__publicField(this, "gzip", /* @__PURE__ */ new Map());
|
|
10
|
+
__publicField(this, "identity", /* @__PURE__ */ new Map());
|
|
11
|
+
}
|
|
12
|
+
/** True when the panel was not built into this binary — see `scripts/buildAssets.ts`. */
|
|
13
|
+
isEmpty() {
|
|
14
|
+
return this.get(indexAssetPath) === void 0;
|
|
15
|
+
}
|
|
16
|
+
getContentType(path) {
|
|
17
|
+
return this.get(path)?.contentType;
|
|
18
|
+
}
|
|
19
|
+
/** Gzipped bytes, decoded from base64 on first use and kept for the process lifetime. */
|
|
20
|
+
getGzip(path) {
|
|
21
|
+
const asset = this.get(path);
|
|
22
|
+
if (!asset) {
|
|
23
|
+
return void 0;
|
|
24
|
+
}
|
|
25
|
+
let decoded = this.gzip.get(path);
|
|
26
|
+
if (!decoded) {
|
|
27
|
+
decoded = Buffer.from(asset.gzip, "base64");
|
|
28
|
+
this.gzip.set(path, decoded);
|
|
29
|
+
}
|
|
30
|
+
return decoded;
|
|
31
|
+
}
|
|
32
|
+
/** Uncompressed bytes, decoded once for clients that do not accept gzip. */
|
|
33
|
+
getIdentity(path) {
|
|
34
|
+
const gzip = this.getGzip(path);
|
|
35
|
+
if (gzip === void 0) {
|
|
36
|
+
return void 0;
|
|
37
|
+
}
|
|
38
|
+
let identity = this.identity.get(path);
|
|
39
|
+
if (!identity) {
|
|
40
|
+
identity = gunzipSync(gzip);
|
|
41
|
+
this.identity.set(path, identity);
|
|
42
|
+
}
|
|
43
|
+
return identity;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The map is a plain object literal, so a request for `constructor` or `toString` would otherwise
|
|
47
|
+
* resolve an inherited member and blow up further down. The route is unauthenticated.
|
|
48
|
+
*/
|
|
49
|
+
get(path) {
|
|
50
|
+
return Object.hasOwn(this.assets, path) ? this.assets[path] : void 0;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
PanelAssetStore,
|
|
55
|
+
indexAssetPath
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=PanelAssets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PanelAssets.js","sources":["../../../../packages/engine-panel/src/PanelAssets.ts"],"sourcesContent":["import { gunzipSync } from 'node:zlib'\n\n/**\n * A single file of the built panel UI. Every entry is stored gzip-compressed and base64-encoded so\n * the whole panel can be bundled into `dist/start.js` — the engine docker image contains nothing but\n * that one file, so there is no directory to serve from.\n */\nexport interface PanelAsset {\n\treadonly contentType: string\n\t/** gzip-compressed content, base64-encoded. */\n\treadonly gzip: string\n}\n\n/** Keys are paths relative to the panel root, without a leading slash (`assets/index-a1b2.js`). */\nexport type PanelAssetMap = Readonly<Record<string, PanelAsset>>\n\nexport const indexAssetPath = 'index.html'\n\nexport class PanelAssetStore {\n\tprivate readonly gzip = new Map<string, Buffer>()\n\tprivate readonly identity = new Map<string, Buffer>()\n\n\tconstructor(private readonly assets: PanelAssetMap) {}\n\n\t/** True when the panel was not built into this binary — see `scripts/buildAssets.ts`. */\n\tpublic isEmpty(): boolean {\n\t\treturn this.get(indexAssetPath) === undefined\n\t}\n\n\tpublic getContentType(path: string): string | undefined {\n\t\treturn this.get(path)?.contentType\n\t}\n\n\t/** Gzipped bytes, decoded from base64 on first use and kept for the process lifetime. */\n\tpublic getGzip(path: string): Buffer | undefined {\n\t\tconst asset = this.get(path)\n\t\tif (!asset) {\n\t\t\treturn undefined\n\t\t}\n\t\tlet decoded = this.gzip.get(path)\n\t\tif (!decoded) {\n\t\t\tdecoded = Buffer.from(asset.gzip, 'base64')\n\t\t\tthis.gzip.set(path, decoded)\n\t\t}\n\t\treturn decoded\n\t}\n\n\t/** Uncompressed bytes, decoded once for clients that do not accept gzip. */\n\tpublic getIdentity(path: string): Buffer | undefined {\n\t\tconst gzip = this.getGzip(path)\n\t\tif (gzip === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\tlet identity = this.identity.get(path)\n\t\tif (!identity) {\n\t\t\tidentity = gunzipSync(gzip)\n\t\t\tthis.identity.set(path, identity)\n\t\t}\n\t\treturn identity\n\t}\n\n\t/**\n\t * The map is a plain object literal, so a request for `constructor` or `toString` would otherwise\n\t * resolve an inherited member and blow up further down. The route is unauthenticated.\n\t */\n\tprivate get(path: string): PanelAsset | undefined {\n\t\treturn Object.hasOwn(this.assets, path) ? this.assets[path] : undefined\n\t}\n}\n"],"names":[],"mappings":";;;;AAgBO,MAAM,iBAAiB;AAEvB,MAAM,gBAAgB;AAAA,EAI5B,YAA6B,QAAuB;AAAvB,SAAA,SAAA;AAH7B,kBAAA,MAAiB,4BAAW,IAAA,CAAoB;AAChD,kBAAA,MAAiB,gCAAe,IAAA,CAAoB;AAAA,EAEC;AAAA;AAAA,EAG9C,UAAmB;AACzB,WAAO,KAAK,IAAI,cAAc,MAAM;AAAA,EACrC;AAAA,EAEO,eAAe,MAAkC;AACvD,WAAO,KAAK,IAAI,IAAI,GAAG;AAAA,EACxB;AAAA;AAAA,EAGO,QAAQ,MAAkC;AAChD,UAAM,QAAQ,KAAK,IAAI,IAAI;AAC3B,QAAI,CAAC,OAAO;AACX,aAAO;AAAA,IACR;AACA,QAAI,UAAU,KAAK,KAAK,IAAI,IAAI;AAChC,QAAI,CAAC,SAAS;AACb,gBAAU,OAAO,KAAK,MAAM,MAAM,QAAQ;AAC1C,WAAK,KAAK,IAAI,MAAM,OAAO;AAAA,IAC5B;AACA,WAAO;AAAA,EACR;AAAA;AAAA,EAGO,YAAY,MAAkC;AACpD,UAAM,OAAO,KAAK,QAAQ,IAAI;AAC9B,QAAI,SAAS,QAAW;AACvB,aAAO;AAAA,IACR;AACA,QAAI,WAAW,KAAK,SAAS,IAAI,IAAI;AACrC,QAAI,CAAC,UAAU;AACd,iBAAW,WAAW,IAAI;AAC1B,WAAK,SAAS,IAAI,MAAM,QAAQ;AAAA,IACjC;AACA,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,IAAI,MAAsC;AACjD,WAAO,OAAO,OAAO,KAAK,QAAQ,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI;AAAA,EAC/D;AACD;"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const node_zlib = require("node:zlib");
|
|
4
|
+
const PanelAssets = require("./PanelAssets.cjs");
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
+
const basePlaceholder = "__CONTEMBER_PANEL_BASE__";
|
|
9
|
+
const configPlaceholder = "__CONTEMBER_PANEL_CONFIG__";
|
|
10
|
+
const contentSecurityPolicy = [
|
|
11
|
+
`default-src 'self'`,
|
|
12
|
+
`script-src 'self'`,
|
|
13
|
+
`style-src 'self' 'unsafe-inline'`,
|
|
14
|
+
`img-src 'self' data:`,
|
|
15
|
+
`font-src 'self' data:`,
|
|
16
|
+
`connect-src 'self'`,
|
|
17
|
+
`base-uri 'self'`,
|
|
18
|
+
`form-action 'self'`,
|
|
19
|
+
`frame-ancestors 'none'`
|
|
20
|
+
].join("; ");
|
|
21
|
+
class PanelController {
|
|
22
|
+
constructor(args) {
|
|
23
|
+
this.args = args;
|
|
24
|
+
__publicField(this, "indexHtml");
|
|
25
|
+
__publicField(this, "indexSource");
|
|
26
|
+
const source = args.assets.getGzip(PanelAssets.indexAssetPath);
|
|
27
|
+
if (source === void 0) {
|
|
28
|
+
throw new Error("The panel UI was not built into this binary. Run `bun run pre-build` before building the server.");
|
|
29
|
+
}
|
|
30
|
+
this.indexSource = source;
|
|
31
|
+
}
|
|
32
|
+
create() {
|
|
33
|
+
return (ctx) => {
|
|
34
|
+
const response = this.handle({
|
|
35
|
+
method: ctx.koa.method,
|
|
36
|
+
path: typeof ctx.params.path === "string" ? ctx.params.path : void 0,
|
|
37
|
+
acceptEncoding: headerValue(ctx.request.headers["accept-encoding"])
|
|
38
|
+
});
|
|
39
|
+
ctx.koa.status = response.status;
|
|
40
|
+
for (const [name, value] of Object.entries(response.headers)) {
|
|
41
|
+
ctx.koa.set(name, value);
|
|
42
|
+
}
|
|
43
|
+
ctx.koa.body = response.body;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
handle(request) {
|
|
47
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
48
|
+
return { status: 405, headers: { "Allow": "GET, HEAD" }, body: Buffer.alloc(0) };
|
|
49
|
+
}
|
|
50
|
+
const path = normalizePath(request.path);
|
|
51
|
+
const gzip = path === void 0 || path === PanelAssets.indexAssetPath ? void 0 : this.args.assets.getGzip(path);
|
|
52
|
+
if (path === void 0 || gzip === void 0) {
|
|
53
|
+
this.indexHtml ?? (this.indexHtml = this.renderIndex());
|
|
54
|
+
return this.respond(request, this.indexHtml.gzip, () => this.indexHtml?.identity, {
|
|
55
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
56
|
+
"Cache-Control": "no-store",
|
|
57
|
+
"Content-Security-Policy": contentSecurityPolicy,
|
|
58
|
+
"X-Robots-Tag": "noindex, nofollow",
|
|
59
|
+
"Referrer-Policy": "no-referrer"
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return this.respond(request, gzip, () => this.args.assets.getIdentity(path), {
|
|
63
|
+
"Content-Type": this.args.assets.getContentType(path) ?? "application/octet-stream",
|
|
64
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
respond(request, gzipped, identity, headers) {
|
|
68
|
+
const common = {
|
|
69
|
+
...headers,
|
|
70
|
+
"Vary": "Accept-Encoding",
|
|
71
|
+
"X-Content-Type-Options": "nosniff"
|
|
72
|
+
};
|
|
73
|
+
if (acceptsGzip(request.acceptEncoding)) {
|
|
74
|
+
return { status: 200, headers: { ...common, "Content-Encoding": "gzip" }, body: request.method === "HEAD" ? Buffer.alloc(0) : gzipped };
|
|
75
|
+
}
|
|
76
|
+
return { status: 200, headers: common, body: request.method === "HEAD" ? Buffer.alloc(0) : identity() ?? Buffer.alloc(0) };
|
|
77
|
+
}
|
|
78
|
+
renderIndex() {
|
|
79
|
+
const config = JSON.stringify({
|
|
80
|
+
basePath: `${this.args.basePath}/`,
|
|
81
|
+
apiBaseUrl: this.args.apiBaseUrl,
|
|
82
|
+
pluginApis: this.args.pluginApis()
|
|
83
|
+
});
|
|
84
|
+
const identity = Buffer.from(
|
|
85
|
+
node_zlib.gunzipSync(this.indexSource).toString("utf8").replaceAll(basePlaceholder, () => `${this.args.basePath}/`).replaceAll(configPlaceholder, () => config)
|
|
86
|
+
);
|
|
87
|
+
return { gzip: node_zlib.gzipSync(identity), identity };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const headerValue = (header) => Array.isArray(header) ? header.join(",") : header;
|
|
91
|
+
const acceptsGzip = (acceptEncoding) => acceptEncoding !== void 0 && /(^|,)\s*(gzip|\*)\s*(;|,|$)/.test(acceptEncoding);
|
|
92
|
+
const normalizePath = (raw) => {
|
|
93
|
+
if (raw === void 0 || raw === "") {
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
let decoded;
|
|
97
|
+
try {
|
|
98
|
+
decoded = decodeURIComponent(raw);
|
|
99
|
+
} catch {
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
if (decoded.includes("\0") || decoded.split("/").includes("..")) {
|
|
103
|
+
return void 0;
|
|
104
|
+
}
|
|
105
|
+
return decoded;
|
|
106
|
+
};
|
|
107
|
+
exports.PanelController = PanelController;
|
|
108
|
+
//# sourceMappingURL=PanelController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PanelController.cjs","sources":["../../../../packages/engine-panel/src/PanelController.ts"],"sourcesContent":["import { gunzipSync, gzipSync } from 'node:zlib'\nimport type { InternalHttpContext, InternalHttpController } from '@contember/engine-http'\nimport { indexAssetPath, PanelAssetStore } from './PanelAssets.js'\n\nexport interface PanelControllerArgs {\n\t/** Mount path without a trailing slash, e.g. `/panel`. */\n\tbasePath: string\n\t/** Prefix the panel prepends to every API path, e.g. `/panel/api`. */\n\tapiBaseUrl: string\n\tassets: PanelAssetStore\n\t/** Plugin APIs mounted under {@link apiBaseUrl}; read on the first request, see {@link PanelController.indexHtml}. */\n\tpluginApis: () => readonly string[]\n}\n\n/** Everything the controller reads from a request — see {@link PanelController.handle}. */\nexport interface PanelRequest {\n\tmethod: string\n\t/** Path relative to the panel root, i.e. the route's wildcard parameter. */\n\tpath: string | undefined\n\tacceptEncoding: string | undefined\n}\n\nexport interface PanelResponse {\n\tstatus: number\n\theaders: Readonly<Record<string, string>>\n\tbody: Buffer\n}\n\nconst basePlaceholder = '__CONTEMBER_PANEL_BASE__'\nconst configPlaceholder = '__CONTEMBER_PANEL_CONFIG__'\n\n/**\n * `style-src` allows inline styles because the component libraries set `style` attributes; nothing\n * else is relaxed, and no external origin is reachable at all.\n */\nconst contentSecurityPolicy = [\n\t`default-src 'self'`,\n\t`script-src 'self'`,\n\t`style-src 'self' 'unsafe-inline'`,\n\t`img-src 'self' data:`,\n\t`font-src 'self' data:`,\n\t`connect-src 'self'`,\n\t`base-uri 'self'`,\n\t`form-action 'self'`,\n\t`frame-ancestors 'none'`,\n].join('; ')\n\n/**\n * Serves the built panel UI out of the bundled asset map.\n *\n * Registered as an *internal* route: static assets must not authenticate, and going through the\n * regular pipeline would resolve a project group and verify an API key against the database on every\n * file request. The panel's API traffic is a separate, regular route.\n */\nexport class PanelController {\n\t/**\n\t * Rendered on the first request rather than here: the plugins that mount their API into the panel\n\t * are not all registered yet while the container is still being built, and the shell carries the\n\t * list. Built once and kept, since nothing in it can change afterwards.\n\t */\n\tprivate indexHtml: { gzip: Buffer; identity: Buffer } | undefined\n\tprivate readonly indexSource: Buffer\n\n\tconstructor(private readonly args: PanelControllerArgs) {\n\t\tconst source = args.assets.getGzip(indexAssetPath)\n\t\tif (source === undefined) {\n\t\t\tthrow new Error('The panel UI was not built into this binary. Run `bun run pre-build` before building the server.')\n\t\t}\n\t\tthis.indexSource = source\n\t}\n\n\tpublic create(): InternalHttpController {\n\t\treturn (ctx: InternalHttpContext) => {\n\t\t\tconst response = this.handle({\n\t\t\t\tmethod: ctx.koa.method,\n\t\t\t\tpath: typeof ctx.params.path === 'string' ? ctx.params.path : undefined,\n\t\t\t\tacceptEncoding: headerValue(ctx.request.headers['accept-encoding']),\n\t\t\t})\n\t\t\tctx.koa.status = response.status\n\t\t\tfor (const [name, value] of Object.entries(response.headers)) {\n\t\t\t\tctx.koa.set(name, value)\n\t\t\t}\n\t\t\tctx.koa.body = response.body\n\t\t}\n\t}\n\n\tpublic handle(request: PanelRequest): PanelResponse {\n\t\tif (request.method !== 'GET' && request.method !== 'HEAD') {\n\t\t\treturn { status: 405, headers: { 'Allow': 'GET, HEAD' }, body: Buffer.alloc(0) }\n\t\t}\n\n\t\tconst path = normalizePath(request.path)\n\t\tconst gzip = path === undefined || path === indexAssetPath ? undefined : this.args.assets.getGzip(path)\n\n\t\tif (path === undefined || gzip === undefined) {\n\t\t\t// Anything that is not a built file is a client-side route — serve the app shell.\n\t\t\tthis.indexHtml ??= this.renderIndex()\n\t\t\treturn this.respond(request, this.indexHtml.gzip, () => this.indexHtml?.identity, {\n\t\t\t\t'Content-Type': 'text/html; charset=utf-8',\n\t\t\t\t'Cache-Control': 'no-store',\n\t\t\t\t'Content-Security-Policy': contentSecurityPolicy,\n\t\t\t\t'X-Robots-Tag': 'noindex, nofollow',\n\t\t\t\t'Referrer-Policy': 'no-referrer',\n\t\t\t})\n\t\t}\n\t\t// Every built file name carries a content hash, so it can never change under the same URL.\n\t\treturn this.respond(request, gzip, () => this.args.assets.getIdentity(path), {\n\t\t\t'Content-Type': this.args.assets.getContentType(path) ?? 'application/octet-stream',\n\t\t\t'Cache-Control': 'public, max-age=31536000, immutable',\n\t\t})\n\t}\n\n\tprivate respond(request: PanelRequest, gzipped: Buffer, identity: () => Buffer | undefined, headers: Record<string, string>): PanelResponse {\n\t\tconst common = {\n\t\t\t...headers,\n\t\t\t'Vary': 'Accept-Encoding',\n\t\t\t'X-Content-Type-Options': 'nosniff',\n\t\t}\n\t\tif (acceptsGzip(request.acceptEncoding)) {\n\t\t\treturn { status: 200, headers: { ...common, 'Content-Encoding': 'gzip' }, body: request.method === 'HEAD' ? Buffer.alloc(0) : gzipped }\n\t\t}\n\t\treturn { status: 200, headers: common, body: request.method === 'HEAD' ? Buffer.alloc(0) : identity() ?? Buffer.alloc(0) }\n\t}\n\n\tprivate renderIndex(): { gzip: Buffer; identity: Buffer } {\n\t\tconst config = JSON.stringify({\n\t\t\tbasePath: `${this.args.basePath}/`,\n\t\t\tapiBaseUrl: this.args.apiBaseUrl,\n\t\t\tpluginApis: this.args.pluginApis(),\n\t\t})\n\t\tconst identity = Buffer.from(\n\t\t\tgunzipSync(this.indexSource)\n\t\t\t\t.toString('utf8')\n\t\t\t\t// An absolute base href keeps the relative asset URLs resolving from the panel root at any\n\t\t\t\t// route depth, which is what lets the mount path stay configurable.\n\t\t\t\t.replaceAll(basePlaceholder, () => `${this.args.basePath}/`)\n\t\t\t\t.replaceAll(configPlaceholder, () => config),\n\t\t)\n\t\treturn { gzip: gzipSync(identity), identity }\n\t}\n}\n\nconst headerValue = (header: string | string[] | undefined): string | undefined => Array.isArray(header) ? header.join(',') : header\n\nconst acceptsGzip = (acceptEncoding: string | undefined): boolean =>\n\tacceptEncoding !== undefined && /(^|,)\\s*(gzip|\\*)\\s*(;|,|$)/.test(acceptEncoding)\n\n/**\n * `undefined` for the panel root, `undefined` for anything that tries to escape the asset map;\n * both fall through to the app shell.\n */\nconst normalizePath = (raw: string | undefined): string | undefined => {\n\tif (raw === undefined || raw === '') {\n\t\treturn undefined\n\t}\n\tlet decoded: string\n\ttry {\n\t\tdecoded = decodeURIComponent(raw)\n\t} catch {\n\t\treturn undefined\n\t}\n\tif (decoded.includes('\\0') || decoded.split('/').includes('..')) {\n\t\treturn undefined\n\t}\n\treturn decoded\n}\n"],"names":["indexAssetPath","gunzipSync","gzipSync"],"mappings":";;;;;;;AA4BA,MAAM,kBAAkB;AACxB,MAAM,oBAAoB;AAM1B,MAAM,wBAAwB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,EAAE,KAAK,IAAI;AASJ,MAAM,gBAAgB;AAAA,EAS5B,YAA6B,MAA2B;AAA3B,SAAA,OAAA;AAH7B,kBAAA,MAAQ,WAAA;AACR,kBAAA,MAAiB,aAAA;AAGhB,UAAM,SAAS,KAAK,OAAO,QAAQA,YAAAA,cAAc;AACjD,QAAI,WAAW,QAAW;AACzB,YAAM,IAAI,MAAM,kGAAkG;AAAA,IACnH;AACA,SAAK,cAAc;AAAA,EACpB;AAAA,EAEO,SAAiC;AACvC,WAAO,CAAC,QAA6B;AACpC,YAAM,WAAW,KAAK,OAAO;AAAA,QAC5B,QAAQ,IAAI,IAAI;AAAA,QAChB,MAAM,OAAO,IAAI,OAAO,SAAS,WAAW,IAAI,OAAO,OAAO;AAAA,QAC9D,gBAAgB,YAAY,IAAI,QAAQ,QAAQ,iBAAiB,CAAC;AAAA,MAAA,CAClE;AACD,UAAI,IAAI,SAAS,SAAS;AAC1B,iBAAW,CAAC,MAAM,KAAK,KAAK,OAAO,QAAQ,SAAS,OAAO,GAAG;AAC7D,YAAI,IAAI,IAAI,MAAM,KAAK;AAAA,MACxB;AACA,UAAI,IAAI,OAAO,SAAS;AAAA,IACzB;AAAA,EACD;AAAA,EAEO,OAAO,SAAsC;AACnD,QAAI,QAAQ,WAAW,SAAS,QAAQ,WAAW,QAAQ;AAC1D,aAAO,EAAE,QAAQ,KAAK,SAAS,EAAE,SAAS,YAAA,GAAe,MAAM,OAAO,MAAM,CAAC,EAAA;AAAA,IAC9E;AAEA,UAAM,OAAO,cAAc,QAAQ,IAAI;AACvC,UAAM,OAAO,SAAS,UAAa,SAASA,YAAAA,iBAAiB,SAAY,KAAK,KAAK,OAAO,QAAQ,IAAI;AAEtG,QAAI,SAAS,UAAa,SAAS,QAAW;AAE7C,WAAK,cAAL,KAAK,YAAc,KAAK,YAAA;AACxB,aAAO,KAAK,QAAQ,SAAS,KAAK,UAAU,MAAM,MAAM,KAAK,WAAW,UAAU;AAAA,QACjF,gBAAgB;AAAA,QAChB,iBAAiB;AAAA,QACjB,2BAA2B;AAAA,QAC3B,gBAAgB;AAAA,QAChB,mBAAmB;AAAA,MAAA,CACnB;AAAA,IACF;AAEA,WAAO,KAAK,QAAQ,SAAS,MAAM,MAAM,KAAK,KAAK,OAAO,YAAY,IAAI,GAAG;AAAA,MAC5E,gBAAgB,KAAK,KAAK,OAAO,eAAe,IAAI,KAAK;AAAA,MACzD,iBAAiB;AAAA,IAAA,CACjB;AAAA,EACF;AAAA,EAEQ,QAAQ,SAAuB,SAAiB,UAAoC,SAAgD;AAC3I,UAAM,SAAS;AAAA,MACd,GAAG;AAAA,MACH,QAAQ;AAAA,MACR,0BAA0B;AAAA,IAAA;AAE3B,QAAI,YAAY,QAAQ,cAAc,GAAG;AACxC,aAAO,EAAE,QAAQ,KAAK,SAAS,EAAE,GAAG,QAAQ,oBAAoB,OAAA,GAAU,MAAM,QAAQ,WAAW,SAAS,OAAO,MAAM,CAAC,IAAI,QAAA;AAAA,IAC/H;AACA,WAAO,EAAE,QAAQ,KAAK,SAAS,QAAQ,MAAM,QAAQ,WAAW,SAAS,OAAO,MAAM,CAAC,IAAI,SAAA,KAAc,OAAO,MAAM,CAAC,EAAA;AAAA,EACxH;AAAA,EAEQ,cAAkD;AACzD,UAAM,SAAS,KAAK,UAAU;AAAA,MAC7B,UAAU,GAAG,KAAK,KAAK,QAAQ;AAAA,MAC/B,YAAY,KAAK,KAAK;AAAA,MACtB,YAAY,KAAK,KAAK,WAAA;AAAA,IAAW,CACjC;AACD,UAAM,WAAW,OAAO;AAAA,MACvBC,UAAAA,WAAW,KAAK,WAAW,EACzB,SAAS,MAAM,EAGf,WAAW,iBAAiB,MAAM,GAAG,KAAK,KAAK,QAAQ,GAAG,EAC1D,WAAW,mBAAmB,MAAM,MAAM;AAAA,IAAA;AAE7C,WAAO,EAAE,MAAMC,UAAAA,SAAS,QAAQ,GAAG,SAAA;AAAA,EACpC;AACD;AAEA,MAAM,cAAc,CAAC,WAA8D,MAAM,QAAQ,MAAM,IAAI,OAAO,KAAK,GAAG,IAAI;AAE9H,MAAM,cAAc,CAAC,mBACpB,mBAAmB,UAAa,8BAA8B,KAAK,cAAc;AAMlF,MAAM,gBAAgB,CAAC,QAAgD;AACtE,MAAI,QAAQ,UAAa,QAAQ,IAAI;AACpC,WAAO;AAAA,EACR;AACA,MAAI;AACJ,MAAI;AACH,cAAU,mBAAmB,GAAG;AAAA,EACjC,QAAQ;AACP,WAAO;AAAA,EACR;AACA,MAAI,QAAQ,SAAS,IAAI,KAAK,QAAQ,MAAM,GAAG,EAAE,SAAS,IAAI,GAAG;AAChE,WAAO;AAAA,EACR;AACA,SAAO;AACR;;"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { gunzipSync, gzipSync } from "node:zlib";
|
|
2
|
+
import { indexAssetPath } from "./PanelAssets.js";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
const basePlaceholder = "__CONTEMBER_PANEL_BASE__";
|
|
7
|
+
const configPlaceholder = "__CONTEMBER_PANEL_CONFIG__";
|
|
8
|
+
const contentSecurityPolicy = [
|
|
9
|
+
`default-src 'self'`,
|
|
10
|
+
`script-src 'self'`,
|
|
11
|
+
`style-src 'self' 'unsafe-inline'`,
|
|
12
|
+
`img-src 'self' data:`,
|
|
13
|
+
`font-src 'self' data:`,
|
|
14
|
+
`connect-src 'self'`,
|
|
15
|
+
`base-uri 'self'`,
|
|
16
|
+
`form-action 'self'`,
|
|
17
|
+
`frame-ancestors 'none'`
|
|
18
|
+
].join("; ");
|
|
19
|
+
class PanelController {
|
|
20
|
+
constructor(args) {
|
|
21
|
+
this.args = args;
|
|
22
|
+
__publicField(this, "indexHtml");
|
|
23
|
+
__publicField(this, "indexSource");
|
|
24
|
+
const source = args.assets.getGzip(indexAssetPath);
|
|
25
|
+
if (source === void 0) {
|
|
26
|
+
throw new Error("The panel UI was not built into this binary. Run `bun run pre-build` before building the server.");
|
|
27
|
+
}
|
|
28
|
+
this.indexSource = source;
|
|
29
|
+
}
|
|
30
|
+
create() {
|
|
31
|
+
return (ctx) => {
|
|
32
|
+
const response = this.handle({
|
|
33
|
+
method: ctx.koa.method,
|
|
34
|
+
path: typeof ctx.params.path === "string" ? ctx.params.path : void 0,
|
|
35
|
+
acceptEncoding: headerValue(ctx.request.headers["accept-encoding"])
|
|
36
|
+
});
|
|
37
|
+
ctx.koa.status = response.status;
|
|
38
|
+
for (const [name, value] of Object.entries(response.headers)) {
|
|
39
|
+
ctx.koa.set(name, value);
|
|
40
|
+
}
|
|
41
|
+
ctx.koa.body = response.body;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
handle(request) {
|
|
45
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
46
|
+
return { status: 405, headers: { "Allow": "GET, HEAD" }, body: Buffer.alloc(0) };
|
|
47
|
+
}
|
|
48
|
+
const path = normalizePath(request.path);
|
|
49
|
+
const gzip = path === void 0 || path === indexAssetPath ? void 0 : this.args.assets.getGzip(path);
|
|
50
|
+
if (path === void 0 || gzip === void 0) {
|
|
51
|
+
this.indexHtml ?? (this.indexHtml = this.renderIndex());
|
|
52
|
+
return this.respond(request, this.indexHtml.gzip, () => this.indexHtml?.identity, {
|
|
53
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
54
|
+
"Cache-Control": "no-store",
|
|
55
|
+
"Content-Security-Policy": contentSecurityPolicy,
|
|
56
|
+
"X-Robots-Tag": "noindex, nofollow",
|
|
57
|
+
"Referrer-Policy": "no-referrer"
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return this.respond(request, gzip, () => this.args.assets.getIdentity(path), {
|
|
61
|
+
"Content-Type": this.args.assets.getContentType(path) ?? "application/octet-stream",
|
|
62
|
+
"Cache-Control": "public, max-age=31536000, immutable"
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
respond(request, gzipped, identity, headers) {
|
|
66
|
+
const common = {
|
|
67
|
+
...headers,
|
|
68
|
+
"Vary": "Accept-Encoding",
|
|
69
|
+
"X-Content-Type-Options": "nosniff"
|
|
70
|
+
};
|
|
71
|
+
if (acceptsGzip(request.acceptEncoding)) {
|
|
72
|
+
return { status: 200, headers: { ...common, "Content-Encoding": "gzip" }, body: request.method === "HEAD" ? Buffer.alloc(0) : gzipped };
|
|
73
|
+
}
|
|
74
|
+
return { status: 200, headers: common, body: request.method === "HEAD" ? Buffer.alloc(0) : identity() ?? Buffer.alloc(0) };
|
|
75
|
+
}
|
|
76
|
+
renderIndex() {
|
|
77
|
+
const config = JSON.stringify({
|
|
78
|
+
basePath: `${this.args.basePath}/`,
|
|
79
|
+
apiBaseUrl: this.args.apiBaseUrl,
|
|
80
|
+
pluginApis: this.args.pluginApis()
|
|
81
|
+
});
|
|
82
|
+
const identity = Buffer.from(
|
|
83
|
+
gunzipSync(this.indexSource).toString("utf8").replaceAll(basePlaceholder, () => `${this.args.basePath}/`).replaceAll(configPlaceholder, () => config)
|
|
84
|
+
);
|
|
85
|
+
return { gzip: gzipSync(identity), identity };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const headerValue = (header) => Array.isArray(header) ? header.join(",") : header;
|
|
89
|
+
const acceptsGzip = (acceptEncoding) => acceptEncoding !== void 0 && /(^|,)\s*(gzip|\*)\s*(;|,|$)/.test(acceptEncoding);
|
|
90
|
+
const normalizePath = (raw) => {
|
|
91
|
+
if (raw === void 0 || raw === "") {
|
|
92
|
+
return void 0;
|
|
93
|
+
}
|
|
94
|
+
let decoded;
|
|
95
|
+
try {
|
|
96
|
+
decoded = decodeURIComponent(raw);
|
|
97
|
+
} catch {
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
if (decoded.includes("\0") || decoded.split("/").includes("..")) {
|
|
101
|
+
return void 0;
|
|
102
|
+
}
|
|
103
|
+
return decoded;
|
|
104
|
+
};
|
|
105
|
+
export {
|
|
106
|
+
PanelController
|
|
107
|
+
};
|
|
108
|
+
//# sourceMappingURL=PanelController.js.map
|