{"record":{"id":"ed243953efb4576e","repo":"ZhuLinsen/daily_stock_analysis","slug":"mime-type","errorCode":null,"errorMessage":"无法验证类型: {mime_type}","messagePattern":"无法验证类型: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"src/services/image_stock_extractor.py","lineNumber":81,"sourceCode":"ALLOWED_MIME = frozenset({\"image/jpeg\", \"image/png\", \"image/webp\", \"image/gif\"})\nMAX_SIZE_BYTES = 5 * 1024 * 1024  # 5MB\nVISION_API_TIMEOUT = 60  # seconds; avoid long blocks on network/API issues\n\n# Magic bytes for server-side MIME validation (client Content-Type can be forged)\n_IMAGE_SIGNATURES = {\n    \"image/jpeg\": [b\"\\xff\\xd8\\xff\"],\n    \"image/png\": [b\"\\x89PNG\\r\\n\\x1a\\n\"],\n    \"image/gif\": [b\"GIF87a\", b\"GIF89a\"],\n    \"image/webp\": [b\"RIFF\"],  # bytes[8:12] must be WEBP, checked separately\n}\n\n\ndef _verify_image_magic_bytes(image_bytes: bytes, mime_type: str) -> None:\n    \"\"\"Verify actual file content matches declared MIME type (rejects forged Content-Type).\"\"\"\n    if len(image_bytes) < 12:\n        raise ValueError(\"图片文件过小或损坏\")\n    if mime_type not in _IMAGE_SIGNATURES:\n        raise ValueError(f\"无法验证类型: {mime_type}\")\n    if mime_type == \"image/webp\":\n        if image_bytes[:4] != b\"RIFF\" or image_bytes[8:12] != b\"WEBP\":\n            raise ValueError(\"文件内容与声明的类型 image/webp 不匹配，可能被篡改\")\n        return\n    for sig in _IMAGE_SIGNATURES[mime_type]:\n        if image_bytes.startswith(sig):\n            return\n    raise ValueError(f\"文件内容与声明的类型 {mime_type} 不匹配，可能被篡改\")\n\n\ndef _normalize_code(raw: str) -> Optional[str]:\n    \"\"\"Normalize and validate a single stock code. A-shares & HK: 5-6 digits; US: 1-5 letters.\"\"\"\n    s = raw.strip().upper()\n    if not s:\n        return None\n    # A-shares & HK: 5-6 digit codes (600519, 00700, 09988)\n    if s.isdigit() and len(s) in (5, 6):\n        return s","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/src/services/image_stock_extractor.py#L63-L99","documentation":"Error \"无法验证类型: {mime_type}\" thrown in ZhuLinsen/daily_stock_analysis.","triggerScenarios":"Thrown at src/services/image_stock_extractor.py:81 when the library encounters an invalid state.","commonSituations":"Raised when the declared MIME type of an uploaded image cannot be verified; occurs with uncommon or missing content-type headers on upload.","solutions":["Upload the file with a supported image MIME type (png, jpeg, webp, etc.).","Check the Content-Type of the upload; correct the client to send the proper type.","Convert the file to a supported image format and retry."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}