← 메인 화면프로젝트 자료·코드
← 자료 목록

cc/tools/materials_smoke.py

프로그램·코드 · 2.3 KB원본 다운로드

원본 내용을 읽기 전용으로 표시합니다.

  1. """자료실의 실제 HTTPS 조회·다운로드·격리 설정을 확인한다."""
  2. import hashlib
  3. import json
  4. import sys
  5. import urllib.error
  6. import urllib.request
  7. from pathlib import Path
  8. def verify(base):
  9. entries = json.loads((Path(__file__).resolve().parents[1] / 'public_materials' / 'manifest.json').read_text(encoding='utf-8'))['files']
  10. checks = []
  11. def get(path, expected=200):
  12. try:
  13. response = urllib.request.urlopen(base.rstrip('/') + path, timeout=30)
  14. except urllib.error.HTTPError as error:
  15. response = error
  16. with response:
  17. body = response.read()
  18. assert response.code == expected, (path, response.code)
  19. checks.append({'path': path, 'status': response.code})
  20. return body, response.headers
  21. assert b'/materials/' in get('/')[0]
  22. assert '프로젝트 자료실'.encode() in get('/materials/')[0]
  23. for kind in ('code', 'text', 'csv', 'image', 'map'):
  24. entry = next(e for e in entries if e['kind'] == kind)
  25. assert entry['path'].encode() in get('/materials/file/' + entry['id'])[0]
  26. body, headers = get('/materials/download/' + entry['id'])
  27. assert 'attachment' in headers['Content-Disposition']
  28. assert hashlib.sha256(body).hexdigest() == entry['sha256']
  29. if kind == 'map':
  30. body, headers = get('/materials/preview/' + entry['id'])
  31. assert hashlib.sha256(body).hexdigest() == entry['sha256']
  32. assert 'sandbox allow-scripts;' in headers['Content-Security-Policy']
  33. assert 'allow-same-origin' not in headers['Content-Security-Policy']
  34. assert b'<html' in get('/materials/file/' + entry['id'] + '?view=source')[0]
  35. if kind == 'image':
  36. assert get('/materials/image/' + entry['id'])[1]['Content-Type'].startswith('image/png')
  37. get('/materials/download/.env', 404)
  38. get('/materials/file/' + 'f' * 20, 404)
  39. get('/materials/?folder=..', 404)
  40. return {'base': base, 'files': len(entries), 'checks': checks}
  41. if __name__ == '__main__':
  42. result = verify(sys.argv[1] if len(sys.argv) > 1 else 'http://127.0.0.1:5000')
  43. if len(sys.argv) > 2:
  44. Path(sys.argv[2]).write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
  45. print(json.dumps(result, ensure_ascii=False, indent=2))
파일 확인 정보

SHA-256: 02030b79509754c6d05d91c62f83420f3ae04e934c929a178821323021fc2ac4