@backmatter/next 0.0.77 → 0.0.79
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/package.json +3 -3
- package/src/cms-proxy.ts +17 -0
- package/src/devbar-client.tsx +19 -33
- package/src/devbar.tsx +0 -1
- package/src/preview-route.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backmatter/next",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.79",
|
|
4
4
|
"description": "Experimental Next.js adapter for Backmatter. APIs will break. Not ready for production.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -48,10 +48,10 @@
|
|
|
48
48
|
"test": "bun test"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@backmatter/core": "^0.0.
|
|
51
|
+
"@backmatter/core": "^0.0.61",
|
|
52
52
|
"@mdx-js/mdx": "^3.1.1",
|
|
53
53
|
"remark-gfm": "^4.0.1",
|
|
54
|
-
"@backmatter/studio": "^0.0.
|
|
54
|
+
"@backmatter/studio": "^0.0.56"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"next": ">=16.3.0",
|
package/src/cms-proxy.ts
CHANGED
|
@@ -101,6 +101,22 @@ export function siteOrigin(req: Request, url: URL): string {
|
|
|
101
101
|
return `${proto}://${host}`;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Whether a write may use the site session. The cookie is SameSite=None on
|
|
106
|
+
* HTTPS, so a form on any page can post here with it. Browsers mark every
|
|
107
|
+
* cross-site write with Sec-Fetch-Site or Origin; the site's own pages send
|
|
108
|
+
* same-origin. A request with neither header is not from a browser, so it
|
|
109
|
+
* cannot carry a visitor's cookie by accident.
|
|
110
|
+
*/
|
|
111
|
+
export function fromThisSite(req: Request, url: URL): boolean {
|
|
112
|
+
if (req.method === "GET" || req.method === "HEAD") return true;
|
|
113
|
+
const fetchSite = req.headers.get("sec-fetch-site");
|
|
114
|
+
if (fetchSite) return fetchSite === "same-origin";
|
|
115
|
+
const origin = req.headers.get("origin");
|
|
116
|
+
if (origin) return origin === siteOrigin(req, url);
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
104
120
|
function cookieValue(header: string | null, name: string): string | undefined {
|
|
105
121
|
if (!header) return undefined;
|
|
106
122
|
for (const part of header.split(";")) {
|
|
@@ -130,6 +146,7 @@ export function createCmsProxy(
|
|
|
130
146
|
if (!secret || !apiUrl) return Response.json({ error: "preview_not_configured" }, { status: 503 });
|
|
131
147
|
|
|
132
148
|
const url = new URL(req.url);
|
|
149
|
+
if (!fromThisSite(req, url)) return Response.json({ error: "forbidden_origin" }, { status: 403 });
|
|
133
150
|
const marker = "/api/backmatter/cms";
|
|
134
151
|
const at = url.pathname.indexOf(marker);
|
|
135
152
|
const path = at === -1 ? "" : url.pathname.slice(at + marker.length) || "/";
|
package/src/devbar-client.tsx
CHANGED
|
@@ -5,12 +5,10 @@ import { Suspense, lazy, startTransition, useCallback, useEffect, useLayoutEffec
|
|
|
5
5
|
import { useRouter } from "next/navigation";
|
|
6
6
|
import {
|
|
7
7
|
clearHostPreviewSession,
|
|
8
|
-
fetchCmsSession,
|
|
9
8
|
fetchHostPreviewSession,
|
|
10
9
|
setHostSiteLive,
|
|
11
10
|
setHostSitePreview,
|
|
12
11
|
resolveDevMode,
|
|
13
|
-
type CmsSessionUser,
|
|
14
12
|
type HostSessionReason,
|
|
15
13
|
} from "@backmatter/core/preview";
|
|
16
14
|
import {
|
|
@@ -28,13 +26,10 @@ import { DEVBAR_CLASS, DEVBAR_ENTER_CSS, devbarShellStyle } from "./devbar-motio
|
|
|
28
26
|
/** The bar and its panels load only after the gate opens. */
|
|
29
27
|
const SiteChrome = lazy(() => import("./site-editor").then((m) => ({ default: m.SiteChrome })));
|
|
30
28
|
|
|
31
|
-
export type { CmsSessionUser };
|
|
32
|
-
|
|
33
29
|
export type DevModeState =
|
|
34
30
|
| { status: "hidden"; reason?: HostSessionReason }
|
|
35
31
|
| {
|
|
36
32
|
status: "ready";
|
|
37
|
-
user?: CmsSessionUser;
|
|
38
33
|
mode: "preview" | "live";
|
|
39
34
|
branch?: string;
|
|
40
35
|
tree?: string;
|
|
@@ -81,53 +76,44 @@ function rememberShown(next: DevModeState) {
|
|
|
81
76
|
shownRev += 1;
|
|
82
77
|
}
|
|
83
78
|
|
|
84
|
-
export function devModeState(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
93
|
-
): DevModeState {
|
|
94
|
-
if (!user && !session.enabled && !session.signedIn) {
|
|
79
|
+
export function devModeState(session: {
|
|
80
|
+
enabled: boolean;
|
|
81
|
+
signedIn?: boolean;
|
|
82
|
+
branch?: string;
|
|
83
|
+
tree?: string;
|
|
84
|
+
reason?: HostSessionReason;
|
|
85
|
+
}): DevModeState {
|
|
86
|
+
if (!session.enabled && !session.signedIn) {
|
|
95
87
|
return session.reason ? { status: "hidden", reason: session.reason } : { status: "hidden" };
|
|
96
88
|
}
|
|
97
89
|
const resolved = resolveDevMode(session);
|
|
98
|
-
return {
|
|
99
|
-
status: "ready",
|
|
100
|
-
...(user ? { user } : {}),
|
|
101
|
-
...resolved,
|
|
102
|
-
};
|
|
90
|
+
return { status: "ready", ...resolved };
|
|
103
91
|
}
|
|
104
92
|
|
|
93
|
+
/**
|
|
94
|
+
* The site's own session decides the bar. It never calls the CMS API: only
|
|
95
|
+
* the dashboard may use the person's cookie across origins.
|
|
96
|
+
*/
|
|
105
97
|
export async function loadDevModeState(
|
|
106
|
-
apiUrl: string,
|
|
107
|
-
fetchUser: typeof fetchCmsSession = fetchCmsSession,
|
|
108
98
|
fetchSession: typeof fetchHostPreviewSession = fetchHostPreviewSession,
|
|
109
99
|
): Promise<DevModeState> {
|
|
110
|
-
|
|
111
|
-
const [user, session] = await Promise.all([
|
|
112
|
-
trimmed ? fetchUser(trimmed) : Promise.resolve(null),
|
|
113
|
-
fetchSession(""),
|
|
114
|
-
]);
|
|
115
|
-
return devModeState(user, session);
|
|
100
|
+
return devModeState(await fetchSession(""));
|
|
116
101
|
}
|
|
117
102
|
|
|
118
|
-
|
|
103
|
+
/** `apiUrl` is ignored; kept so older host code still compiles. */
|
|
104
|
+
export function useDevMode(_options?: { apiUrl?: string }): DevModeState {
|
|
119
105
|
const [state, setState] = useState<DevModeState>({ status: "hidden" });
|
|
120
106
|
|
|
121
107
|
useEffect(() => {
|
|
122
108
|
let cancelled = false;
|
|
123
|
-
void loadDevModeState(
|
|
109
|
+
void loadDevModeState().then((next) => {
|
|
124
110
|
if (cancelled) return;
|
|
125
111
|
setState(next);
|
|
126
112
|
});
|
|
127
113
|
return () => {
|
|
128
114
|
cancelled = true;
|
|
129
115
|
};
|
|
130
|
-
}, [
|
|
116
|
+
}, []);
|
|
131
117
|
|
|
132
118
|
return state;
|
|
133
119
|
}
|
|
@@ -376,7 +362,7 @@ function useSiteSession(
|
|
|
376
362
|
if (gate === "closed") return;
|
|
377
363
|
const rev = shownRev;
|
|
378
364
|
let cancelled = false;
|
|
379
|
-
void loadDevModeState(
|
|
365
|
+
void loadDevModeState().then((next) => {
|
|
380
366
|
if (cancelled || rev !== shownRev) return;
|
|
381
367
|
const showBar = next.status === "ready";
|
|
382
368
|
const reason = next.status === "hidden" ? (next.reason ?? "anonymous") : "anonymous";
|
package/src/devbar.tsx
CHANGED
package/src/preview-route.ts
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
import { previewStreamUrl, previewTreeStreamUrl } from "@backmatter/core/preview";
|
|
18
18
|
import type { BackmatterClient } from "@backmatter/core/node/create-backmatter";
|
|
19
19
|
import { MARKER_COOKIE, MARKER_TTL_S, safeReturnPath } from "./gate";
|
|
20
|
+
import { fromThisSite } from "./cms-proxy";
|
|
20
21
|
import { requestUrlFrom } from "./request-url";
|
|
21
22
|
|
|
22
23
|
export type PreviewRouteDeps = {
|
|
@@ -352,6 +353,8 @@ export function createPreviewRoute(
|
|
|
352
353
|
|
|
353
354
|
const POST = async (req: Request): Promise<Response> => {
|
|
354
355
|
const url = new URL(req.url);
|
|
356
|
+
// Every POST here changes the visitor's cookies, so only the site's own pages may send one.
|
|
357
|
+
if (!fromThisSite(req, url)) return Response.json({ error: "forbidden_origin" }, { status: 403 });
|
|
355
358
|
// Slide the site session while the bar is open. The cookie is HttpOnly, so
|
|
356
359
|
// the browser cannot renew it itself; a dead session is a 401 and the bar
|
|
357
360
|
// bounces to the dashboard instead of failing the next chat send.
|