Skip to content

Commit

Permalink
allow adding whitelist for pubkeys allow to upload
Browse files Browse the repository at this point in the history
  • Loading branch information
w3irdrobot committed Feb 16, 2023
1 parent 6fa9ebe commit 1b5257d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 15 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type response struct {
Url string `json:"url"`
}

func upload(baseDir, domain string) http.HandlerFunc {
func upload(baseDir, domain string, pubkeys []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)
if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
Expand Down Expand Up @@ -75,6 +75,11 @@ func upload(baseDir, domain string) http.HandlerFunc {
signature := r.FormValue("signature")
logrus.WithFields(logrus.Fields{"pubkey": pubkey, "signature": signature}).Debug("form values")

if len(pubkeys) > 0 && !pubkeyIsApproved(pubkey, pubkeys) {
http.Error(w, "pubkey not approved", http.StatusUnauthorized)
return
}

validSig, err := checkSignature(pubkey, signature, shasum[:])
if err != nil {
logrus.WithError(err).Error("error checking the signature")
Expand Down Expand Up @@ -155,3 +160,12 @@ func fileServer(r chi.Router, path string, root http.FileSystem) {
fs.ServeHTTP(w, r)
})
}

func pubkeyIsApproved(pubkey string, pubkeys []string) bool {
for _, pk := range pubkeys {
if pk == pubkey {
return true
}
}
return false
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
f.String("dir", "./uploads", "directory to upload and serve files from")
f.String("domain", "", "domain files are served from")
f.String("log-level", "info", "level of logs to output")
f.StringSlice("pubkey", []string{}, "pubkey to whitelist for uploading files")
f.Parse(os.Args[1:])

config, err := getConfiguration(f)
Expand Down Expand Up @@ -65,7 +66,7 @@ func main() {
router.Use(middleware.CleanPath)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Post("/upload", upload(dir, domain))
router.Post("/upload", upload(dir, domain, config.Strings("pubkey")))
fileServer(router, "/static", http.Dir(dir))

host := fmt.Sprintf("%s:%d", config.String("host"), config.Int("port"))
Expand Down

0 comments on commit 1b5257d

Please sign in to comment.