添加error 测试用例,但是测试用例太复杂,还需要优化框架
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# This file marks the security directory as a Python package.
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,84 @@
|
||||
from typing import Dict, Any, Optional, List
|
||||
from ddms_compliance_suite.test_framework_core import BaseAPITestCase, TestSeverity, ValidationResult, APIRequestContext, APIResponseContext
|
||||
import urllib.parse
|
||||
|
||||
class HTTPSMandatoryCase(BaseAPITestCase):
|
||||
id = "TC-SECURITY-001"
|
||||
name = "HTTPS Protocol Mandatory Verification"
|
||||
description = "验证API端点是否通过HTTPS提供服务,以及HTTP请求是否被拒绝或重定向到HTTPS。"
|
||||
severity = TestSeverity.CRITICAL
|
||||
tags = ["security", "https", "transport-security"]
|
||||
execution_order = 120
|
||||
|
||||
# 此测试会修改URL为HTTP,应适用于大多数端点。
|
||||
|
||||
def __init__(self, endpoint_spec: Dict[str, Any], global_api_spec: Dict[str, Any], json_schema_validator: Optional[Any] = None):
|
||||
super().__init__(endpoint_spec, global_api_spec, json_schema_validator)
|
||||
self.logger.info(f"测试用例 '{self.id}' 已为端点 '{self.endpoint_spec.get('method')} {self.endpoint_spec.get('path')}' 初始化。")
|
||||
|
||||
def modify_request_url(self, current_url: str) -> str:
|
||||
parsed_url = urllib.parse.urlparse(current_url)
|
||||
if parsed_url.scheme.lower() == "httpss":
|
||||
# 将 https 替换为 http
|
||||
modified_url = parsed_url._replace(scheme="http").geturl()
|
||||
self.logger.info(f"为进行HTTPS检查修改URL:原始 '{current_url}', 修改为 '{modified_url}'")
|
||||
return modified_url
|
||||
else:
|
||||
self.logger.warning(f"原始URL '{current_url}' 不是HTTPS。跳过此测试用例的URL修改。")
|
||||
# 如果原始URL不是HTTPS,此测试可能无效,
|
||||
# 或者暗示基础URL本身可能未正确配置以进行HTTPS测试。
|
||||
return current_url # 如果不是HTTPS则返回原始URL
|
||||
|
||||
def validate_response(self, response_context: APIResponseContext, request_context: APIRequestContext) -> List[ValidationResult]:
|
||||
results = []
|
||||
status_code = response_context.status_code
|
||||
# request_context.url 是调用 modify_request_url 之后,APICaller实际发送的URL
|
||||
request_url_scheme = urllib.parse.urlparse(request_context.url).scheme
|
||||
|
||||
# 检查URL是否确实被此测试的钩子修改为了HTTP
|
||||
if request_url_scheme.lower() != "http":
|
||||
results.append(self.passed(
|
||||
message=f"测试已跳过,因为发送的URL已经是 {request_url_scheme.upper()}(可能是由于原始基础URL非HTTPS或测试设置问题)。"
|
||||
))
|
||||
self.logger.info("HTTPS强制性检查已跳过,因为有效URL不是HTTP。")
|
||||
return results
|
||||
|
||||
# 如果请求是通过HTTP发出的,我们期望几种结果:
|
||||
# 1. 拒绝(例如400、403,或连接被拒绝 - 尽管APICaller可能在此之前处理连接拒绝)
|
||||
# 2. 重定向到HTTPS(例如301、302、307、308,并带有指向HTTPS的Location头)
|
||||
|
||||
if status_code in [301, 302, 307, 308]: # 重定向状态码
|
||||
location_header = response_context.headers.get("Location")
|
||||
if location_header and urllib.parse.urlparse(location_header).scheme.lower() == "httpss":
|
||||
results.append(self.passed(
|
||||
message=f"对 {request_context.url} 的HTTP请求被正确重定向到HTTPS ({location_header}),状态码 {status_code}。"
|
||||
))
|
||||
self.logger.info(f"HTTP被正确重定向到HTTPS: {location_header}")
|
||||
else:
|
||||
results.append(self.failed(
|
||||
message=f"对 {request_context.url} 的HTTP请求被重定向(状态码 {status_code}),但Location头 '{location_header}' 未指向HTTPS URL。",
|
||||
details={"status_code": status_code, "location_header": location_header}
|
||||
))
|
||||
self.logger.warning(f"HTTP被重定向,状态码 {status_code},但Location '{location_header}' 不是HTTPS。")
|
||||
elif status_code // 100 == 4: # 客户端错误(例如400错误请求,403禁止访问)
|
||||
results.append(self.passed(
|
||||
message=f"对 {request_context.url} 的HTTP请求被客户端错误(状态码 {status_code})拒绝,表明不允许HTTP访问。"
|
||||
))
|
||||
self.logger.info(f"HTTP请求被客户端错误 {status_code} 拒绝。")
|
||||
elif status_code // 100 == 2: # 通过HTTP成功返回2xx响应
|
||||
results.append(self.failed(
|
||||
message=f"API通过HTTP ({request_context.url}) 响应了成功的状态码 {status_code},这违反了HTTPS强制策略。",
|
||||
details={"status_code": status_code}
|
||||
))
|
||||
self.logger.error(f"安全漏洞:API允许通过HTTP成功响应 ({status_code})。")
|
||||
else:
|
||||
# 其他状态码(例如5xx)可能表示与HTTPS强制执行无关的服务器错误,
|
||||
# 或者连接在更底层被拒绝(APICaller可能会抛出异常)。
|
||||
results.append(ValidationResult(
|
||||
passed=False, # 或True,取决于严格程度 - 5xx不是通过,但不一定是HTTPS失败
|
||||
message=f"对 {request_context.url} 的HTTP请求返回了意外的状态码 {status_code}。需要手动调查。",
|
||||
details={"status_code": status_code, "response_text_sample": (response_context.text_content or "")[:200]}
|
||||
))
|
||||
self.logger.warning(f"HTTP请求返回意外状态码 {status_code}。潜在问题或服务器错误。")
|
||||
|
||||
return results
|
||||
Reference in New Issue
Block a user