{"record":{"id":"dc9113530624ae7c","repo":"Tencent/WeKnora","slug":"image-exceeds-d-bytes-limit","errorCode":null,"errorMessage":"image exceeds %d bytes limit","messagePattern":"image exceeds (.+?) bytes limit","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/docparser/image_resolver.go","lineNumber":1044,"sourceCode":"\tct := resp.Header.Get(\"Content-Type\")\n\tmimeType, _, _ = mime.ParseMediaType(ct)\n\tif mimeType == \"\" {\n\t\tmimeType = \"application/octet-stream\"\n\t}\n\n\t// Only allow image content types (or octet-stream which we sniff later).\n\tif !strings.HasPrefix(mimeType, \"image/\") && mimeType != \"application/octet-stream\" {\n\t\treturn nil, \"\", fmt.Errorf(\"non-image content type: %s\", mimeType)\n\t}\n\n\t// Read body with size limit.\n\tlimited := io.LimitReader(resp.Body, maxRemoteImageSize+1)\n\tbody, err := io.ReadAll(limited)\n\tif err != nil {\n\t\treturn nil, \"\", fmt.Errorf(\"read body: %w\", err)\n\t}\n\tif len(body) > maxRemoteImageSize {\n\t\treturn nil, \"\", fmt.Errorf(\"image exceeds %d bytes limit\", maxRemoteImageSize)\n\t}\n\n\t// If MIME was octet-stream, sniff the real type from body.\n\tif mimeType == \"application/octet-stream\" {\n\t\tdetected := http.DetectContentType(body)\n\t\tif strings.HasPrefix(detected, \"image/\") {\n\t\t\tmimeType = detected\n\t\t} else {\n\t\t\treturn nil, \"\", fmt.Errorf(\"downloaded data is not an image (sniffed: %s)\", detected)\n\t\t}\n\t}\n\n\treturn body, mimeType, nil\n}\n\n// extFromURLPath extracts the image file extension from the URL path segment.\nfunc extFromURLPath(rawURL string) string {\n\tp := path.Ext(path.Base(rawURL))","sourceCodeStart":1026,"sourceCodeEnd":1062,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/docparser/image_resolver.go#L1026-L1062","documentation":"downloadImage reads the remote image body through an io.LimitReader capped at maxRemoteImageSize+1 bytes; if the buffered body exceeds maxRemoteImageSize, the image is considered too large and downloadImage returns this error instead of storing it. It is a deliberate guard so oversized remote images do not exhaust memory or bloat converted documents.","triggerScenarios":"fetchAndStoreRemoteImage -> downloadImage fetched a remote image whose response body length exceeds maxRemoteImageSize bytes (the LimitReader read back more bytes than the cap).","commonSituations":"Documents referencing huge product photos, unresized camera originals, or scans served from a CDN; someone raised the document size limit but not the remote image cap; hotlinked images that cannot be resized server-side.","solutions":["Resize or recompress the image at its source/CDN so it is under maxRemoteImageSize bytes.","Raise the maxRemoteImageSize configuration value if your use case legitimately needs larger images.","Replace the hotlinked image in the source document with a smaller one.","Pre-download and inline the image locally so downloadImage is not invoked for it."],"exampleFix":"// before: raw 12MB camera original linked in the doc\n<img src=\"https://cdn.example.com/photos/IMG_0001.JPG\" />\n// after: serve a resized variant under the limit\n<img src=\"https://cdn.example.com/photos/IMG_0001_w1600.jpg\" />","handlingStrategy":"validation","validationCode":"resp, err := http.Head(imgURL)\nif err != nil { return err }\nconst maxRemoteImageSize = 10 << 20 // match library config\nif resp.ContentLength > maxRemoteImageSize {\n    return fmt.Errorf(\"image %s too large: %d > %d bytes\", imgURL, resp.ContentLength, maxRemoteImageSize)\n}","typeGuard":null,"tryCatchPattern":"body, mdPath, err := resolver.fetchAndStoreRemoteImage(ctx, req, imgURL)\nif err != nil {\n    if strings.Contains(err.Error(), \"bytes limit\") {\n        log.Warnf(\"skipping oversized remote image %s (limit exceeded)\", imgURL)\n        return nil // degrade gracefully: skip image, keep document text\n    }\n    return err\n}","preventionTips":["Serve images through a CDN/image-resizer that enforces output size under the library limit.","Keep maxRemoteImageSize in config alongside your document size limits and review them together.","Pre-fetch and validate image sizes when ingesting documents, not at conversion time.","Log the offending URL so authors can replace oversized images."],"tags":["image-download","size-limit","network"],"backgroundTag":"image-size-limit-exceeded","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}