{"record":{"id":"9d178a7afb0b99e9","repo":"ZhuLinsen/daily_stock_analysis","slug":"mime-type-9d178a","errorCode":null,"errorMessage":"文件内容与声明的类型 {mime_type} 不匹配，可能被篡改","messagePattern":"文件内容与声明的类型 (.+?) 不匹配，可能被篡改","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"src/services/image_stock_extractor.py","lineNumber":89,"sourceCode":"    \"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\n    # 尝试去除 SH/SZ 后缀\n    for suffix in (\".SH\", \".SZ\", \".SS\"):\n        if s.endswith(suffix):\n            base = s[: -len(suffix)].strip()\n            if base.isdigit() and len(base) in (5, 6):","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/src/services/image_stock_extractor.py#L71-L107","documentation":"Error \"文件内容与声明的类型 {mime_type} 不匹配，可能被篡改\" thrown in ZhuLinsen/daily_stock_analysis.","triggerScenarios":"Thrown at src/services/image_stock_extractor.py:89 when the library encounters an invalid state.","commonSituations":"Raised when file bytes do not match the declared MIME type; occurs when the actual file signature differs from the client-supplied content-type.","solutions":["Ensure the file content matches its declared MIME type; re-upload the original file.","Convert the file properly to the declared format rather than renaming extensions.","Check upload tooling for content-type spoofing or byte corruption."],"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"}