Merge pull request #39 from matrix-org/fayed/fix-collisions

Add random string to report directories to prevent time-based collisions
This commit is contained in:
Faye Duxovni 2022-01-11 11:27:48 -05:00 committed by GitHub
commit c442ced2e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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)