@uniformdev/canvas-sveltekit 20.48.1-alpha.48.sha-989c5a3792 → 20.89.0
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,8 +1,7 @@
|
|
|
1
1
|
import { error, redirect } from '@sveltejs/kit';
|
|
2
|
-
import { IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM, IN_CONTEXT_EDITOR_QUERY_STRING_PARAM, isAllowedReferrer, SECRET_QUERY_STRING_PARAM, } from '@uniformdev/canvas';
|
|
2
|
+
import { IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM, IN_CONTEXT_EDITOR_QUERY_STRING_PARAM, isAllowedReferrer, SECRET_QUERY_STRING_PARAM, toLocalRedirectPath, } from '@uniformdev/canvas';
|
|
3
3
|
import {} from '../models.js';
|
|
4
4
|
import { clearPreviewData, setPreviewData } from './previewCookie.js';
|
|
5
|
-
const BASE_URL_EXAMPLE = 'https://example.com';
|
|
6
5
|
const compositionQueryParams = {
|
|
7
6
|
id: 'id',
|
|
8
7
|
slug: 'slug',
|
|
@@ -16,12 +15,6 @@ const contextualEditingQueryParams = [
|
|
|
16
15
|
const resolveFullPathDefault = ({ slug, path }) => {
|
|
17
16
|
return path || slug;
|
|
18
17
|
};
|
|
19
|
-
function validateLocalRedirectUrl(pathToRedirectTo) {
|
|
20
|
-
// Prevent open redirection to any complete URL with a protocol
|
|
21
|
-
if (pathToRedirectTo?.match(/^[a-z]+:\/\//g)) {
|
|
22
|
-
throw new Error('Tried to redirect to absolute URL with protocol. Disallowing open redirect.');
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
18
|
/**
|
|
26
19
|
* Creates a SvelteKit GET request handler for preview mode.
|
|
27
20
|
*
|
|
@@ -80,14 +73,40 @@ export function createPreviewHandlerGet(options) {
|
|
|
80
73
|
if (typeof pathToRedirectTo === 'undefined') {
|
|
81
74
|
pathToRedirectTo = resolveFullPath({ id, slug, path, locale });
|
|
82
75
|
}
|
|
83
|
-
validateLocalRedirectUrl(pathToRedirectTo);
|
|
84
76
|
if (!pathToRedirectTo) {
|
|
85
77
|
error(400, 'Could not resolve the full path of the preview page');
|
|
86
78
|
}
|
|
79
|
+
// Check if this is valid contextual editing
|
|
80
|
+
const isUniformContextualEditing = searchParams.get(IN_CONTEXT_EDITOR_QUERY_STRING_PARAM) === 'true' &&
|
|
81
|
+
isAllowedReferrer(request.headers.get('referer') ?? undefined);
|
|
82
|
+
const localPathToRedirectTo = toLocalRedirectPath(pathToRedirectTo);
|
|
83
|
+
const fullPathToRedirectTo = toLocalRedirectPath(pathToRedirectTo, (redirectSearchParams) => {
|
|
84
|
+
const compositionQueryParamNames = Object.values(compositionQueryParams);
|
|
85
|
+
// Copy query params except secret and composition params
|
|
86
|
+
searchParams.forEach((value, name) => {
|
|
87
|
+
if (name === SECRET_QUERY_STRING_PARAM) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (compositionQueryParamNames.includes(name)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
redirectSearchParams.append(name, value);
|
|
94
|
+
});
|
|
95
|
+
// Remove contextual editing params if not in contextual editing mode
|
|
96
|
+
if (!isUniformContextualEditing) {
|
|
97
|
+
contextualEditingQueryParams.forEach((param) => {
|
|
98
|
+
redirectSearchParams.delete(param);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
// An open redirect is refused before any preview cookie is set or cleared.
|
|
103
|
+
if (!localPathToRedirectTo || !fullPathToRedirectTo) {
|
|
104
|
+
error(400, 'Invalid preview path');
|
|
105
|
+
}
|
|
87
106
|
// Handle disable preview
|
|
88
107
|
if (searchParams.has('disable')) {
|
|
89
108
|
clearPreviewData(cookies);
|
|
90
|
-
redirect(307,
|
|
109
|
+
redirect(307, localPathToRedirectTo);
|
|
91
110
|
}
|
|
92
111
|
const previewSecret = secret();
|
|
93
112
|
if (!previewSecret) {
|
|
@@ -98,9 +117,6 @@ export function createPreviewHandlerGet(options) {
|
|
|
98
117
|
if (searchParams.get(SECRET_QUERY_STRING_PARAM) !== previewSecret) {
|
|
99
118
|
error(401, 'Invalid token');
|
|
100
119
|
}
|
|
101
|
-
// Check if this is valid contextual editing
|
|
102
|
-
const isUniformContextualEditing = searchParams.get(IN_CONTEXT_EDITOR_QUERY_STRING_PARAM) === 'true' &&
|
|
103
|
-
isAllowedReferrer(request.headers.get('referer') ?? undefined);
|
|
104
120
|
// Set preview cookie
|
|
105
121
|
const previewData = {
|
|
106
122
|
isUniformContextualEditing,
|
|
@@ -118,26 +134,6 @@ export function createPreviewHandlerGet(options) {
|
|
|
118
134
|
// Short max age for contextual editing to avoid stale preview state
|
|
119
135
|
maxAge: isUniformContextualEditing ? 10 : undefined,
|
|
120
136
|
});
|
|
121
|
-
// Build redirect URL with query params
|
|
122
|
-
const redirectionUrl = new URL(pathToRedirectTo, BASE_URL_EXAMPLE);
|
|
123
|
-
const compositionQueryParamNames = Object.values(compositionQueryParams);
|
|
124
|
-
// Copy query params except secret and composition params
|
|
125
|
-
searchParams.forEach((value, name) => {
|
|
126
|
-
if (name === SECRET_QUERY_STRING_PARAM) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
if (compositionQueryParamNames.includes(name)) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
redirectionUrl.searchParams.append(name, value);
|
|
133
|
-
});
|
|
134
|
-
// Remove contextual editing params if not in contextual editing mode
|
|
135
|
-
if (!isUniformContextualEditing) {
|
|
136
|
-
contextualEditingQueryParams.forEach((param) => {
|
|
137
|
-
redirectionUrl.searchParams.delete(param);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
const fullPathToRedirectTo = redirectionUrl.href.replace(BASE_URL_EXAMPLE, '');
|
|
141
137
|
redirect(307, fullPathToRedirectTo);
|
|
142
138
|
};
|
|
143
139
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniformdev/canvas-sveltekit",
|
|
3
|
-
"version": "20.
|
|
3
|
+
"version": "20.89.0",
|
|
4
4
|
"description": "SvelteKit SDK for Uniform Canvas",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"build": "svelte-kit sync && svelte-package && publint",
|
|
9
9
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
10
10
|
"lint": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
11
|
-
"test": "vitest run",
|
|
11
|
+
"test": "svelte-kit sync && vitest run",
|
|
12
12
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
@@ -48,10 +48,10 @@
|
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@uniformdev/canvas": "20.
|
|
52
|
-
"@uniformdev/canvas-svelte": "20.
|
|
53
|
-
"@uniformdev/project-map": "20.
|
|
54
|
-
"@uniformdev/redirect": "20.
|
|
51
|
+
"@uniformdev/canvas": "20.89.0",
|
|
52
|
+
"@uniformdev/canvas-svelte": "20.89.0",
|
|
53
|
+
"@uniformdev/project-map": "20.89.0",
|
|
54
|
+
"@uniformdev/redirect": "20.89.0",
|
|
55
55
|
"colorette": "2.0.20"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@sveltejs/kit": "3.1.0",
|
|
61
61
|
"@sveltejs/package": "3.0.1",
|
|
62
62
|
"@sveltejs/vite-plugin-svelte": "7.3.1",
|
|
63
|
-
"@uniformdev/context-edge": "20.
|
|
63
|
+
"@uniformdev/context-edge": "20.89.0",
|
|
64
64
|
"publint": "0.3.25",
|
|
65
65
|
"svelte": "5.57.3",
|
|
66
66
|
"svelte-check": "4.7.7",
|
|
@@ -76,5 +76,5 @@
|
|
|
76
76
|
"publishConfig": {
|
|
77
77
|
"access": "public"
|
|
78
78
|
},
|
|
79
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "90277489e8c13e3cbe111ef0110bcb80d7958516"
|
|
80
80
|
}
|