@sixb/app 0.1.16 → 0.1.17
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/README.md +8 -0
- package/dist/codegen.d.ts.map +1 -1
- package/dist/createCustomApp.d.ts.map +1 -1
- package/dist/frame-origins.d.ts +10 -0
- package/dist/frame-origins.d.ts.map +1 -0
- package/dist/server.js +97 -22
- package/dist/server.js.map +6 -5
- package/package.json +2 -2
- package/src/codegen.ts +44 -0
- package/src/createCustomApp.ts +53 -26
- package/src/frame-origins.ts +61 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sixb/app",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "Custom app toolkit for Sixb projects — route scanning, dev serving, and production builds",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sixb",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@sixb/client": "^0.1.23",
|
|
58
|
-
"@sixb/core": "^0.1.
|
|
58
|
+
"@sixb/core": "^0.1.25",
|
|
59
59
|
"@sixb/ui": "^0.1.8",
|
|
60
60
|
"@tailwindcss/cli": "^4.2.2",
|
|
61
61
|
"@xyflow/react": "^12.10.2",
|
package/src/codegen.ts
CHANGED
|
@@ -717,6 +717,49 @@ function InternalLinkInterceptor({ basename }: { readonly basename?: string }) {
|
|
|
717
717
|
return null
|
|
718
718
|
}
|
|
719
719
|
|
|
720
|
+
// The page frames only the API and the sites SIXB_APP_FRAME_ORIGINS lists. The browser logs a
|
|
721
|
+
// refused <iframe> without saying where that list lives: name it, once per site. A site counts
|
|
722
|
+
// only when an <iframe> asked for it: an artifact frame that navigates elsewhere keeps the API's
|
|
723
|
+
// address as its src, and the list is no place for wherever its code tried to go.
|
|
724
|
+
function RefusedFrameReporter() {
|
|
725
|
+
// Subscribed before the browser can report a frame rendered in the same commit.
|
|
726
|
+
React.useLayoutEffect(() => {
|
|
727
|
+
const reported = new Set<string>()
|
|
728
|
+
|
|
729
|
+
function onViolation(event: SecurityPolicyViolationEvent) {
|
|
730
|
+
if (event.effectiveDirective !== "frame-src") return
|
|
731
|
+
|
|
732
|
+
const origin = originOf(event.blockedURI)
|
|
733
|
+
if (!origin || reported.has(origin)) return
|
|
734
|
+
|
|
735
|
+
const frames = Array.from(document.querySelectorAll("iframe"))
|
|
736
|
+
if (!frames.some((frame) => originOf(frame.src) === origin)) return
|
|
737
|
+
|
|
738
|
+
reported.add(origin)
|
|
739
|
+
console.warn(
|
|
740
|
+
"[SixbApp] The app refused to frame " +
|
|
741
|
+
origin +
|
|
742
|
+
". Add each site a frame loads, redirects included, to SIXB_APP_FRAME_ORIGINS on the " +
|
|
743
|
+
"app server."
|
|
744
|
+
)
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
document.addEventListener("securitypolicyviolation", onViolation)
|
|
748
|
+
return () => document.removeEventListener("securitypolicyviolation", onViolation)
|
|
749
|
+
}, [])
|
|
750
|
+
|
|
751
|
+
return null
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function originOf(url: string): string | null {
|
|
755
|
+
try {
|
|
756
|
+
const { origin } = new URL(url)
|
|
757
|
+
return origin === "null" ? null : origin
|
|
758
|
+
} catch {
|
|
759
|
+
return null
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
720
763
|
function pathInsideApp(pathname: string, basename: string | undefined): string {
|
|
721
764
|
if (
|
|
722
765
|
basename &&
|
|
@@ -987,6 +1030,7 @@ function App({ basename }: StartAppOptions) {
|
|
|
987
1030
|
${sectionsStart}
|
|
988
1031
|
<BrowserRouter basename={basename}>
|
|
989
1032
|
<ActivityPageView />
|
|
1033
|
+
<RefusedFrameReporter />
|
|
990
1034
|
<SharedDocumentLinkInterceptor basename={basename} />
|
|
991
1035
|
<InternalLinkInterceptor basename={basename} />
|
|
992
1036
|
{basename ? (
|
package/src/createCustomApp.ts
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
generateRouteManifest,
|
|
21
21
|
} from "./codegen"
|
|
22
22
|
import { devReloadResponse } from "./dev-reload"
|
|
23
|
+
import { readAppFrameOrigins } from "./frame-origins"
|
|
23
24
|
import { collectPublicEnv, renderCustomAppRuntimeScript } from "./runtime"
|
|
24
25
|
import { type PageRoute, routePatternKey, scanAppRoutes } from "./scanner"
|
|
25
26
|
import { sectionCatalog } from "./sections/catalog"
|
|
@@ -354,6 +355,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
354
355
|
|
|
355
356
|
async dev(devOptions: CustomAppDevOptions = {}) {
|
|
356
357
|
const publicEnv = collectPublicEnv()
|
|
358
|
+
const frameOrigins = readAppFrameOrigins(process.env)
|
|
357
359
|
const withRuntime = (html: string) =>
|
|
358
360
|
injectRuntimeConfig(html, { apiBaseUrl, authEnabled, publicEnv })
|
|
359
361
|
const host = devOptions.host ?? "0.0.0.0"
|
|
@@ -397,17 +399,20 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
397
399
|
authEnabled: false,
|
|
398
400
|
publicEnv,
|
|
399
401
|
}),
|
|
400
|
-
apiBaseUrl
|
|
402
|
+
apiBaseUrl,
|
|
403
|
+
frameOrigins
|
|
401
404
|
),
|
|
402
405
|
"/": spaHtmlRoute(() => internalOrigin, {
|
|
403
406
|
shellRoute: privateAppShellRoute,
|
|
404
407
|
transformHtml: withRuntime,
|
|
405
408
|
apiBaseUrl,
|
|
409
|
+
frameOrigins,
|
|
406
410
|
}),
|
|
407
411
|
"/*": spaHtmlRoute(() => internalOrigin, {
|
|
408
412
|
shellRoute: privateAppShellRoute,
|
|
409
413
|
transformHtml: withRuntime,
|
|
410
414
|
apiBaseUrl,
|
|
415
|
+
frameOrigins,
|
|
411
416
|
}),
|
|
412
417
|
},
|
|
413
418
|
}) as Parameters<typeof Bun.serve>[0]
|
|
@@ -577,6 +582,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
577
582
|
|
|
578
583
|
async start(startOptions: CustomAppStartOptions = {}) {
|
|
579
584
|
const publicEnv = collectPublicEnv()
|
|
585
|
+
const frameOrigins = readAppFrameOrigins(process.env)
|
|
580
586
|
const host = startOptions.host ?? "0.0.0.0"
|
|
581
587
|
const port = startOptions.port ?? 3000
|
|
582
588
|
const outdir = resolve(rootDir, startOptions.outdir ?? join(".sixb", "dist", "app"))
|
|
@@ -629,7 +635,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
629
635
|
if (isAssetRequest(url.pathname)) {
|
|
630
636
|
return notFoundResponse()
|
|
631
637
|
}
|
|
632
|
-
return sharedHtmlResponse(req, sharedIndexHtml, runtimeApiBaseUrl)
|
|
638
|
+
return sharedHtmlResponse(req, sharedIndexHtml, runtimeApiBaseUrl, frameOrigins)
|
|
633
639
|
}
|
|
634
640
|
|
|
635
641
|
if (isSharedNamespace(url.pathname)) {
|
|
@@ -637,7 +643,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
637
643
|
}
|
|
638
644
|
|
|
639
645
|
if (url.pathname === "/" || url.pathname === "") {
|
|
640
|
-
return htmlResponse(req, indexHtml, runtimeApiBaseUrl)
|
|
646
|
+
return htmlResponse(req, indexHtml, runtimeApiBaseUrl, frameOrigins)
|
|
641
647
|
}
|
|
642
648
|
|
|
643
649
|
const resolvedPath = resolveStaticPath(outdir, url.pathname)
|
|
@@ -648,7 +654,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
648
654
|
const directFile = Bun.file(resolvedPath)
|
|
649
655
|
if (await directFile.exists()) {
|
|
650
656
|
if (resolvedPath === indexPath) {
|
|
651
|
-
return htmlResponse(req, indexHtml, runtimeApiBaseUrl)
|
|
657
|
+
return htmlResponse(req, indexHtml, runtimeApiBaseUrl, frameOrigins)
|
|
652
658
|
}
|
|
653
659
|
return staticFileResponse(req, resolvedPath, staticAssetHeaders(url.pathname))
|
|
654
660
|
}
|
|
@@ -660,7 +666,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
660
666
|
const htmlFile = Bun.file(`${resolvedPath}.html`)
|
|
661
667
|
if (await htmlFile.exists()) {
|
|
662
668
|
if (`${resolvedPath}.html` === indexPath) {
|
|
663
|
-
return htmlResponse(req, indexHtml, runtimeApiBaseUrl)
|
|
669
|
+
return htmlResponse(req, indexHtml, runtimeApiBaseUrl, frameOrigins)
|
|
664
670
|
}
|
|
665
671
|
return fileResponse(req, htmlFile)
|
|
666
672
|
}
|
|
@@ -670,7 +676,7 @@ export async function createCustomApp(options: CreateCustomAppOptions): Promise<
|
|
|
670
676
|
return fileResponse(req, nestedIndexFile)
|
|
671
677
|
}
|
|
672
678
|
|
|
673
|
-
return htmlResponse(req, indexHtml, runtimeApiBaseUrl)
|
|
679
|
+
return htmlResponse(req, indexHtml, runtimeApiBaseUrl, frameOrigins)
|
|
674
680
|
},
|
|
675
681
|
})
|
|
676
682
|
|
|
@@ -885,7 +891,8 @@ function reservedSixbRoutes(): BunServeRoutes {
|
|
|
885
891
|
|
|
886
892
|
function sharedAppDevRoutes(
|
|
887
893
|
html: () => string | Promise<string>,
|
|
888
|
-
apiBaseUrl: string | undefined
|
|
894
|
+
apiBaseUrl: string | undefined,
|
|
895
|
+
frameOrigins: readonly string[]
|
|
889
896
|
): BunServeRoutes {
|
|
890
897
|
const reject = () => notFoundResponse()
|
|
891
898
|
const guardedHandler = async (request: Request) => {
|
|
@@ -893,7 +900,7 @@ function sharedAppDevRoutes(
|
|
|
893
900
|
if (!isSharedAppRoute(pathname) || isAssetRequest(pathname)) {
|
|
894
901
|
return notFoundResponse()
|
|
895
902
|
}
|
|
896
|
-
return sharedHtmlResponse(request, await html(), apiBaseUrl)
|
|
903
|
+
return sharedHtmlResponse(request, await html(), apiBaseUrl, frameOrigins)
|
|
897
904
|
}
|
|
898
905
|
|
|
899
906
|
return {
|
|
@@ -1061,33 +1068,51 @@ function injectRuntimeConfig(
|
|
|
1061
1068
|
return `${script}\n${html}`
|
|
1062
1069
|
}
|
|
1063
1070
|
|
|
1064
|
-
function htmlResponse(
|
|
1071
|
+
function htmlResponse(
|
|
1072
|
+
request: Request,
|
|
1073
|
+
html: string,
|
|
1074
|
+
apiBaseUrl: string | undefined,
|
|
1075
|
+
frameOrigins: readonly string[]
|
|
1076
|
+
): Response {
|
|
1065
1077
|
return new Response(request.method === "HEAD" ? null : html, {
|
|
1066
1078
|
headers: {
|
|
1067
1079
|
"content-type": "text/html; charset=utf-8",
|
|
1068
1080
|
"cache-control": "no-store",
|
|
1069
|
-
"content-security-policy": appContentSecurityPolicy(request, apiBaseUrl),
|
|
1081
|
+
"content-security-policy": appContentSecurityPolicy(request, apiBaseUrl, frameOrigins),
|
|
1070
1082
|
},
|
|
1071
1083
|
})
|
|
1072
1084
|
}
|
|
1073
1085
|
|
|
1074
1086
|
/**
|
|
1075
|
-
* The policy of the app's own page: it frames
|
|
1076
|
-
* A page in
|
|
1077
|
-
* policy keeps what it loads to its files and a
|
|
1078
|
-
*
|
|
1087
|
+
* The policy of the app's own page: it frames what the API serves, its artifacts' frames, and the
|
|
1088
|
+
* sites `SIXB_APP_FRAME_ORIGINS` lists. A page in an artifact frame reads its queries' rows as the
|
|
1089
|
+
* reader; its frame opens no window, and the frame's policy keeps what it loads to its files and a
|
|
1090
|
+
* few CDNs. This keeps the frame's navigations on those origins too, so a page's code cannot send
|
|
1091
|
+
* what it reads to any other site in an address.
|
|
1079
1092
|
*
|
|
1080
1093
|
* It is the only directive: what a project's own pages load is theirs, but a frame they embed from
|
|
1081
|
-
*
|
|
1094
|
+
* a site the list leaves out is refused too.
|
|
1082
1095
|
*/
|
|
1083
|
-
function appContentSecurityPolicy(
|
|
1084
|
-
|
|
1096
|
+
function appContentSecurityPolicy(
|
|
1097
|
+
request: Request,
|
|
1098
|
+
apiBaseUrl: string | undefined,
|
|
1099
|
+
frameOrigins: readonly string[]
|
|
1100
|
+
): string {
|
|
1101
|
+
return `frame-src ${frameSources(request, apiBaseUrl, frameOrigins)}`
|
|
1085
1102
|
}
|
|
1086
1103
|
|
|
1087
|
-
/**
|
|
1088
|
-
|
|
1104
|
+
/**
|
|
1105
|
+
* Where the page's frames may come from: the API's origin, `'self'` when it is the page's own, then
|
|
1106
|
+
* the listed sites.
|
|
1107
|
+
*/
|
|
1108
|
+
function frameSources(
|
|
1109
|
+
request: Request,
|
|
1110
|
+
apiBaseUrl: string | undefined,
|
|
1111
|
+
frameOrigins: readonly string[]
|
|
1112
|
+
): string {
|
|
1089
1113
|
const { requestOrigin, apiOrigin } = resolveOrigins(request, apiBaseUrl)
|
|
1090
|
-
|
|
1114
|
+
const api = apiOrigin === requestOrigin ? "'self'" : apiOrigin
|
|
1115
|
+
return [api, ...frameOrigins.filter((origin) => origin !== apiOrigin)].join(" ")
|
|
1091
1116
|
}
|
|
1092
1117
|
|
|
1093
1118
|
function resolveOrigins(
|
|
@@ -1106,9 +1131,10 @@ function resolveOrigins(
|
|
|
1106
1131
|
function sharedHtmlResponse(
|
|
1107
1132
|
request: Request,
|
|
1108
1133
|
html: string,
|
|
1109
|
-
apiBaseUrl: string | undefined
|
|
1134
|
+
apiBaseUrl: string | undefined,
|
|
1135
|
+
frameOrigins: readonly string[]
|
|
1110
1136
|
): Response {
|
|
1111
|
-
const secured = secureSharedHtml(html, request, { apiBaseUrl })
|
|
1137
|
+
const secured = secureSharedHtml(html, request, { apiBaseUrl, frameOrigins })
|
|
1112
1138
|
return new Response(request.method === "HEAD" ? null : secured.html, {
|
|
1113
1139
|
headers: {
|
|
1114
1140
|
"content-type": "text/html; charset=utf-8",
|
|
@@ -1120,7 +1146,7 @@ function sharedHtmlResponse(
|
|
|
1120
1146
|
function secureSharedHtml(
|
|
1121
1147
|
html: string,
|
|
1122
1148
|
request: Request,
|
|
1123
|
-
options: { readonly apiBaseUrl?: string }
|
|
1149
|
+
options: { readonly apiBaseUrl?: string; readonly frameOrigins: readonly string[] }
|
|
1124
1150
|
): { readonly html: string; readonly headers: Record<string, string> } {
|
|
1125
1151
|
const nonce = randomBytes(18).toString("base64url")
|
|
1126
1152
|
const { requestOrigin, apiOrigin } = resolveOrigins(request, options.apiBaseUrl)
|
|
@@ -1143,7 +1169,7 @@ function secureSharedHtml(
|
|
|
1143
1169
|
`media-src ${fileSources.join(" ")}`,
|
|
1144
1170
|
"font-src 'self' data: https:",
|
|
1145
1171
|
`connect-src ${connectSources.join(" ")}`,
|
|
1146
|
-
`frame-src ${
|
|
1172
|
+
`frame-src ${frameSources(request, options.apiBaseUrl, options.frameOrigins)}`,
|
|
1147
1173
|
"manifest-src 'none'",
|
|
1148
1174
|
].join("; ")
|
|
1149
1175
|
|
|
@@ -1324,7 +1350,8 @@ function spaHtmlRoute(
|
|
|
1324
1350
|
readonly shellRoute?: string
|
|
1325
1351
|
readonly transformHtml?: (html: string) => string
|
|
1326
1352
|
readonly apiBaseUrl?: string
|
|
1327
|
-
|
|
1353
|
+
readonly frameOrigins: readonly string[]
|
|
1354
|
+
}
|
|
1328
1355
|
): BunServeRoute {
|
|
1329
1356
|
return getHeadRoute(async (request) => {
|
|
1330
1357
|
const publicUrl = new URL(request.url)
|
|
@@ -1359,7 +1386,7 @@ function spaHtmlRoute(
|
|
|
1359
1386
|
responseHeaders.delete("etag")
|
|
1360
1387
|
responseHeaders.set(
|
|
1361
1388
|
"content-security-policy",
|
|
1362
|
-
appContentSecurityPolicy(request, options.apiBaseUrl)
|
|
1389
|
+
appContentSecurityPolicy(request, options.apiBaseUrl, options.frameOrigins)
|
|
1363
1390
|
)
|
|
1364
1391
|
if (request.method === "HEAD") {
|
|
1365
1392
|
return new Response(null, {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const APP_FRAME_ORIGINS_ENV = "SIXB_APP_FRAME_ORIGINS"
|
|
2
|
+
|
|
3
|
+
// A CSP host: labels of letters, digits and hyphens. `URL` keeps `*`, `;` and quotes in a host, and
|
|
4
|
+
// any of them would rewrite the policy instead of naming a site.
|
|
5
|
+
const CSP_HOST = /^[a-z0-9-]+(?:\.[a-z0-9-]+)*$/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The sites besides the API that the app's pages may show in an `<iframe>`, from a comma-separated
|
|
9
|
+
* list such as `https://www.youtube-nocookie.com,https://contoso.sharepoint.com`.
|
|
10
|
+
*
|
|
11
|
+
* They join the `frame-src` that keeps artifact frames on the API's origin, and a frame may navigate
|
|
12
|
+
* itself to any site the policy allows: an artifact page could send what it reads to a site listed
|
|
13
|
+
* here. Each entry is therefore one exact origin, never a wildcard, a scheme, or a keyword.
|
|
14
|
+
*/
|
|
15
|
+
export function readAppFrameOrigins(
|
|
16
|
+
env: Readonly<Record<string, string | undefined>>
|
|
17
|
+
): readonly string[] {
|
|
18
|
+
const entries = (env[APP_FRAME_ORIGINS_ENV] ?? "")
|
|
19
|
+
.split(",")
|
|
20
|
+
.map((entry) => entry.trim())
|
|
21
|
+
.filter((entry) => entry !== "")
|
|
22
|
+
|
|
23
|
+
return [...new Set(entries.map(frameOrigin))]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function frameOrigin(entry: string): string {
|
|
27
|
+
if (entry.includes("*")) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`[SixbCustomApp] ${APP_FRAME_ORIGINS_ENV}: '${entry}' is a wildcard. List each site, such as ` +
|
|
30
|
+
"https://contoso.sharepoint.com: an artifact's frame can navigate to any site this " +
|
|
31
|
+
"variable allows."
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let url: URL
|
|
36
|
+
try {
|
|
37
|
+
url = new URL(entry)
|
|
38
|
+
} catch {
|
|
39
|
+
throw notAnOrigin(entry)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const exact =
|
|
43
|
+
(url.protocol === "https:" || url.protocol === "http:") &&
|
|
44
|
+
CSP_HOST.test(url.hostname) &&
|
|
45
|
+
!url.username &&
|
|
46
|
+
!url.password &&
|
|
47
|
+
url.pathname === "/" &&
|
|
48
|
+
!url.search &&
|
|
49
|
+
!url.hash
|
|
50
|
+
|
|
51
|
+
if (!exact) throw notAnOrigin(entry)
|
|
52
|
+
|
|
53
|
+
return url.origin
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function notAnOrigin(entry: string): Error {
|
|
57
|
+
return new Error(
|
|
58
|
+
`[SixbCustomApp] ${APP_FRAME_ORIGINS_ENV}: '${entry}' is not an origin. Give the scheme, host ` +
|
|
59
|
+
"and optional port of each site, such as https://contoso.sharepoint.com."
|
|
60
|
+
)
|
|
61
|
+
}
|