Skip to content

Commit 147f2ca

Browse files
authored
Escape CSS URLs that are coming from profiles (#5874)
1 parent 78d0ebb commit 147f2ca

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/components/shared/StyleDef.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// duplication.
1212

1313
import { PureComponent } from 'react';
14+
import { escapeCssUrl } from 'firefox-profiler/utils/url';
1415

1516
type StyleDefProps = {
1617
readonly content: string;
@@ -59,7 +60,7 @@ export class BackgroundImageStyleDef extends PureComponent<BackgroundImageStyleD
5960
override render(): React.ReactElement {
6061
const content = `
6162
.${this.props.className} {
62-
background-image: url(${this.props.url});
63+
background-image: url("${escapeCssUrl(this.props.url)}");
6364
}
6465
`;
6566
return <StyleDef content={content} />;

src/utils/url.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ export function isLocalURL(url: string | URL): boolean {
2020
return false;
2121
}
2222
}
23+
24+
/**
25+
* Escape a URL string so it can be safely embedded inside a double-quoted CSS
26+
* url("...").
27+
*/
28+
export function escapeCssUrl(url: string): string {
29+
return url
30+
.replace(/\\/g, '\\\\')
31+
.replace(/"/g, '\\"')
32+
.replace(/\n/g, '\\A ')
33+
.replace(/\r/g, '');
34+
}

0 commit comments

Comments
 (0)