@prismicio/svelte 3.0.0-pr.71.08694b9 → 3.0.0-pr.71.249fd8d

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.
@@ -19,20 +19,9 @@
19
19
  * Only set this value if the route parameter's name differs from the parameter's value.
20
20
  */
21
21
  routePrefixName?: string
22
-
23
- /**
24
- * The URL of your preview route, such as `/api/preview`. The Prismic Toolbar uses it to create
25
- * share links, and hides its Share button without it.
26
- */
27
- previewURL?: string
28
22
  }
29
23
 
30
- const {
31
- repositoryName,
32
- routePrefix = "preview",
33
- routePrefixName = routePrefix,
34
- previewURL,
35
- }: Props = $props()
24
+ const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
36
25
 
37
26
  const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
38
27
 
@@ -100,5 +89,5 @@
100
89
  </script>
101
90
 
102
91
  <svelte:head>
103
- <script defer src={toolbarSrc} data-preview-url={previewURL}></script>
92
+ <script defer src={toolbarSrc}></script>
104
93
  </svelte:head>
@@ -12,11 +12,6 @@ type Props = {
12
12
  * Only set this value if the route parameter's name differs from the parameter's value.
13
13
  */
14
14
  routePrefixName?: string;
15
- /**
16
- * The URL of your preview route, such as `/api/preview`. The Prismic Toolbar uses it to create
17
- * share links, and hides its Share button without it.
18
- */
19
- previewURL?: string;
20
15
  };
21
16
  declare const PrismicPreview: import("svelte").Component<Props, {}, "">;
22
17
  type PrismicPreview = ReturnType<typeof PrismicPreview>;
@@ -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,14 @@ 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 (url.origin !== origin || !url.pathname.startsWith(`${base}/`)) {
66
+ const rest = path?.startsWith(base) ? path.slice(base.length) : undefined;
67
+ // `/docs` and `/docs?q=1` are under the `/docs` base, but `/docs-old` isn't.
68
+ if (rest === undefined || !/^([/?#]|$)/.test(rest)) {
72
69
  return;
73
70
  }
74
- const pathname = url.pathname.slice(base.length);
75
- if (pathname !== `/${routePrefix}` && !pathname.startsWith(`/${routePrefix}/`)) {
76
- url.pathname = `${base}/${routePrefix}${pathname}`;
77
- }
78
- return url.pathname + url.search + url.hash;
71
+ return /^\/([^/?#]*)/.exec(rest)?.[1] === routePrefix ? path : `${base}/${routePrefix}${rest}`;
79
72
  };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
3
+ * `location`, so it already includes `paths.base`.
4
+ */
5
+ export declare function getSafePath(path: string | null | undefined): string | undefined;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
3
+ * `location`, so it already includes `paths.base`.
4
+ */
5
+ export function getSafePath(path) {
6
+ if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
7
+ return;
8
+ }
9
+ const origin = "https://placeholder.invalid";
10
+ let url;
11
+ try {
12
+ url = new URL(path, origin);
13
+ }
14
+ catch {
15
+ return;
16
+ }
17
+ const safePath = url.href.slice(origin.length);
18
+ // Normalizing can create `//`, like with `/.//example.com`.
19
+ if (url.origin === origin && !safePath.startsWith("//")) {
20
+ return safePath;
21
+ }
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/svelte",
3
- "version": "3.0.0-pr.71.08694b9",
3
+ "version": "3.0.0-pr.71.249fd8d",
4
4
  "description": "Svelte components to present Prismic content.",
5
5
  "keywords": [
6
6
  "prismic",
@@ -62,7 +62,7 @@
62
62
  "esm-env": "^1.2.2"
63
63
  },
64
64
  "devDependencies": {
65
- "@prismicio/client": "7.22.3-pr.477.9eb2102",
65
+ "@prismicio/client": "^7.23.0",
66
66
  "@prismicio/mock": "^0.7.2",
67
67
  "@sveltejs/kit": "^3.0.1",
68
68
  "@sveltejs/package": "^3.0.1",
@@ -71,7 +71,7 @@
71
71
  "@types/node": "^26.6.5",
72
72
  "@vitest/coverage-v8": "^5.0.5",
73
73
  "globals": "^17.13.0",
74
- "happy-dom": "^21.0.3",
74
+ "happy-dom": "^21.0.4",
75
75
  "oxfmt": "^0.74.0",
76
76
  "oxlint": "^1.89.0",
77
77
  "standard-version": "^9.5.0",
@@ -89,9 +89,6 @@
89
89
  "optional": true
90
90
  }
91
91
  },
92
- "overrides": {
93
- "@prismicio/client": "$@prismicio/client"
94
- },
95
92
  "engines": {
96
93
  "node": ">=22"
97
94
  }
@@ -19,20 +19,9 @@
19
19
  * Only set this value if the route parameter's name differs from the parameter's value.
20
20
  */
21
21
  routePrefixName?: string
22
-
23
- /**
24
- * The URL of your preview route, such as `/api/preview`. The Prismic Toolbar uses it to create
25
- * share links, and hides its Share button without it.
26
- */
27
- previewURL?: string
28
22
  }
29
23
 
30
- const {
31
- repositoryName,
32
- routePrefix = "preview",
33
- routePrefixName = routePrefix,
34
- previewURL,
35
- }: Props = $props()
24
+ const { repositoryName, routePrefix = "preview", routePrefixName = routePrefix }: Props = $props()
36
25
 
37
26
  const toolbarSrc = $derived(getToolbarSrc(repositoryName, { version: 5 }))
38
27
 
@@ -100,5 +89,5 @@
100
89
  </script>
101
90
 
102
91
  <svelte:head>
103
- <script defer src={toolbarSrc} data-preview-url={previewURL}></script>
92
+ <script defer src={toolbarSrc}></script>
104
93
  </svelte:head>
@@ -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 (url.origin !== origin || !url.pathname.startsWith(`${base}/`)) {
105
+ const rest = path?.startsWith(base) ? path.slice(base.length) : undefined
106
+ // `/docs` and `/docs?q=1` are under the `/docs` base, but `/docs-old` isn't.
107
+ if (rest === undefined || !/^([/?#]|$)/.test(rest)) {
111
108
  return
112
109
  }
113
110
 
114
- const pathname = url.pathname.slice(base.length)
115
- if (pathname !== `/${routePrefix}` && !pathname.startsWith(`/${routePrefix}/`)) {
116
- url.pathname = `${base}/${routePrefix}${pathname}`
117
- }
118
-
119
- return url.pathname + url.search + url.hash
111
+ return /^\/([^/?#]*)/.exec(rest)?.[1] === routePrefix ? path : `${base}/${routePrefix}${rest}`
120
112
  }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Returns `path` if it stays on the current origin, like `/blog?page=2#top`. It comes from
3
+ * `location`, so it already includes `paths.base`.
4
+ */
5
+ export function getSafePath(path: string | null | undefined): string | undefined {
6
+ if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
7
+ return
8
+ }
9
+
10
+ const origin = "https://placeholder.invalid"
11
+ let url: URL
12
+ try {
13
+ url = new URL(path, origin)
14
+ } catch {
15
+ return
16
+ }
17
+ const safePath = url.href.slice(origin.length)
18
+ // Normalizing can create `//`, like with `/.//example.com`.
19
+ if (url.origin === origin && !safePath.startsWith("//")) {
20
+ return safePath
21
+ }
22
+ }