Commit cd2df08f authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Faster validation of JSON object input payloads.

parent cfa07e1b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
## [Unreleased]
### Changed
- Do not use absolute path for `tini` executable in default container entrypoint.
- Faster validation of JSON object input payloads.

### Fixed
- Correctly read pagination cursor in notification listings.
+3 −0
Original line number Diff line number Diff line
@@ -58,6 +58,9 @@ import (
	"google.golang.org/grpc/status"
)

// Used as part of JSON input validation.
const byteBracket byte = '{'

// Keys used for storing/retrieving user information in the context of a request after authentication.
type ctxUserIDKey struct{}
type ctxUsernameKey struct{}
+2 −2
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
package server

import (
	"bytes"
	"context"
	"encoding/json"

@@ -184,8 +185,7 @@ func (s *ApiServer) WriteLeaderboardRecord(ctx context.Context, in *api.WriteLea
	} else if in.Record == nil {
		return nil, status.Error(codes.InvalidArgument, "Invalid input, record score value is required.")
	} else if in.Record.Metadata != "" {
		var maybeJSON map[string]interface{}
		if json.Unmarshal([]byte(in.Record.Metadata), &maybeJSON) != nil {
		if maybeJSON := []byte(in.Record.Metadata); !json.Valid(maybeJSON) || bytes.TrimSpace(maybeJSON)[0] != byteBracket {
			return nil, status.Error(codes.InvalidArgument, "Metadata value must be JSON, if provided.")
		}
	}
+2 −2
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
package server

import (
	"bytes"
	"context"
	"encoding/json"

@@ -201,8 +202,7 @@ func (s *ApiServer) WriteStorageObjects(ctx context.Context, in *api.WriteStorag
			}
		}

		var maybeJSON map[string]interface{}
		if json.Unmarshal([]byte(object.GetValue()), &maybeJSON) != nil {
		if maybeJSON := []byte(object.GetValue()); !json.Valid(maybeJSON) || bytes.TrimSpace(maybeJSON)[0] != byteBracket {
			return nil, status.Error(codes.InvalidArgument, "Value must be a JSON object.")
		}
	}
+1 −2
Original line number Diff line number Diff line
@@ -303,8 +303,7 @@ func (s *ApiServer) WriteTournamentRecord(ctx context.Context, in *api.WriteTour
	} else if in.GetRecord() == nil {
		return nil, status.Error(codes.InvalidArgument, "Invalid input, record score value is required.")
	} else if in.GetRecord().GetMetadata() != "" {
		var maybeJSON map[string]interface{}
		if json.Unmarshal([]byte(in.GetRecord().GetMetadata()), &maybeJSON) != nil {
		if maybeJSON := []byte(in.GetRecord().GetMetadata()); !json.Valid(maybeJSON) || bytes.TrimSpace(maybeJSON)[0] != byteBracket {
			return nil, status.Error(codes.InvalidArgument, "Metadata value must be JSON, if provided.")
		}
	}
Loading