Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion main.go
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"
)
Comment on lines +2 to +7
Copy link
Copy Markdown

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.

ioutil is deprecated; MD5 is cryptographically broken. Prefer os + streaming and sha256.

-import (
-  "crypto/md5"
-  "fmt"
-  "io/ioutil"
-  "net/http"
-)
+import (
+  "crypto/sha256"
+  "fmt"
+  "io"
+  "log"
+  "net/http"
+  "os"
+  "path/filepath"
+  "strings"
+  "time"
+)
📝 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.

Suggested change
import (
"crypto/md5"
"fmt"
"io/ioutil"
"net/http"
)
import (
"crypto/sha256"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
🤖 Prompt for AI Agents
In main.go around lines 2 to 7, the code imports deprecated ioutil and insecure
crypto/md5; replace them with non-deprecated streaming APIs and a modern hash:
swap "io/ioutil" for "io" (and "os" if reading files from disk), replace
"crypto/md5" with "crypto/sha256", and update the code to stream data into the
hasher (e.g., create sha256.New() and use io.Copy(hasher, source) where source
is an http response body or os.File) instead of reading everything with ReadAll;
ensure you close response bodies and handle errors appropriately.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Block path traversal, handle I/O errors, avoid full-file reads, and set Content-Type.

Untrusted file is concatenated into a path (path traversal). Read errors are ignored, and ReadFile loads entire files (DoS risk). Also returns a digest without declaring a content type.

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

‼️ 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.

Suggested change
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))
})

http.ListenAndServe(":8080", nil)
}