-
Notifications
You must be signed in to change notification settings - Fork 0
[Fix] Decode Percent-encoded Characters in URL #17
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
Conversation
WalkthroughThe changes add URL percent-decoding functionality in the file handling process. The Changes
Sequence Diagram(s)sequenceDiagram
participant C as Client
participant FH as fileHandler
participant U as url.PathUnescape
C->>FH: GET /files/...
FH->>U: Unescape URL path
U-->>FH: Decoded path or error
alt Decoding successful
FH->>C: Serve corresponding file
else Decoding error
FH-->>C: Return HTTP 400 "Invalid URL"
end
Poem
Tip 🌐 Web search-backed reviews and chat
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
close #15 |
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
servefiles/main.go(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: build
🔇 Additional comments (1)
servefiles/main.go (1)
10-10: LGTM!The addition of the
net/urlpackage is appropriate for implementing URL decoding functionality.
| // Decode percent-encoded characters | ||
| path, err := url.PathUnescape(path) | ||
| if err != nil { | ||
| http.Error(w, "Invalid URL", http.StatusBadRequest) | ||
| return | ||
| } |
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.
Add path sanitization to prevent directory traversal.
While the URL decoding is implemented correctly, the decoded path needs to be sanitized before use to prevent potential directory traversal attacks.
Apply this diff to add path sanitization:
// Decode percent-encoded characters
path, err := url.PathUnescape(path)
if err != nil {
- http.Error(w, "Invalid URL", http.StatusBadRequest)
+ http.Error(w, "Invalid URL encoding", http.StatusBadRequest)
return
}
+ // Clean the path to prevent directory traversal
+ path = filepath.Clean(path)
+ if strings.HasPrefix(path, "..") {
+ http.Error(w, "Invalid path", http.StatusBadRequest)
+ return
+ }Don't forget to add the required import:
+ "strings"📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Decode percent-encoded characters | |
| path, err := url.PathUnescape(path) | |
| if err != nil { | |
| http.Error(w, "Invalid URL", http.StatusBadRequest) | |
| return | |
| } | |
| // Decode percent-encoded characters | |
| path, err := url.PathUnescape(path) | |
| if err != nil { | |
| http.Error(w, "Invalid URL encoding", http.StatusBadRequest) | |
| return | |
| } | |
| // Clean the path to prevent directory traversal | |
| path = filepath.Clean(path) | |
| if strings.HasPrefix(path, "..") { | |
| http.Error(w, "Invalid path", http.StatusBadRequest) | |
| return | |
| } |
Relate #15
Summary by CodeRabbit