fix(webui): refuse change captures whose file entries have the wrong key set
This commit is contained in:
1 parent
55c201e7c1
commit
18eed1fb96
1 file changed
+27
-7
@@ -64,6 +64,23 @@ type capturePayload struct {
|
||||
// key set are refused.
|
||||
var capturePayloadKeys = structKeys(reflect.TypeFor[capturePayload]())
|
||||
|
||||
// fileChangeKeys is the key set every files[] entry must carry exactly.
|
||||
var fileChangeKeys = structKeys(reflect.TypeFor[task.FileChange]())
|
||||
|
||||
// exactKeys decodes raw as an object and reports whether its key set is want.
|
||||
func exactKeys(raw []byte, want map[string]struct{}) (map[string]json.RawMessage, bool) {
|
||||
var keys map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &keys); err != nil || len(keys) != len(want) {
|
||||
return nil, false
|
||||
}
|
||||
for key := range want {
|
||||
if _, ok := keys[key]; !ok {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
return keys, true
|
||||
}
|
||||
|
||||
// structKeys collects the JSON key names a struct declares, recursing into the
|
||||
// embedded value structs a decoder flattens.
|
||||
func structKeys(t reflect.Type) map[string]struct{} {
|
||||
@@ -139,17 +156,20 @@ func decodeCapture(e events.Event) (changeCaptureView, bool) {
|
||||
if err != nil {
|
||||
return changeCaptureView{}, false
|
||||
}
|
||||
var keys map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &keys); err != nil {
|
||||
keys, ok := exactKeys(raw, capturePayloadKeys)
|
||||
if !ok {
|
||||
return changeCaptureView{}, false
|
||||
}
|
||||
if len(keys) != len(capturePayloadKeys) {
|
||||
return changeCaptureView{}, false
|
||||
}
|
||||
for key := range capturePayloadKeys {
|
||||
if _, ok := keys[key]; !ok {
|
||||
if files := keys["files"]; string(files) != "null" {
|
||||
var entries []json.RawMessage
|
||||
if err := json.Unmarshal(files, &entries); err != nil {
|
||||
return changeCaptureView{}, false
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if _, ok := exactKeys(entry, fileChangeKeys); !ok {
|
||||
return changeCaptureView{}, false
|
||||
}
|
||||
}
|
||||
}
|
||||
var payload capturePayload
|
||||
if err := json.Unmarshal(raw, &payload); err != nil {
|
||||
|
||||
Reference in new issue
Block a user