MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / decodeAppJSON

Function decodeAppJSON

internal/server/app_api_write.go:54–77  ·  view source on GitHub ↗

decodeAppJSON decodes a request DTO through the ordinary body rules (size cap, NUL refusal, repeated members) and refuses any key the DTO does not name, so nothing a DTO does not name can be written.

(r *http.Request, v any)

Source from the content-addressed store, hash-verified

52// cap, NUL refusal, repeated members) and refuses any key the DTO does not
53// name, so nothing a DTO does not name can be written.
54func decodeAppJSON(r *http.Request, v any) error {
55 raw, err := readBodyForDecode(r, defaultJSONBodyLimit)
56 if err != nil {
57 return fmt.Errorf("invalid JSON: %w", err)
58 }
59 if err := decodeJSONBytes(raw, v); err != nil {
60 return err
61 }
62 var keys map[string]json.RawMessage
63 if err := json.NewDecoder(bytes.NewReader(raw)).Decode(&keys); err != nil {
64 return fmt.Errorf("invalid JSON: %w", err)
65 }
66 allowed := map[string]bool{}
67 tp := reflect.TypeOf(v).Elem()
68 for i := 0; i < tp.NumField(); i++ {
69 allowed[strings.Split(tp.Field(i).Tag.Get("json"), ",")[0]] = true
70 }
71 for k := range keys {
72 if !allowed[k] {
73 return fmt.Errorf("unknown field %q", k)
74 }
75 }
76 return nil
77}
78
79// appActor is the FencedActor for this request: the bot as an agent for a
80// service token (§4: the actor is decided by token kind; X-Pad-Agent is

Callers 4

appCreateItemMethod · 0.85
appUpdateItemMethod · 0.85
appCreateCommentMethod · 0.85
appUpdateCommentMethod · 0.85

Calls 4

readBodyForDecodeFunction · 0.85
decodeJSONBytesFunction · 0.85
DecodeMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected