Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Incorrectly quoted URLs in CSS (#2642)

Ivan Starkova9391213181d
diff --git a/fixtures/webstudio-custom-template/public/custom-folder/home_wsKvRSqvkajPPBeycZ-C8.svg b/fixtures/webstudio-custom-template/public/custom-folder/home_wsKvRSqvkajPPBeycZ-C8.svg
new file mode 100644
index 0000000000..67284862e7
--- /dev/null
+++ b/fixtures/webstudio-custom-template/public/custom-folder/home_wsKvRSqvkajPPBeycZ-C8.svg
@@ -0,0 +1,3 @@
+<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path fill-rule="evenodd" clip-rule="evenodd" d="M7.6559 0.907089C7.88124 0.846075 8.11876 0.846075 8.34411 0.907089C8.60553 0.97787 8.82581 1.15075 9.00433 1.29086C9.02124 1.30414 9.03777 1.31712 9.05393 1.32968L13.5755 4.84644C13.5927 4.85983 13.6098 4.87308 13.6267 4.88623C13.8784 5.08159 14.0978 5.25187 14.2612 5.47281C14.4047 5.66677 14.5116 5.88528 14.5766 6.11759C14.6507 6.38223 14.6504 6.65995 14.65 6.9786C14.65 7.00004 14.65 7.02165 14.65 7.04347V11.8919C14.65 12.2438 14.65 12.5464 14.6297 12.7954C14.6082 13.0579 14.561 13.3177 14.4338 13.5672C14.2437 13.9404 13.9403 14.2438 13.5671 14.4339C13.3176 14.561 13.0579 14.6083 12.7953 14.6298C12.5463 14.6501 12.2437 14.6501 11.8918 14.6501H4.10816C3.75628 14.6501 3.4537 14.6501 3.20467 14.6298C2.94214 14.6083 2.68243 14.561 2.43292 14.4339C2.05973 14.2438 1.75632 13.9404 1.56617 13.5672C1.43904 13.3177 1.39178 13.0579 1.37033 12.7954C1.34998 12.5464 1.34999 12.2438 1.35 11.8919L1.35 7.04347C1.35 7.02165 1.34998 7.00003 1.34996 6.9786C1.34963 6.65995 1.34934 6.38222 1.42341 6.11759C1.48844 5.88527 1.59531 5.66677 1.73876 5.47281C1.90218 5.25187 2.12158 5.08159 2.3733 4.88623C2.39024 4.87308 2.40732 4.85983 2.42453 4.84644L6.94607 1.32968C6.96223 1.31711 6.97877 1.30414 6.99568 1.29086C7.1742 1.15075 7.39448 0.97787 7.6559 0.907089ZM6.65 13.3501H9.35V9.06675C9.35 8.86934 9.3495 8.76038 9.34308 8.68182C9.34236 8.67303 9.34162 8.66552 9.34091 8.65918C9.33457 8.65847 9.32706 8.65773 9.31827 8.65701C9.23971 8.65059 9.13075 8.65009 8.93334 8.65009H7.06667C6.86926 8.65009 6.76029 8.65059 6.68174 8.65701C6.67294 8.65773 6.66544 8.65847 6.6591 8.65918C6.65839 8.66552 6.65765 8.67303 6.65693 8.68182C6.65051 8.76038 6.65 8.86934 6.65 9.06675V13.3501ZM10.65 13.3501L10.65 9.04584C10.65 8.87727 10.65 8.71403 10.6388 8.57596C10.6264 8.42459 10.5973 8.24721 10.5065 8.069C10.3803 7.82125 10.1788 7.61983 9.93109 7.49359C9.75288 7.40279 9.5755 7.37369 9.42413 7.36133C9.28606 7.35005 9.12282 7.35007 8.95425 7.35008H7.04575C6.87718 7.35007 6.71395 7.35005 6.57587 7.36133C6.42451 7.37369 6.24712 7.40279 6.06892 7.49359C5.82117 7.61983 5.61974 7.82125 5.49351 8.069C5.40271 8.24721 5.37361 8.42459 5.36124 8.57596C5.34996 8.71403 5.34998 8.87727 5.35 9.04584L5.35 13.3501H4.13334C3.74924 13.3501 3.50026 13.3496 3.31054 13.3341C3.1284 13.3192 3.05881 13.2938 3.02311 13.2756C2.89453 13.2101 2.79 13.1056 2.72448 12.977C2.70629 12.9413 2.68089 12.8717 2.66601 12.6896C2.65051 12.4998 2.65 12.2508 2.65 11.8668V7.04347C2.65 6.62375 2.65565 6.53819 2.6753 6.46801C2.6977 6.38796 2.73452 6.31268 2.78395 6.24586C2.82729 6.18726 2.89135 6.13027 3.22266 5.87259L7.74419 2.35584C7.86861 2.25907 7.93516 2.20781 7.98588 2.17392C7.99132 2.17029 7.99601 2.16725 8 2.16473C8.00399 2.16725 8.00869 2.17029 8.01413 2.17392C8.06485 2.20781 8.13139 2.25907 8.25581 2.35584L12.7773 5.87259C13.1087 6.13027 13.1727 6.18726 13.2161 6.24586C13.2655 6.31268 13.3023 6.38796 13.3247 6.46801C13.3444 6.53819 13.35 6.62375 13.35 7.04347V11.8668C13.35 12.2508 13.3495 12.4998 13.334 12.6896C13.3191 12.8717 13.2937 12.9413 13.2755 12.977C13.21 13.1056 13.1055 13.2101 12.9769 13.2756C12.9412 13.2938 12.8716 13.3192 12.6895 13.3341C12.4997 13.3496 12.2508 13.3501 11.8667 13.3501H10.65Z" fill="#11181C"/>
+</svg>
diff --git a/fixtures/webstudio-custom-template/public/favicon.ico b/fixtures/webstudio-custom-template/public/favicon.ico
new file mode 100644
index 0000000000..9cb8c815dc
Binary files /dev/null and b/fixtures/webstudio-custom-template/public/favicon.ico differ
diff --git a/packages/css-engine/src/core/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.ts
index 5db1f50beb..12c9658e6c 100644
--- a/packages/css-engine/src/core/css-engine.test.ts
+++ b/packages/css-engine/src/core/css-engine.test.ts
@@ -480,7 +480,7 @@ describe("CssEngine", () => {
expect(engine.cssText).toMatchInlineSnapshot(`
"@media all {
.c {
- background-image: url(foo.png)
+ background-image: url("foo.png")
}
}"
`);
diff --git a/packages/css-engine/src/core/to-value.test.ts b/packages/css-engine/src/core/to-value.test.ts
index 073500a972..5dab921abb 100644
--- a/packages/css-engine/src/core/to-value.test.ts
+++ b/packages/css-engine/src/core/to-value.test.ts
@@ -116,7 +116,7 @@ describe("Convert WS CSS Values to native CSS strings", () => {
}
);
- expect(value).toBe("auto, 10px, calc(10px), url(foo.png)");
+ expect(value).toBe(`auto, 10px, calc(10px), url("foo.png")`);
});
test("tuple", () => {
@@ -131,4 +131,40 @@ describe("Convert WS CSS Values to native CSS strings", () => {
});
expect(value).toBe("10px 20px 30px 40px");
});
+
+ test("sanitize url", () => {
+ const assets = new Map<string, { path: string }>([
+ ["1234567890", { path: `fo"o\\o.png` }],
+ ]);
+
+ const value = toValue(
+ {
+ type: "image",
+ value: {
+ type: "asset",
+ value: "1234567890",
+ },
+ },
+ (styleValue) => {
+ if (styleValue.type === "image" && styleValue.value.type === "asset") {
+ const asset = assets.get(styleValue.value.value);
+ if (asset === undefined) {
+ return {
+ type: "keyword",
+ value: "none",
+ };
+ }
+ return {
+ type: "image",
+ value: {
+ type: "url",
+ url: asset.path,
+ },
+ };
+ }
+ }
+ );
+
+ expect(value).toMatchInlineSnapshot(`"url("fo\\"o\\\\o.png")"`);
+ });
});
diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.ts
index 222aae980c..1f4ae40651 100644
--- a/packages/css-engine/src/core/to-value.ts
+++ b/packages/css-engine/src/core/to-value.ts
@@ -22,6 +22,9 @@ const fallbackTransform: TransformValue = (styleValue) => {
}
};
+// Use JSON.stringify to escape double quotes and backslashes in strings as it automatically replaces " with \" and \ with \\.
+const sanitizeCssUrl = (str: string) => JSON.stringify(str);
+
export const toValue = (
styleValue: undefined | StyleValue,
transformValue?: TransformValue
@@ -73,7 +76,7 @@ export const toValue = (
}
// @todo image-set
- return `url(${value.value.url})`;
+ return `url(${sanitizeCssUrl(value.value.url)})`;
}
if (value.type === "unparsed") {
diff --git a/packages/fonts/src/__snapshots__/get-font-faces.test.ts.snap b/packages/fonts/src/__snapshots__/get-font-faces.test.ts.snap
deleted file mode 100644
index f814ece782..0000000000
--- a/packages/fonts/src/__snapshots__/get-font-faces.test.ts.snap
+++ /dev/null
@@ -1,64 +0,0 @@
-// Jest Snapshot v1, https://goo.gl/fbAQLP
-
-exports[`getFontFaces() different formats 1`] = `
-[
- {
- "fontDisplay": "swap",
- "fontFamily": "Roboto",
- "fontStyle": "normal",
- "fontWeight": 400,
- "src": "url('/fonts/roboto.woff') format('woff'), url('/fonts/roboto.ttf') format('truetype')",
- },
-]
-`;
-
-exports[`getFontFaces() different style 1`] = `
-[
- {
- "fontDisplay": "swap",
- "fontFamily": "Roboto",
- "fontStyle": "normal",
- "fontWeight": 400,
- "src": "url('/fonts/roboto.ttf') format('truetype')",
- },
- {
- "fontDisplay": "swap",
- "fontFamily": "Roboto",
- "fontStyle": "italic",
- "fontWeight": 400,
- "src": "url('/fonts/roboto-italic.ttf') format('truetype')",
- },
-]
-`;
-
-exports[`getFontFaces() different weight 1`] = `
-[
- {
- "fontDisplay": "swap",
- "fontFamily": "Roboto",
- "fontStyle": "normal",
- "fontWeight": 400,
- "src": "url('/fonts/roboto.ttf') format('truetype')",
- },
- {
- "fontDisplay": "swap",
- "fontFamily": "Roboto",
- "fontStyle": "normal",
- "fontWeight": 500,
- "src": "url('/fonts/roboto-bold.ttf') format('truetype')",
- },
-]
-`;
-
-exports[`getFontFaces() variable font 1`] = `
-[
- {
- "fontDisplay": "swap",
- "fontFamily": "Inter",
- "fontStretch": "25% 151%",
- "fontStyle": "normal",
- "fontWeight": "100 1000",
- "src": "url('/fonts/inter.ttf') format('truetype')",
- },
-]
-`;
diff --git a/packages/fonts/src/get-font-faces.test.ts b/packages/fonts/src/get-font-faces.test.ts
index 16d811e7b5..21a58b23fd 100644
--- a/packages/fonts/src/get-font-faces.test.ts
+++ b/packages/fonts/src/get-font-faces.test.ts
@@ -2,6 +2,32 @@ import { describe, test, expect } from "@jest/globals";
import { getFontFaces, type PartialFontAsset } from "./get-font-faces";
describe("getFontFaces()", () => {
+ test("sanitize url", () => {
+ const assets: Array<PartialFontAsset> = [
+ {
+ format: "woff",
+ meta: {
+ family: "Roboto",
+ style: "normal",
+ weight: 400,
+ },
+ name: `robot"o.woff`,
+ },
+ ];
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" }))
+ .toMatchInlineSnapshot(`
+[
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 400,
+ "src": "url("/fonts/robot\\"o.woff") format("woff")",
+ },
+]
+`);
+ });
+
test("different formats", () => {
const assets: Array<PartialFontAsset> = [
{
@@ -23,7 +49,18 @@ describe("getFontFaces()", () => {
name: "roboto.ttf",
},
];
- expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" }))
+ .toMatchInlineSnapshot(`
+[
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 400,
+ "src": "url("/fonts/roboto.woff") format("woff"), url("/fonts/roboto.ttf") format("truetype")",
+ },
+]
+`);
});
test("different style", () => {
@@ -47,7 +84,25 @@ describe("getFontFaces()", () => {
name: "roboto-italic.ttf",
},
];
- expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" }))
+ .toMatchInlineSnapshot(`
+[
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 400,
+ "src": "url("/fonts/roboto.ttf") format("truetype")",
+ },
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "italic",
+ "fontWeight": 400,
+ "src": "url("/fonts/roboto-italic.ttf") format("truetype")",
+ },
+]
+`);
});
test("different weight", () => {
@@ -71,7 +126,25 @@ describe("getFontFaces()", () => {
name: "roboto-bold.ttf",
},
];
- expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" }))
+ .toMatchInlineSnapshot(`
+[
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 400,
+ "src": "url("/fonts/roboto.ttf") format("truetype")",
+ },
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Roboto",
+ "fontStyle": "normal",
+ "fontWeight": 500,
+ "src": "url("/fonts/roboto-bold.ttf") format("truetype")",
+ },
+]
+`);
});
test("variable font", () => {
@@ -99,6 +172,18 @@ describe("getFontFaces()", () => {
name: "inter.ttf",
},
];
- expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
+ expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" }))
+ .toMatchInlineSnapshot(`
+[
+ {
+ "fontDisplay": "swap",
+ "fontFamily": "Inter",
+ "fontStretch": "25% 151%",
+ "fontStyle": "normal",
+ "fontWeight": "100 1000",
+ "src": "url("/fonts/inter.ttf") format("truetype")",
+ },
+]
+`);
});
});
diff --git a/packages/fonts/src/get-font-faces.ts b/packages/fonts/src/get-font-faces.ts
index 9fe254a2e5..838e51bdba 100644
--- a/packages/fonts/src/get-font-faces.ts
+++ b/packages/fonts/src/get-font-faces.ts
@@ -16,6 +16,9 @@ export type FontFace = {
fontStretch?: string;
};
+// Use JSON.stringify to escape double quotes and backslashes in strings as it automatically replaces " with \" and \ with \\.
+const sanitizeCssUrl = (str: string) => JSON.stringify(str);
+
const formatFace = (
asset: PartialFontAsset,
format: string,
@@ -27,7 +30,7 @@ const formatFace = (
fontFamily: asset.meta.family,
fontStyle: "normal",
fontDisplay: "swap",
- src: `url('${url}') format('${format}')`,
+ src: `url(${sanitizeCssUrl(url)}) format("${format}")`,
fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : undefined,
fontWeight: wght ? `${wght.min} ${wght.max}` : undefined,
};
@@ -37,7 +40,7 @@ const formatFace = (
fontStyle: asset.meta.style,
fontWeight: asset.meta.weight,
fontDisplay: "swap",
- src: `url('${url}') format('${format}')`,
+ src: `url(${sanitizeCssUrl(url)}) format("${format}")`,
};
};
@@ -73,7 +76,7 @@ export const getFontFaces = (
}
// We already have that font face, so we need to add the new src
- face.src += `, url('${url}') format('${format}')`;
+ face.src += `, url(${sanitizeCssUrl(url)}) format("${format}")`;
}
return Array.from(faces.values());
};