Skip to content

Commit

Permalink
fix: added sanitization on SSR input URL
Browse files Browse the repository at this point in the history
  • Loading branch information
contentstackMridul committed Feb 6, 2024
1 parent e8b5f4a commit 28c9ff1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"url": "https://github.com/contentstack/live-preview-sdk.git"
},
"dependencies": {
"@braintree/sanitize-url": "^7.0.0",
"just-camel-case": "^4.0.2",
"morphdom": "^2.6.1",
"mustache": "^4.2.0",
Expand Down
20 changes: 17 additions & 3 deletions src/live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { handleInitData } from "./utils/handleUserConfig";
import { userInitData } from "./utils/defaults";
import packageJson from "../package.json";
import { replaceDocumentBody, updateDocumentBody } from "./utils/replaceHtml";
import { sanitizeUrl } from "@braintree/sanitize-url";

export default class LivePreview {
/**
Expand Down Expand Up @@ -339,9 +340,22 @@ export default class LivePreview {

if (this.config.ssr) {
// Get the content from the server and replace the body

const fetch_url = new URL(window.location.href);

const previewURL = sanitizeUrl(window.location.href);
if (previewURL === "about:blank") {
throw new Error(
"LIVE PREVIEW SDK: Invalid URL " +
window.location.href
);
}
const fetch_url = new URL(previewURL);
if (
fetch_url.protocol !== "https:" &&
fetch_url.protocol !== "http:"
) {
throw new Error(
"LIVE PREVIEW SDK: Preview URL should have http or https"
);
}
fetch_url.searchParams.append("live_preview", hash);
fetch_url.searchParams.append(
"content_type_uid",
Expand Down

0 comments on commit 28c9ff1

Please sign in to comment.