@backmatter/core 0.0.59 → 0.0.61
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 +1 -1
- package/src/preview-session.ts +11 -0
- package/src/preview.ts +0 -22
package/package.json
CHANGED
package/src/preview-session.ts
CHANGED
|
@@ -41,6 +41,11 @@ export type PreviewSessionClaims = {
|
|
|
41
41
|
role: string;
|
|
42
42
|
epoch: number;
|
|
43
43
|
exp: number;
|
|
44
|
+
/**
|
|
45
|
+
* `view` follows its own branch whatever the person's checkout is, and
|
|
46
|
+
* cannot write. Absent is a full session that follows the checkout.
|
|
47
|
+
*/
|
|
48
|
+
mode?: "view";
|
|
44
49
|
};
|
|
45
50
|
|
|
46
51
|
export type PreviewCookiePayload = {
|
|
@@ -53,6 +58,11 @@ export type PreviewCookiePayload = {
|
|
|
53
58
|
export function isSafeRedirectPath(path: string): boolean {
|
|
54
59
|
if (!path.startsWith("/") || path.startsWith("//")) return false;
|
|
55
60
|
if (path.includes("://")) return false;
|
|
61
|
+
// Browsers read `\` as `/` and drop tabs and newlines, so `/\evil.example` is another host.
|
|
62
|
+
for (const char of path) {
|
|
63
|
+
const code = char.charCodeAt(0);
|
|
64
|
+
if (char === "\\" || code < 0x20 || code === 0x7f) return false;
|
|
65
|
+
}
|
|
56
66
|
return true;
|
|
57
67
|
}
|
|
58
68
|
|
|
@@ -135,6 +145,7 @@ function asSession(value: unknown): PreviewSessionClaims | null {
|
|
|
135
145
|
role: v.role,
|
|
136
146
|
epoch: v.epoch,
|
|
137
147
|
exp: v.exp,
|
|
148
|
+
...(v.mode === "view" ? { mode: "view" as const } : {}),
|
|
138
149
|
};
|
|
139
150
|
}
|
|
140
151
|
|
package/src/preview.ts
CHANGED
|
@@ -166,28 +166,6 @@ export async function setHostSitePreview(origin = ""): Promise<boolean> {
|
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
export type CmsSessionUser = { id: string; email: string; name: string };
|
|
170
|
-
|
|
171
|
-
/** Cookie session on the CMS API. Fail closed: empty URL, 401, errors → null. */
|
|
172
|
-
export async function fetchCmsSession(
|
|
173
|
-
apiUrl: string,
|
|
174
|
-
): Promise<CmsSessionUser | null> {
|
|
175
|
-
const base = baseUrl(apiUrl.trim());
|
|
176
|
-
if (!base) return null;
|
|
177
|
-
try {
|
|
178
|
-
const res = await fetch(`${base}/me`, {
|
|
179
|
-
credentials: "include",
|
|
180
|
-
cache: "no-store",
|
|
181
|
-
});
|
|
182
|
-
if (!res.ok) return null;
|
|
183
|
-
const data = (await res.json()) as { user?: CmsSessionUser };
|
|
184
|
-
if (!data.user?.id) return null;
|
|
185
|
-
return data.user;
|
|
186
|
-
} catch {
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
169
|
function previewParams(options: PreviewClientOptions, ref: PreviewDocRef) {
|
|
192
170
|
const params = new URLSearchParams({ token: options.previewToken });
|
|
193
171
|
if (ref.branch) params.set("branch", ref.branch);
|