Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

xss-prevention.test.tsx 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * XSS Prevention Test Suite
  3. *
  4. * This test verifies that the XSS vulnerabilities in block-input and support-var-input
  5. * components have been properly fixed by replacing dangerouslySetInnerHTML with safe React rendering.
  6. */
  7. import React from 'react'
  8. import { cleanup, render } from '@testing-library/react'
  9. import '@testing-library/jest-dom'
  10. import BlockInput from '../app/components/base/block-input'
  11. import SupportVarInput from '../app/components/workflow/nodes/_base/components/support-var-input'
  12. // Mock styles
  13. jest.mock('../app/components/app/configuration/base/var-highlight/style.module.css', () => ({
  14. item: 'mock-item-class',
  15. }))
  16. describe('XSS Prevention - Block Input and Support Var Input Security', () => {
  17. afterEach(() => {
  18. cleanup()
  19. })
  20. describe('BlockInput Component Security', () => {
  21. it('should safely render malicious variable names without executing scripts', () => {
  22. const testInput = 'user@test.com{{<script>alert("XSS")</script>}}'
  23. const { container } = render(<BlockInput value={testInput} readonly={true} />)
  24. const scriptElements = container.querySelectorAll('script')
  25. expect(scriptElements).toHaveLength(0)
  26. const textContent = container.textContent
  27. expect(textContent).toContain('<script>')
  28. })
  29. it('should preserve legitimate variable highlighting', () => {
  30. const legitimateInput = 'Hello {{userName}} welcome to {{appName}}'
  31. const { container } = render(<BlockInput value={legitimateInput} readonly={true} />)
  32. const textContent = container.textContent
  33. expect(textContent).toContain('userName')
  34. expect(textContent).toContain('appName')
  35. })
  36. })
  37. describe('SupportVarInput Component Security', () => {
  38. it('should safely render malicious variable names without executing scripts', () => {
  39. const testInput = 'test@evil.com{{<img src=x onerror=alert(1)>}}'
  40. const { container } = render(<SupportVarInput value={testInput} readonly={true} />)
  41. const scriptElements = container.querySelectorAll('script')
  42. const imgElements = container.querySelectorAll('img')
  43. expect(scriptElements).toHaveLength(0)
  44. expect(imgElements).toHaveLength(0)
  45. const textContent = container.textContent
  46. expect(textContent).toContain('<img')
  47. })
  48. })
  49. describe('React Automatic Escaping Verification', () => {
  50. it('should confirm React automatic escaping works correctly', () => {
  51. const TestComponent = () => <span>{'<script>alert("xss")</script>'}</span>
  52. const { container } = render(<TestComponent />)
  53. const spanElement = container.querySelector('span')
  54. const scriptElements = container.querySelectorAll('script')
  55. expect(spanElement?.textContent).toBe('<script>alert("xss")</script>')
  56. expect(scriptElements).toHaveLength(0)
  57. })
  58. })
  59. })
  60. export {}