@prismicio/svelte 3.0.0-pr.71.949ea7f → 3.0.0-pr.71.98fd16f
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/dist/lib/getSafePath.d.ts +4 -1
- package/dist/lib/getSafePath.js +15 -4
- package/package.json +1 -1
- package/src/lib/getSafePath.ts +14 -4
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
/**
|
|
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
|
+
*/
|
|
2
5
|
export declare function getSafePath(path: string | null | undefined): string | undefined;
|
package/dist/lib/getSafePath.js
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
/**
|
|
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
|
+
*/
|
|
2
5
|
export function getSafePath(path) {
|
|
3
6
|
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
|
|
4
7
|
return;
|
|
5
8
|
}
|
|
6
9
|
const origin = "https://placeholder.invalid";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
10
21
|
}
|
|
11
22
|
}
|
package/package.json
CHANGED
package/src/lib/getSafePath.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
/**
|
|
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
|
+
*/
|
|
2
5
|
export function getSafePath(path: string | null | undefined): string | undefined {
|
|
3
6
|
if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
|
|
4
7
|
return
|
|
5
8
|
}
|
|
6
9
|
|
|
7
10
|
const origin = "https://placeholder.invalid"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
11
21
|
}
|
|
12
22
|
}
|