@notionhq/custom-blocks-dev-shell 0.1.111 → 0.1.112
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-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/README.md
CHANGED
|
@@ -45,6 +45,33 @@ The shell and each block use separate origins, so a block cannot access the shel
|
|
|
45
45
|
Instances of the same block share `localStorage`; `sessionStorage` is also scoped to the browser
|
|
46
46
|
tab.
|
|
47
47
|
|
|
48
|
+
Block responses enforce a production-like content security policy (CSP) and send a
|
|
49
|
+
`Connection-Allowlist` header:
|
|
50
|
+
|
|
51
|
+
- **Blocked features:** Native form submission, browser workers, `<object>`, `<embed>`, web app
|
|
52
|
+
manifests, and `<base>`.
|
|
53
|
+
- **Scripts and styles:** Bundled scripts and inline scripts/styles work. Runtime CDN scripts are
|
|
54
|
+
blocked.
|
|
55
|
+
- **Media and fonts:** Notion-hosted media and Google Fonts use the same exceptions as production.
|
|
56
|
+
- **External access:** Network requests and nested frames may use any origin for now. Browser CORS
|
|
57
|
+
checks still apply.
|
|
58
|
+
|
|
59
|
+
#### Differences from production
|
|
60
|
+
|
|
61
|
+
- **Local origins:** Blocks and the shell use HTTP localhost origins instead of production HTTPS
|
|
62
|
+
origins.
|
|
63
|
+
- **Hot reload:** CSP permits the block's exact WebSocket host and port for Vite HMR.
|
|
64
|
+
- **External access:** Production uses workspace-approved external origins. The dev shell currently
|
|
65
|
+
allows all origins; configurable limits will be added later.
|
|
66
|
+
- **Embedding hosts:** Any host may embed local blocks for now, including Notion apps in every
|
|
67
|
+
environment. Configurable limits will be added later.
|
|
68
|
+
- **Parent navigation policy:** The shell does not apply production's parent navigation CSP. The
|
|
69
|
+
block's CSP alone cannot prevent it from navigating its own iframe to another document.
|
|
70
|
+
- **Sample data:** SDK handlers use local sample data instead of production permissions and
|
|
71
|
+
persistence.
|
|
72
|
+
- **Developer control:** Vite plugins are developer-controlled, so the preview is not a security
|
|
73
|
+
boundary for untrusted code.
|
|
74
|
+
|
|
48
75
|
### Error surface
|
|
49
76
|
|
|
50
77
|
The preview shows an error when a block cannot initialize or an uncaught runtime error prevents
|
|
@@ -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 () => {
|