Skip to content

Commit 6a72083

Browse files
authored
Add CSP nonce to Vite reactRefresh inline script (#44816)
1 parent 0702f55 commit 6a72083

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Illuminate/Foundation/Vite.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,17 +575,22 @@ public function reactRefresh()
575575
return;
576576
}
577577

578+
$attributes = $this->parseAttributes([
579+
'nonce' => $this->cspNonce()
580+
]);
581+
578582
return new HtmlString(
579583
sprintf(
580584
<<<'HTML'
581-
<script type="module">
585+
<script type="module" %s>
582586
import RefreshRuntime from '%s'
583587
RefreshRuntime.injectIntoGlobalHook(window)
584588
window.$RefreshReg$ = () => {}
585589
window.$RefreshSig$ = () => (type) => type
586590
window.__vite_plugin_react_preamble_installed__ = true
587591
</script>
588592
HTML,
593+
implode(' ', $attributes),
589594
$this->hotAsset('@react-refresh')
590595
)
591596
);

tests/Foundation/FoundationViteTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ public function testItCanSpecifyCspNonceWithManifest()
170170
);
171171
}
172172

173+
public function testReactRefreshWithNoNonce()
174+
{
175+
$this->makeViteHotFile();
176+
177+
$result = app(Vite::class)->reactRefresh();
178+
179+
$this->assertStringNotContainsString('nonce', $result);
180+
}
181+
182+
public function testReactRefreshNonce()
183+
{
184+
$this->makeViteHotFile();
185+
186+
$nonce = ViteFacade::useCspNonce('expected-nonce');
187+
$result = app(Vite::class)->reactRefresh();
188+
189+
$this->assertStringContainsString(sprintf('nonce="%s"', $nonce), $result);
190+
}
191+
173192
public function testItCanInjectIntegrityWhenPresentInManifest()
174193
{
175194
$buildDir = Str::random();

0 commit comments

Comments
 (0)