{"record":{"id":"ceff65be78568b39","repo":"ZhuLinsen/daily_stock_analysis","slug":"image-webp","errorCode":null,"errorMessage":"文件内容与声明的类型 image/webp 不匹配，可能被篡改","messagePattern":"文件内容与声明的类型 image/webp 不匹配，可能被篡改","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"src/services/image_stock_extractor.py","lineNumber":84,"sourceCode":"\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\n    # US stocks: 1-5 letters, optionally with . (e.g. BRK.B)\n    if re.match(r\"^[A-Z]{1,5}(\\.[A-Z])?$\", s):\n        return s","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/src/services/image_stock_extractor.py#L66-L102","documentation":"Error \"文件内容与声明的类型 image/webp 不匹配，可能被篡改\" thrown in ZhuLinsen/daily_stock_analysis.","triggerScenarios":"Thrown at src/services/image_stock_extractor.py:84 when the library encounters an invalid state.","commonSituations":"Raised when file bytes do not match the declared image/webp type; occurs when a non-webp file is uploaded with a webp content-type, indicating tampering or a mislabelled file.","solutions":["Upload a genuine WebP file; the file content does not match the declared image/webp type.","Re-export or convert the image to WebP instead of just renaming the extension.","Check that no proxy or client rewrote the file bytes in transit."],"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-15T17:31:12.345Z"}