T09 · Insecure Skill Coding Practices
- Location
scripts/main.py:51- Finding
Configurable OCR Endpoint Can Expose API Credentials and Sensitive Invoice Data
- Content
View full analysis
Vulnerability Details
File Location:
scripts/main.py:51-112
Vulnerability Type: Unvalidated network destination for sensitive data
Risk Level: HighVulnerable Code
python config = {} with open(ENV_FILE, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if not line or line.startswith('#'): continue if '=' in line: key, value = line.split('=', 1) key = key.strip() value = value.strip().strip('"').strip("'") config[key] = value # 检查必要配置 api_key = config.get('SCNET_API_KEY', '') if not api_key or api_key == 'your_scnet_api_key_here': error_msg = ( "\n===============================================\n" "Scnet API Key 未配置\n" "===============================================\n" "请按以下步骤配置:\n\n" "1. 申请 Scnet API Token:\n" " 访问 https://www.scnet.cn 注册并获取密钥\n\n" "2. 配置 Token:\n" f" 编辑 {ENV_FILE}\n" " 设置 SCNET_API_KEY=你的密钥\n" ) sys.exit(error_msg) config.setdefault('SCNET_API_BASE', 'https://api.scnet.cn/api/llm/v1') return config def recognize_with_retry(ocr_type, file_path, config, retry_count=0): """ 带重试机制的 OCR 识别函数。 当遇到 429 (Too Many Requests) 时,自动等待后重试。 调用 Scnet OCR API 进行识别""" api_base = config['SCNET_API_BASE'] api_key = config['SCNET_API_KEY'] url = f"{api_base}/ocr/recognize" # 检查文件是否存在 if not os.path.isfile(file_path): sys.exit(f"错误: 文件不存在 - {file_path}") # 自动检测 MIME 类型 mime_type, _ = mimetypes.guess_type(file_path) if mime_type is None: mime_type = 'application/octet-stream' headers = { 'Authorization': f'Bearer {api_key}' } try: with open(file_path, 'rb') as f: files = { 'file': (os.path.basename(file_path), f, mime_t ...[truncated 2572 chars]- Remediation
View remediation
Remediation Suggestions
- Remove arbitrary endpoint configuration if it is not operationally required, and use a constant trusted endpoint:
python API_BASE = "https://api.scnet.cn/api/llm/v1" - If endpoint configuration must remain available, parse it with
urllib.parse.urlsplitand require:- The
httpsscheme. - An exact allowlisted hostname such as
api.scnet.cn. - No embedded username or password.
- An approved port and path prefix.
- The
- Reject unexpected destinations before reading the invoice or constructing the authorization header.
- Do not attach the Scnet credential to custom or non-Scnet destinations. Use separate credentials for explicitly supported private endpoints.
- Establish a restrictive redirect policy and verify the final destination before transmitting sensitive material.
- Protect
config/.envwith owner-only permissions and verify that it is not writable by less-privileged users. - Require explicit user confirmation before uploading sensitive invoices and clearly display the validated destination.
- Add automated tests confirming rejection of plaintext URLs, unexpected hosts, userinfo, unapproved ports, and malformed endpoints.
- Remove arbitrary endpoint configuration if it is not operationally required, and use a constant trusted endpoint:
