add random string to report directories to prevent time-based collisions

This commit is contained in:
Faye Duxovni 2021-12-09 10:03:25 -05:00
parent 065b2b9a04
commit 86b4ccc1df
3 changed files with 8 additions and 0 deletions

1
changelog.d/39.bugfix Normal file
View File

@ -0,0 +1 @@
Prevent timestamp collisions when reports are submitted within 1 second of each other.

View File

@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -146,6 +147,7 @@ func main() {
} }
log.Printf("Using %s/listing as public URI", apiPrefix) log.Printf("Using %s/listing as public URI", apiPrefix)
rand.Seed(time.Now().UnixNano())
http.Handle("/api/submit", &submitServer{ghClient, glClient, apiPrefix, slack, cfg}) http.Handle("/api/submit", &submitServer{ghClient, glClient, apiPrefix, slack, cfg})
// Make sure bugs directory exists // Make sure bugs directory exists

View File

@ -20,11 +20,13 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"context" "context"
"encoding/base32"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"mime" "mime"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
@ -145,6 +147,9 @@ func (s *submitServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// files straight in // files straight in
t := time.Now().UTC() t := time.Now().UTC()
prefix := t.Format("2006-01-02/150405") prefix := t.Format("2006-01-02/150405")
randBytes := make([]byte, 5)
rand.Read(randBytes)
prefix += "-" + base32.StdEncoding.EncodeToString(randBytes)
reportDir := filepath.Join("bugs", prefix) reportDir := filepath.Join("bugs", prefix)
if err := os.MkdirAll(reportDir, os.ModePerm); err != nil { if err := os.MkdirAll(reportDir, os.ModePerm); err != nil {
log.Println("Unable to create report directory", err) log.Println("Unable to create report directory", err)