From 86b4ccc1df2cd544e057c225ec6824f246028bb9 Mon Sep 17 00:00:00 2001 From: Faye Duxovni Date: Thu, 9 Dec 2021 10:03:25 -0500 Subject: [PATCH] add random string to report directories to prevent time-based collisions --- changelog.d/39.bugfix | 1 + main.go | 2 ++ submit.go | 5 +++++ 3 files changed, 8 insertions(+) create mode 100644 changelog.d/39.bugfix diff --git a/changelog.d/39.bugfix b/changelog.d/39.bugfix new file mode 100644 index 0000000..13f492b --- /dev/null +++ b/changelog.d/39.bugfix @@ -0,0 +1 @@ +Prevent timestamp collisions when reports are submitted within 1 second of each other. \ No newline at end of file diff --git a/main.go b/main.go index b678512..5cce776 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "fmt" "io/ioutil" "log" + "math/rand" "net" "net/http" "os" @@ -146,6 +147,7 @@ func main() { } log.Printf("Using %s/listing as public URI", apiPrefix) + rand.Seed(time.Now().UnixNano()) http.Handle("/api/submit", &submitServer{ghClient, glClient, apiPrefix, slack, cfg}) // Make sure bugs directory exists diff --git a/submit.go b/submit.go index 799dea9..e53db1b 100644 --- a/submit.go +++ b/submit.go @@ -20,11 +20,13 @@ import ( "bytes" "compress/gzip" "context" + "encoding/base32" "encoding/json" "fmt" "io" "io/ioutil" "log" + "math/rand" "mime" "mime/multipart" "net/http" @@ -145,6 +147,9 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { // files straight in t := time.Now().UTC() prefix := t.Format("2006-01-02/150405") + randBytes := make([]byte, 5) + rand.Read(randBytes) + prefix += "-" + base32.StdEncoding.EncodeToString(randBytes) reportDir := filepath.Join("bugs", prefix) if err := os.MkdirAll(reportDir, os.ModePerm); err != nil { log.Println("Unable to create report directory", err)