@notionhq/custom-blocks-dev-shell 0.1.111 → 0.1.113
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 +27 -0
- package/dist/assets/index-DgQZ0zg6.js +11 -0
- package/dist/index.html +1 -1
- package/dist-cli/block-csp.js +84 -0
- package/dist-cli/dev-shell-launcher.js +5 -0
- package/dist-cli/vite-middleware.js +23 -1
- package/package.json +1 -1
- package/dist/assets/index-D2bAuB0Q.js +0 -11
package/dist/index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>Workers local shell</title>
|
|
7
|
-
<script type="module" crossorigin src="/assets/index-
|
|
7
|
+
<script type="module" crossorigin src="/assets/index-DgQZ0zg6.js"></script>
|
|
8
8
|
<link rel="stylesheet" crossorigin href="/assets/index-E1j3FxAu.css">
|
|
9
9
|
</head>
|
|
10
10
|
<body>
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network response policies for custom blocks served by the dev shell.
|
|
3
|
+
* Keep directives, media/font sources, and connection-allowlist semantics in sync with
|
|
4
|
+
* notion-next's production policy. Local origins and the HMR exception are documented below.
|
|
5
|
+
*
|
|
6
|
+
* @see https://github.com/makenotion/notion-next/blob/main/src/shared-customBlocks/customBlockBundleCsp.ts
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Keep these paths in sync with getNotionHostedMediaSources and FILE_S3_URLS.production
|
|
10
|
+
* in notion-next. Media exceptions do not grant fetch or script access.
|
|
11
|
+
*/
|
|
12
|
+
const notionMediaSources = [
|
|
13
|
+
"https://s3-us-west-2.amazonaws.com/public.notion-static.com/",
|
|
14
|
+
"https://s3.us-west-2.amazonaws.com/public.notion-static.com/",
|
|
15
|
+
"https://prod-files-secure.s3.us-west-2.amazonaws.com/",
|
|
16
|
+
"https://prod-files-secure-euc1.s3.eu-central-1.amazonaws.com/",
|
|
17
|
+
"https://prod-files-secure-apne1.s3.ap-northeast-1.amazonaws.com/",
|
|
18
|
+
"https://prod-files-secure-apne2.s3.ap-northeast-2.amazonaws.com/",
|
|
19
|
+
"https://s3-us-west-2.amazonaws.com/temporary.notion-static.com/",
|
|
20
|
+
"https://prod-notion-temporary-files-usw2.s3.us-west-2.amazonaws.com/",
|
|
21
|
+
"https://prod-notion-temporary-files-euc1.s3.eu-central-1.amazonaws.com/",
|
|
22
|
+
"https://prod-notion-temporary-files-apne1.s3.ap-northeast-1.amazonaws.com/",
|
|
23
|
+
"https://prod-notion-temporary-files-apne2.s3.ap-northeast-2.amazonaws.com/",
|
|
24
|
+
];
|
|
25
|
+
/** Builds the CSP and Connection-Allowlist headers for a local custom block response. */
|
|
26
|
+
export function buildCustomBlockDevShellResponseHeaders(args) {
|
|
27
|
+
return {
|
|
28
|
+
"Content-Security-Policy": buildCustomBlockDevShellCsp(args),
|
|
29
|
+
"Connection-Allowlist": buildCustomBlockDevShellConnectionAllowlist(args),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Mirrors notion-next's buildCustomBlockBundleCsp with local bundle and embedding origins.
|
|
34
|
+
* The extra WebSocket source permits Vite HMR without broadening other connection sources.
|
|
35
|
+
*/
|
|
36
|
+
function buildCustomBlockDevShellCsp(args) {
|
|
37
|
+
const { blockOrigin, shellOrigin, allowedNetworkOrigins, allowedFrameAncestors, } = args;
|
|
38
|
+
const approved = allowedNetworkOrigins.join(" ");
|
|
39
|
+
const media = notionMediaSources.join(" ");
|
|
40
|
+
// Production has no HMR connection. Allow only this block's WebSocket host and port
|
|
41
|
+
// so Vite can update modules without granting access to sibling blocks or the shell.
|
|
42
|
+
const hmr = new URL(blockOrigin);
|
|
43
|
+
hmr.protocol = "ws:";
|
|
44
|
+
return [
|
|
45
|
+
"default-src 'self'",
|
|
46
|
+
"worker-src 'none'",
|
|
47
|
+
"child-src 'none'",
|
|
48
|
+
`frame-src ${approved || "'none'"}`,
|
|
49
|
+
"object-src 'none'",
|
|
50
|
+
"manifest-src 'none'",
|
|
51
|
+
`media-src 'self' ${media} ${approved}`,
|
|
52
|
+
// The local HTTP origin replaces production's definition-specific bundle sources.
|
|
53
|
+
`connect-src ${blockOrigin} ${hmr.origin} ${approved}`,
|
|
54
|
+
`img-src 'self' data: ${media} ${approved}`,
|
|
55
|
+
`font-src 'self' data: https://fonts.gstatic.com ${approved}`,
|
|
56
|
+
"base-uri 'none'",
|
|
57
|
+
"form-action 'none'",
|
|
58
|
+
// Local hosts replace Notion's production app, relay, and mobile-shell ancestors.
|
|
59
|
+
`frame-ancestors 'self' ${shellOrigin} ${allowedFrameAncestors.join(" ")}`,
|
|
60
|
+
"script-src 'self' 'unsafe-inline'",
|
|
61
|
+
`style-src 'self' 'unsafe-inline' https://fonts.googleapis.com ${approved}`,
|
|
62
|
+
]
|
|
63
|
+
.map(directive => directive.trim())
|
|
64
|
+
.join("; ");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Mirrors notion-next's buildCustomBlockBundleConnectionAllowlist, including redirect and
|
|
68
|
+
* WebRTC blocking. The local block origin replaces production's bundle sources.
|
|
69
|
+
*/
|
|
70
|
+
function buildCustomBlockDevShellConnectionAllowlist(args) {
|
|
71
|
+
const sources = [
|
|
72
|
+
// WebSocket matching rewrites ws: to http:, so this also permits Vite HMR.
|
|
73
|
+
args.blockOrigin,
|
|
74
|
+
...notionMediaSources,
|
|
75
|
+
"https://fonts.googleapis.com",
|
|
76
|
+
"https://fonts.gstatic.com",
|
|
77
|
+
...args.allowedNetworkOrigins,
|
|
78
|
+
];
|
|
79
|
+
// CSP path prefixes need an explicit wildcard in Connection-Allowlist URLPatterns.
|
|
80
|
+
const patterns = sources.map(source => JSON.stringify(source === "*"
|
|
81
|
+
? "*://*:*/*"
|
|
82
|
+
: `${source}${source.endsWith("/") ? "" : "/"}*`));
|
|
83
|
+
return `(${patterns.join(" ")}); redirects=block; webrtc=block`;
|
|
84
|
+
}
|
|
@@ -203,6 +203,11 @@ export async function launchDevShell(options) {
|
|
|
203
203
|
const close = await mountVite({
|
|
204
204
|
projectDir: workerDir,
|
|
205
205
|
hostname: block.hostname,
|
|
206
|
+
// TODO(custom-blocks): Let users limit network origins and embedding hosts.
|
|
207
|
+
blockPolicy: {
|
|
208
|
+
allowedNetworkOrigins: ["*"],
|
|
209
|
+
allowedFrameAncestors: ["*"],
|
|
210
|
+
},
|
|
206
211
|
shared,
|
|
207
212
|
config: { configFile: block.configFile, root: block.blockDir },
|
|
208
213
|
});
|
|
@@ -2,9 +2,17 @@ import { createServer } from "node:http";
|
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { buildCustomBlockDevShellResponseHeaders, } from "./block-csp.js";
|
|
5
6
|
/** Resolve Vite from the project, including for the dependency-free published CLI. */
|
|
6
7
|
export async function mountVite(options) {
|
|
7
8
|
const { projectDir, hostname, shared, config } = options;
|
|
9
|
+
const blockHeaders = options.blockPolicy === false
|
|
10
|
+
? undefined
|
|
11
|
+
: buildCustomBlockDevShellResponseHeaders({
|
|
12
|
+
blockOrigin: `http://${hostname}:${shared.port}`,
|
|
13
|
+
shellOrigin: `http://localhost:${shared.port}`,
|
|
14
|
+
...options.blockPolicy,
|
|
15
|
+
});
|
|
8
16
|
let vitePath;
|
|
9
17
|
try {
|
|
10
18
|
vitePath = createRequire(join(projectDir, "package.json")).resolve("vite");
|
|
@@ -32,6 +40,13 @@ export async function mountVite(options) {
|
|
|
32
40
|
configResolved(resolved) {
|
|
33
41
|
resolved.server.middlewareMode = { server: upgrades };
|
|
34
42
|
resolved.server.open = false;
|
|
43
|
+
if (blockHeaders !== undefined) {
|
|
44
|
+
// Vite's asset middleware writes these headers after the route middleware.
|
|
45
|
+
resolved.server.headers = {
|
|
46
|
+
...resolved.server.headers,
|
|
47
|
+
...blockHeaders,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
35
50
|
resolved.server.origin = `http://${hostname}:${shared.port}`;
|
|
36
51
|
const ws = {
|
|
37
52
|
server: upgrades,
|
|
@@ -74,7 +89,14 @@ export async function mountVite(options) {
|
|
|
74
89
|
throw error;
|
|
75
90
|
}
|
|
76
91
|
shared.routes.set(hostname, {
|
|
77
|
-
handle: (req, res) =>
|
|
92
|
+
handle: (req, res) => {
|
|
93
|
+
if (blockHeaders !== undefined) {
|
|
94
|
+
for (const [name, value] of Object.entries(blockHeaders)) {
|
|
95
|
+
res.setHeader(name, value);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
server.middlewares(req, res);
|
|
99
|
+
},
|
|
78
100
|
upgrades,
|
|
79
101
|
});
|
|
80
102
|
return async () => {
|