Skip to content

Commit a6f9e1a

Browse files
committed
test: enhance XSS pattern detection with improved regex and edge case tests
1 parent b10327f commit a6f9e1a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/security/input-validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class InputValidator {
243243
*/
244244
private containsXSSPattern(value: string): boolean {
245245
const xssPatterns = [
246-
/<script[^>]*>.*?<\/script>/i,
246+
/<script\b[^>]*>.*?<\/script\b[^>]*>/gis,
247247
/<iframe[^>]*>/i,
248248
/javascript:/i,
249249
/on\w+\s*=/i, // Event handlers like onclick=

test/security/input-validator.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,28 @@ describe('InputValidator', () => {
223223
}
224224
})
225225

226+
test('should detect XSS patterns with malformed closing tags (CodeQL fix)', () => {
227+
const validator = new InputValidator()
228+
// Test cases for the improved regex that handles edge cases
229+
const edgeCaseXSSPatterns = [
230+
'<script>alert(1)</script >', // Whitespace before >
231+
'<script>alert(1)</script >', // Multiple spaces
232+
'<SCRIPT>alert(1)</SCRIPT>', // Uppercase
233+
'<ScRiPt>alert(1)</ScRiPt>', // Mixed case
234+
'<script>alert(1)\n</script>', // Multiline
235+
'<script type="text/javascript">alert(1)</script>', // Attributes
236+
'<script async>alert(1)</script defer>', // Attributes in closing tag
237+
]
238+
239+
for (const pattern of edgeCaseXSSPatterns) {
240+
const params = new URLSearchParams()
241+
params.set('input', pattern)
242+
const result = validator.validateQueryParams(params)
243+
expect(result.valid).toBe(false)
244+
expect(result.errors?.some((err) => err.includes('XSS'))).toBe(true)
245+
}
246+
})
247+
226248
test('should detect command injection patterns', () => {
227249
const validator = new InputValidator()
228250
const commandInjections = [

0 commit comments

Comments
 (0)