-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathmiddleware-security.ts
More file actions
59 lines (46 loc) · 1.78 KB
/
middleware-security.ts
File metadata and controls
59 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import fs from 'fs'
import path from 'path'
import { describe, expect, test } from 'vitest'
const middlewareDir = path.join(__dirname, '..', 'middleware')
describe('shielding middleware security patterns', () => {
const middlewareFiles = fs.readdirSync(middlewareDir).filter((f) => f.endsWith('.ts'))
test("every .send() call uses .type('text')", () => {
const violations: string[] = []
for (const file of middlewareFiles) {
// index.ts is just the router, skip it
if (file === 'index.ts') continue
const content = fs.readFileSync(path.join(middlewareDir, file), 'utf-8')
const lines = content.split('\n')
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (line.includes('.send(') && !line.includes(".type('text')")) {
violations.push(`${file}:${i + 1}: ${line.trim()}`)
}
}
}
expect(
violations,
`All .send() calls in shielding middleware must use .type('text') to prevent XSS.\n` +
`Violations:\n${violations.join('\n')}`,
).toHaveLength(0)
})
test('no .send() call reflects user input via template literals', () => {
const violations: string[] = []
for (const file of middlewareFiles) {
if (file === 'index.ts') continue
const content = fs.readFileSync(path.join(middlewareDir, file), 'utf-8')
const lines = content.split('\n')
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (line.includes('.send(') && line.includes('${')) {
violations.push(`${file}:${i + 1}: ${line.trim()}`)
}
}
}
expect(
violations,
`Shielding middleware must not reflect user input in .send() responses.\n` +
`Violations:\n${violations.join('\n')}`,
).toHaveLength(0)
})
})