@prismicio/svelte 3.0.0-pr.71.08694b9 → 3.0.0-pr.71.35fb12f
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.
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { resolve } from "$app/paths";
|
|
2
2
|
import * as prismic from "@prismicio/client";
|
|
3
|
+
import { getSafePath } from "../lib/getSafePath";
|
|
3
4
|
/**
|
|
4
5
|
* Redirects a visitor to the URL of a previewed Prismic document from within a SvelteKit `+server`
|
|
5
6
|
* file.
|
|
@@ -58,22 +59,13 @@ export const redirectToPreviewURL = async (config) => {
|
|
|
58
59
|
* if it is missing or not a path on this website.
|
|
59
60
|
*/
|
|
60
61
|
const getSharedPath = (requestURL, routePrefix) => {
|
|
61
|
-
const path = new URL(requestURL).searchParams.get("path");
|
|
62
|
-
if (!path?.startsWith("/")) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
// Paths like `//evil.com` and `/\evil.com` resolve to another origin.
|
|
66
|
-
const origin = "https://placeholder.invalid";
|
|
67
|
-
const url = new URL(path, origin);
|
|
62
|
+
const path = getSafePath(new URL(requestURL).searchParams.get("path"));
|
|
68
63
|
// `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
|
|
69
64
|
// either absolute or relative to the request.
|
|
70
65
|
const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1);
|
|
71
|
-
if (
|
|
66
|
+
if (!path?.startsWith(`${base}/`)) {
|
|
72
67
|
return;
|
|
73
68
|
}
|
|
74
|
-
const pathname =
|
|
75
|
-
|
|
76
|
-
url.pathname = `${base}/${routePrefix}${pathname}`;
|
|
77
|
-
}
|
|
78
|
-
return url.pathname + url.search + url.hash;
|
|
69
|
+
const pathname = path.slice(base.length);
|
|
70
|
+
return pathname.split(/[/?#]/)[1] === routePrefix ? path : `${base}/${routePrefix}${pathname}`;
|
|
79
71
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
|
|
2
|
+
export function getSafePath(path) {
|
|
3
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const origin = "https://placeholder.invalid";
|
|
7
|
+
const url = new URL(path, origin);
|
|
8
|
+
if (url.origin === origin) {
|
|
9
|
+
return url.href.slice(origin.length);
|
|
10
|
+
}
|
|
11
|
+
}
|
package/package.json
CHANGED
|
@@ -2,6 +2,8 @@ import { resolve } from "$app/paths"
|
|
|
2
2
|
import * as prismic from "@prismicio/client"
|
|
3
3
|
import type { Cookies } from "@sveltejs/kit"
|
|
4
4
|
|
|
5
|
+
import { getSafePath } from "../lib/getSafePath"
|
|
6
|
+
|
|
5
7
|
export type RedirectToPreviewURLConfig = {
|
|
6
8
|
/** The Prismic client configured for the preview session's repository. */
|
|
7
9
|
// `Pick` is used to use the smallest possible subset of
|
|
@@ -96,25 +98,15 @@ export const redirectToPreviewURL = async (
|
|
|
96
98
|
* if it is missing or not a path on this website.
|
|
97
99
|
*/
|
|
98
100
|
const getSharedPath = (requestURL: string, routePrefix: string): string | undefined => {
|
|
99
|
-
const path = new URL(requestURL).searchParams.get("path")
|
|
100
|
-
if (!path?.startsWith("/")) {
|
|
101
|
-
return
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Paths like `//evil.com` and `/\evil.com` resolve to another origin.
|
|
105
|
-
const origin = "https://placeholder.invalid"
|
|
106
|
-
const url = new URL(path, origin)
|
|
101
|
+
const path = getSafePath(new URL(requestURL).searchParams.get("path"))
|
|
107
102
|
// `path` comes from the browser, so it already includes `paths.base`. `resolve()` returns `base`
|
|
108
103
|
// either absolute or relative to the request.
|
|
109
104
|
const base = new URL(resolve("/", {}), requestURL).pathname.slice(0, -1)
|
|
110
|
-
if (
|
|
105
|
+
if (!path?.startsWith(`${base}/`)) {
|
|
111
106
|
return
|
|
112
107
|
}
|
|
113
108
|
|
|
114
|
-
const pathname =
|
|
115
|
-
if (pathname !== `/${routePrefix}` && !pathname.startsWith(`/${routePrefix}/`)) {
|
|
116
|
-
url.pathname = `${base}/${routePrefix}${pathname}`
|
|
117
|
-
}
|
|
109
|
+
const pathname = path.slice(base.length)
|
|
118
110
|
|
|
119
|
-
return
|
|
111
|
+
return pathname.split(/[/?#]/)[1] === routePrefix ? path : `${base}/${routePrefix}${pathname}`
|
|
120
112
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Returns `path` if it stays on the current origin, like `/blog?page=2#top`. */
|
|
2
|
+
export function getSafePath(path: string | null | undefined): string | undefined {
|
|
3
|
+
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
|
|
4
|
+
return
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const origin = "https://placeholder.invalid"
|
|
8
|
+
const url = new URL(path, origin)
|
|
9
|
+
if (url.origin === origin) {
|
|
10
|
+
return url.href.slice(origin.length)
|
|
11
|
+
}
|
|
12
|
+
}
|