-
Notifications
You must be signed in to change notification settings - Fork 0
Vulnerable sample for Action test #2
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
base: main
Are you sure you want to change the base?
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1 +1,15 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main; func main(){} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "crypto/md5" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "io/ioutil" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func main() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file := r.URL.Query().Get("file") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| b, _ := ioutil.ReadFile("./data/" + file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Fprintf(w, "%x", md5.Sum(b)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+13
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. Block path traversal, handle I/O errors, avoid full-file reads, and set Content-Type. Untrusted Apply this safer handler: - http.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) {
- file := r.URL.Query().Get("file")
- b, _ := ioutil.ReadFile("./data/" + file)
- fmt.Fprintf(w, "%x", md5.Sum(b))
- })
+ http.HandleFunc("/read", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ file := r.URL.Query().Get("file")
+ if file == "" {
+ http.Error(w, "missing 'file' parameter", http.StatusBadRequest)
+ return
+ }
+ base := "./data"
+ baseAbs, err := filepath.Abs(base)
+ if err != nil {
+ http.Error(w, "server error", http.StatusInternalServerError)
+ return
+ }
+ targetAbs, err := filepath.Abs(filepath.Join(base, file))
+ if err != nil || !(targetAbs == baseAbs || strings.HasPrefix(targetAbs, baseAbs+string(os.PathSeparator))) {
+ http.Error(w, "invalid path", http.StatusBadRequest)
+ return
+ }
+ f, err := os.Open(targetAbs)
+ if err != nil {
+ if os.IsNotExist(err) {
+ http.Error(w, "not found", http.StatusNotFound)
+ } else {
+ http.Error(w, "unable to open file", http.StatusInternalServerError)
+ }
+ return
+ }
+ defer f.Close()
+ info, err := f.Stat()
+ if err != nil || info.IsDir() {
+ http.Error(w, "invalid file", http.StatusBadRequest)
+ return
+ }
+ const max = 10 << 20 // 10 MiB
+ if info.Size() > max {
+ http.Error(w, "file too large", http.StatusRequestEntityTooLarge)
+ return
+ }
+ h := sha256.New()
+ if _, err := io.Copy(h, f); err != nil {
+ http.Error(w, "read error", http.StatusInternalServerError)
+ return
+ }
+ fmt.Fprintf(w, "%x", h.Sum(nil))
+ })📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http.ListenAndServe(":8080", nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🛠️ Refactor suggestion
Use non-deprecated APIs and a modern hash.
ioutilis deprecated; MD5 is cryptographically broken. Preferos+ streaming andsha256.📝 Committable suggestion
🤖 Prompt for AI Agents