-
Notifications
You must be signed in to change notification settings - Fork 5
Stop reproducing file-upload field values in config decode errors #1113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ package field | |
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "errors" | ||
| "fmt" | ||
| "io/fs" | ||
| "net/url" | ||
| "os" | ||
| "reflect" | ||
|
|
@@ -75,7 +77,7 @@ func getFileContentFromPath(path string) ([]byte, error) { | |
| // Check if the file exists | ||
| fileInfo, err := os.Stat(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot access file: %w", err) | ||
| return nil, fmt.Errorf("cannot access file: %w", redactPathError(err)) | ||
| } | ||
|
|
||
| // Check file size limit (2MB) | ||
|
|
@@ -87,11 +89,19 @@ func getFileContentFromPath(path string) ([]byte, error) { | |
| // Read the file | ||
| content, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error reading file: %w", err) | ||
| return nil, fmt.Errorf("error reading file: %w", redactPathError(err)) | ||
| } | ||
| return content, nil | ||
| } | ||
|
|
||
| func redactPathError(err error) error { | ||
| var pathErr *fs.PathError | ||
| if errors.As(err, &pathErr) { | ||
| return pathErr.Err | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // parseFileContent returns the file upload content from a string field value. | ||
| func parseFileContent(data string) ([]byte, error) { | ||
| if data == "" { | ||
|
|
@@ -118,6 +128,10 @@ func parseFileContent(data string) ([]byte, error) { | |
| func parseJSONBase64DataURL(dataURL string) ([]byte, error) { | ||
| parsedURL, err := url.Parse(dataURL) | ||
| if err != nil { | ||
| var urlErr *url.Error | ||
| if errors.As(err, &urlErr) { | ||
| return nil, fmt.Errorf("invalid data URL: %w", urlErr.Err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion (low confidence): this redacts the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion: Unwrapping to |
||
| } | ||
| return nil, fmt.Errorf("invalid data URL: %w", err) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Suggestion (low confidence, SDK compatibility): returning
pathErr.Errdrops*fs.PathErrorfrom the error chain, so a downstream caller doingerrors.As(err, &pathErr)to inspectOp/Pathno longer matches. Sentinel checks are unaffected (errors.Is(err, fs.ErrNotExist)/os.IsNotExiststill work, since thesyscall.Errnois preserved), and this error only surfaces through the config decode path, so real breakage is unlikely — worth a line in the PR description as an intentional error-shape change rather than a code change.