Skip to content

Commit 1292ad0

Browse files
authored
test: restrict iife helpers injection (#13918)
1 parent 28ddd43 commit 1292ad0

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

‎playground/lib/__tests__/lib.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ describe.runIf(isBuild)('build', () => {
3939
)
4040
})
4141

42+
test('restrisct-helpers-injection', async () => {
43+
const code = readFile(
44+
'dist/helpers-injection/my-lib-custom-filename.iife.js',
45+
)
46+
expect(code).toMatch(
47+
`'"use strict"; return (' + expressionSyntax + ").constructor;"`,
48+
)
49+
})
50+
4251
test('Library mode does not include `preload`', async () => {
4352
await untilUpdated(
4453
() => page.textContent('.dynamic-import-message'),

‎playground/lib/__tests__/serve.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ export async function serve(): Promise<{ close(): Promise<void> }> {
6767
configFile: path.resolve(__dirname, '../vite.nominify.config.js'),
6868
})
6969

70+
await build({
71+
root: rootDir,
72+
logLevel: 'warn', // output esbuild warns
73+
configFile: path.resolve(
74+
__dirname,
75+
'../vite.helpers-injection.config.js',
76+
),
77+
})
78+
7079
// start static file server
7180
const serve = sirv(path.resolve(rootDir, 'dist'))
7281
const httpServer = http.createServer((req, res) => {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Trigger wrong helpers injection if not properly constrained
2+
;(function () {
3+
var getEvalledConstructor = function (expressionSyntax) {
4+
try {
5+
return $Function(
6+
'"use strict"; return (' + expressionSyntax + ').constructor;',
7+
)()
8+
} catch (e) {}
9+
}
10+
console.log(getEvalledConstructor(0))
11+
})()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import path from 'node:path'
2+
import { defineConfig } from 'vite'
3+
4+
// Check that helpers injection is properly constrained
5+
6+
export default defineConfig({
7+
build: {
8+
lib: {
9+
entry: path.resolve(__dirname, 'src/main-helpers-injection.js'),
10+
name: 'MyLib',
11+
formats: ['iife'],
12+
fileName: 'my-lib-custom-filename',
13+
},
14+
minify: false,
15+
outDir: 'dist/helpers-injection',
16+
},
17+
plugins: [],
18+
cacheDir: 'node_modules/.vite-helpers-injection',
19+
})

0 commit comments

Comments
 (0)