{"record":{"id":"09e4a7c3addd99be","repo":"Tencent/WeKnora","slug":"create-request-w-09e4a7","errorCode":null,"errorMessage":"create request: %w","messagePattern":"create request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/docparser/image_resolver.go","lineNumber":1010,"sourceCode":"\n\tmarkdown, htmlImages := resolveRemoteImagePass(ctx, markdown, remotePassSpec{\n\t\tPattern:  imgHTMLSrc,\n\t\tURLGroup: searchutil.HTMLImageSrcURLGroup,\n\t\tSyntax:   \"html\",\n\t\tSrcOf:    htmlAttrSrc,\n\t}, client, fileSvc, tenantID)\n\n\timages = append(images, mdImages...)\n\timages = append(images, htmlImages...)\n\treturn markdown, images, nil\n}\n\n// downloadImage fetches an image from remoteURL using the provided SSRF-safe\n// client. It validates Content-Type and enforces maxRemoteImageSize.\nfunc downloadImage(ctx context.Context, client *http.Client, remoteURL string) (data []byte, mimeType string, err error) {\n\treq, err := http.NewRequestWithContext(ctx, http.MethodGet, remoteURL, nil)\n\tif err != nil {\n\t\treturn nil, \"\", fmt.Errorf(\"create request: %w\", err)\n\t}\n\t// Some CDNs require a browser-like User-Agent.\n\treq.Header.Set(\"User-Agent\", \"Mozilla/5.0 (compatible; WeKnora/1.0)\")\n\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\treturn nil, \"\", fmt.Errorf(\"HTTP GET: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn nil, \"\", fmt.Errorf(\"unexpected status %d\", resp.StatusCode)\n\t}\n\n\t// Determine MIME type from Content-Type header.\n\tct := resp.Header.Get(\"Content-Type\")\n\tmimeType, _, _ = mime.ParseMediaType(ct)\n\tif mimeType == \"\" {","sourceCodeStart":992,"sourceCodeEnd":1028,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/docparser/image_resolver.go#L992-L1028","documentation":"This error is returned when http.NewRequestWithContext fails to construct the GET request for a remote image download, before any network I/O occurs. It almost always indicates a malformed URL (unparseable scheme or host) or an invalid/nested context setup.","triggerScenarios":"downloadImage receives a remoteURL that url.Parse cannot handle — e.g. contains control characters, spaces, or is not a valid absolute URL.","commonSituations":"Unescaped characters extracted from document markup, URLs with stray whitespace or newline, partially formed href/src attributes scraped from documents.","solutions":["Validate/clean the URL before calling downloadImage (trim whitespace, url.Parse check)","Ensure the URL is absolute with http/https scheme","Reject or skip images with malformed src attributes at extraction time","Log the offending URL to identify the malformed source"],"exampleFix":"// before\nreq, err := http.NewRequestWithContext(ctx, http.MethodGet, remoteURL, nil)\nif err != nil { return nil, \"\", fmt.Errorf(\"create request: %w\", err) }\n// after\nu, perr := url.Parse(strings.TrimSpace(remoteURL))\nif perr != nil || (u.Scheme != \"http\" && u.Scheme != \"https\") {\n    return nil, \"\", fmt.Errorf(\"invalid remote image url: %q\", remoteURL)\n}\nreq, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)\nif err != nil { return nil, \"\", fmt.Errorf(\"create request: %w\", err) }","handlingStrategy":"validation","validationCode":"func isFetchableURL(raw string) bool {\n    u, err := url.Parse(strings.TrimSpace(raw))\n    return err == nil && (u.Scheme == \"http\" || u.Scheme == \"https\") && u.Host != \"\"\n}\n// call before fetching: if !isFetchableURL(imgURL) { skip }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Sanitize URLs extracted from documents (trim whitespace, escape characters)","Validate scheme and host before constructing requests","Reject malformed src/href attributes at extraction time","Unit-test URL cleaning with edge cases (spaces, control chars, relative URLs)"],"tags":["http","url","request","validation"],"backgroundTag":"invalid-url","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}