test_security.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import subprocess
  2. from parameterized import parameterized
  3. BASE_URL = 'http://localhost:8080'
  4. def curl_status(url):
  5. """Get HTTP status code using curl to avoid URL normalization by Python libs."""
  6. result = subprocess.run(
  7. ['curl', '-s', '-o', '/dev/null', '-w', '%{http_code}', url],
  8. capture_output=True, text=True, timeout=10
  9. )
  10. return int(result.stdout)
  11. class TestMalformedPaths:
  12. """Test that malformed paths don't crash the server.
  13. URLs like //foo are parsed as having 'foo' as the authority (host),
  14. resulting in an empty path. Empty paths previously crashed jester's
  15. static file handler. Now they return 400.
  16. URLs like //foo/bar are parsed as authority='foo', path='/bar',
  17. so they route normally (not empty path).
  18. """
  19. @parameterized.expand([
  20. # These parse to empty paths -> 400
  21. ('//lefty_rae', 400),
  22. ('//test', 400),
  23. ('//anyuser', 400),
  24. ])
  25. def test_empty_path_returns_400(self, path, expected_status):
  26. """URLs that parse to empty paths should return 400, not crash."""
  27. status = curl_status(f'{BASE_URL}{path}')
  28. assert status == expected_status, \
  29. f'Expected {expected_status} for {path}, got {status}'
  30. @parameterized.expand([
  31. ('/jack', 200),
  32. ('/about', 200),
  33. ('/', 200),
  34. ])
  35. def test_normal_paths_work(self, path, expected_status):
  36. """Normal paths should still work."""
  37. status = curl_status(f'{BASE_URL}{path}')
  38. assert status == expected_status, \
  39. f'Expected {expected_status} for {path}, got {status}'
  40. def test_server_survives_malformed_requests(self):
  41. """Server should handle malformed requests without crashing."""
  42. # These all parse to empty paths
  43. malformed_paths = ['//a', '//b', '//c', '//user', '//test']
  44. for path in malformed_paths:
  45. status = curl_status(f'{BASE_URL}{path}')
  46. assert status == 400, f'Expected 400 for {path}, got {status}'
  47. # Verify server is still responding after malformed requests
  48. status = curl_status(f'{BASE_URL}/')
  49. assert status == 200, 'Server should still be alive'