feat: signed one-time uploads for material files
- classes/uploads.go: tickets signed with HMAC-SHA256 under the
jz.bm.upload.v1: label (app.key), 10-minute expiry, the nonce pulled
from the cache once (duplicated from golem15.presentations on purpose)
- POST /api/v1/bm/materials/{id}/files/upload-url issues the URL with a
curl example; POST /_bm/api/v1/uploads/{ticket} takes one file part,
no bearer, throttled per IP and per ticket, user and permission
re-checked, stored through storeMaterialFile
- bm_manage_create_material_upload; README documents the file routes
This commit is contained in:
48
routes.go
48
routes.go
@@ -1,6 +1,8 @@
|
||||
package bm
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -10,10 +12,12 @@ import (
|
||||
userclasses "git.golem15.com/golem15/sm-user-plugin/classes"
|
||||
usermiddleware "git.golem15.com/golem15/sm-user-plugin/middleware"
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/cooler"
|
||||
"git.golem15.com/golem15/summercms/modules/lighthouse"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"git.golem15.com/golem15/summercms/modules/phrasebook"
|
||||
"git.golem15.com/golem15/summercms/modules/surf"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/classes"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/controllers"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/controllers/api"
|
||||
"git.golem15.com/jakub/sm-bm-plugin/models"
|
||||
@@ -45,11 +49,16 @@ const (
|
||||
// (it runs after user.api_token), or per client IP for a request
|
||||
// without one.
|
||||
bucketToken = "throttle:jz.bm.token"
|
||||
// bucketUploadIP and bucketUploadTicket throttle the signed material
|
||||
// upload per client IP and per ticket: it carries no bearer, so the
|
||||
// ticket in its path is the only credential (D-12).
|
||||
bucketUploadIP = "throttle:jz.bm.upload-ip"
|
||||
bucketUploadTicket = "throttle:jz.bm.upload-ticket"
|
||||
)
|
||||
|
||||
// Buckets declares the rate limits of the public calendar feed, the
|
||||
// management API, the token twins of the student API, the realtime routes
|
||||
// and the BM chat routes.
|
||||
// management API, the token twins of the student API, the signed material
|
||||
// upload, the realtime routes and the BM chat routes.
|
||||
func (p *Plugin) Buckets() map[string]surf.Bucket {
|
||||
trusted := surf.TrustedProxies(nil)
|
||||
if p.app != nil {
|
||||
@@ -83,6 +92,20 @@ func (p *Plugin) Buckets() map[string]surf.Bucket {
|
||||
return "jz.bm.manage.ip|" + surf.ClientIP(r, trusted)
|
||||
},
|
||||
},
|
||||
"jz.bm.upload-ip": {
|
||||
Max: 10,
|
||||
Decay: time.Minute,
|
||||
Key: func(r *http.Request) string { return "jz.bm.upload.ip|" + surf.ClientIP(r, trusted) },
|
||||
},
|
||||
"jz.bm.upload-ticket": {
|
||||
Max: 3,
|
||||
Decay: time.Minute,
|
||||
Key: func(r *http.Request) string {
|
||||
// A digest, so the ticket itself is not kept as a limiter key.
|
||||
sum := sha256.Sum256([]byte(r.PathValue("ticket")))
|
||||
return "jz.bm.upload.ticket|" + hex.EncodeToString(sum[:])[:32]
|
||||
},
|
||||
},
|
||||
"jz.bm.realtime": {
|
||||
Max: 120,
|
||||
Decay: time.Minute,
|
||||
@@ -174,6 +197,8 @@ func (p *Plugin) Routes(r pact.Router) error {
|
||||
g.Where("id", "[0-9]+")
|
||||
g.Post("/materials/{id}/files/inline", h.AttachMaterialInline, "user.scope:write", "user.permission:"+controllers.PermMaterials, "body.limit:"+strconv.Itoa(api.MaterialInlineBodyLimit))
|
||||
g.Where("id", "[0-9]+")
|
||||
g.Post("/materials/{id}/files/upload-url", h.CreateMaterialUpload, "user.scope:write", "user.permission:"+controllers.PermMaterials)
|
||||
g.Where("id", "[0-9]+")
|
||||
g.Delete("/materials/{id}/files/{file}", h.DetachMaterialFile, "user.scope:write", "user.permission:"+controllers.PermMaterials)
|
||||
g.Where("id", "[0-9]+")
|
||||
g.Where("file", "[0-9]+")
|
||||
@@ -200,6 +225,15 @@ func (p *Plugin) Routes(r pact.Router) error {
|
||||
"user.permission:"+controllers.PermSchedule,
|
||||
"user.permission:"+controllers.PermMaterials)
|
||||
})
|
||||
// The signed material upload (D-10, D-12): no bearer, because curl sends
|
||||
// the file without one; the ticket in the path is the credential. It is
|
||||
// bound to one material and one user, expires and works once, and the
|
||||
// handler re-checks the user's jz.bm.access_materials permission.
|
||||
// Throttled per IP and per ticket; the body is capped at one material
|
||||
// file plus the multipart framing.
|
||||
r.Group(APIPrefix+"/uploads", surf.Use(bucketUploadIP, bucketUploadTicket), func(g pact.Router) {
|
||||
g.Post("/{ticket}", h.SignedMaterialUpload, "body.limit:"+strconv.Itoa(api.MaterialFilesBodyLimit))
|
||||
})
|
||||
// The calendar feed: public, because calendar clients fetch it without
|
||||
// credentials; the token in the path is the credential. ServeMux cannot
|
||||
// match a suffix inside a segment, so {token} carries the ".ics" and the
|
||||
@@ -302,6 +336,16 @@ func (p *Plugin) handlers() *api.Handlers {
|
||||
h.DashboardLimit = p.app.Config.Int("jz.bm.dashboard_limit")
|
||||
h.PublicURL = p.app.Config.String("jz.bm.public_url")
|
||||
h.AppURL = p.app.Config.String("app.url")
|
||||
h.UploadKey = func() []byte { return classes.UploadKey(p.app.Config.String("app.key")) }
|
||||
}
|
||||
if p.app != nil {
|
||||
h.Cache = func() cooler.Cache {
|
||||
cache, err := cooler.FromApp(p.app)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return cache
|
||||
}
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user