Skip to content

Commit 9186bc7

Browse files
authored
fix: vite v6 escape chars issue (#172)
1 parent 41e8b9f commit 9186bc7

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

packages/vite-plugin-csp-guard/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export default function vitePluginCSP(
153153
isTransformationStatusEmpty: isTransformationStatusEmpty(),
154154
sri,
155155
shouldSkip,
156+
isVite6: true, //This is set to true constantly because we cannot determine the vite version at runtime accurately.
156157
});
157158
},
158159
},

packages/vite-plugin-csp-guard/src/transform/handleIndexHtml.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function handleIndexHtml({
8080
}
8181
});
8282

83+
8384
// TODO: Maybe we don't need this if we are just using 'self' anyway in the policy?
8485
// $("link").each(function (i, el) {
8586
// if (
@@ -139,3 +140,11 @@ export function handleIndexHtml({
139140
// }
140141
return { HASH_COLLECTION, html: $.html() };
141142
}
143+
144+
// Used for Vite 6 workaround
145+
export const handleCSPInsert = (html: string, policy: string) => {
146+
const $ = cheerio.load(html);
147+
const metaTag = `<meta http-equiv="Content-Security-Policy" content="${policy}">`;
148+
$("head").prepend(metaTag);
149+
return $.html();
150+
};

packages/vite-plugin-csp-guard/src/transform/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ShouldSkip,
88
TransformationStatus,
99
} from "../types";
10-
import { handleIndexHtml } from "./handleIndexHtml";
10+
import { handleCSPInsert, handleIndexHtml } from "./handleIndexHtml";
1111
import { PluginContext } from "rollup";
1212
import { generatePolicyString, policyToTag } from "../policy/createPolicy";
1313
import { cssFilter, jsFilter, preCssFilter, tsFilter } from "../utils";
@@ -114,6 +114,7 @@ export interface TransformIndexHtmlHandlerProps {
114114
isTransformationStatusEmpty: boolean;
115115
sri: boolean;
116116
shouldSkip: ShouldSkip;
117+
isVite6?: boolean;
117118
}
118119

119120
export const transformIndexHtmlHandler = async ({
@@ -126,6 +127,7 @@ export const transformIndexHtmlHandler = async ({
126127
isTransformationStatusEmpty,
127128
sri,
128129
shouldSkip,
130+
isVite6 = false,
129131
}: TransformIndexHtmlHandlerProps) => {
130132
if (isTransformationStatusEmpty && server) {
131133
//Return early if there are no transformations and we are in dev mode
@@ -186,9 +188,18 @@ export const transformIndexHtmlHandler = async ({
186188
});
187189

188190
const InjectedHtmlTags = policyToTag(policyString);
191+
192+
if(isVite6){
193+
const changedHtml = handleCSPInsert(newHtml, policyString)
194+
return {
195+
html: changedHtml,
196+
tags: []
197+
}
198+
}
189199

190200
return {
191201
html: newHtml,
192202
tags: InjectedHtmlTags,
193203
};
204+
194205
};

0 commit comments

Comments
 (0)