Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pss target input validation #2463

Merged
merged 2 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ components:
type: string

PssTargets:
pattern: '^[0-9a-fA-F]{1,6}(,[0-9a-fA-F]{1,6})*$'
description: List of hex string targets that are comma seprated and can have maximum length of 6
type: string

PssTopic:
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/pss.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -42,13 +43,13 @@ func (s *server) pssPostHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
s.logger.Debugf("pss send: bad target (%s): %v", target, err)
s.logger.Errorf("pss send: bad target (%s): %v", target, err)
jsonhttp.BadRequest(w, nil)
jsonhttp.BadRequest(w, "target is not valid hex string")
return
}
if len(target) > targetMaxLength {
s.logger.Debugf("pss send: bad target length: %d", len(target))
s.logger.Errorf("pss send: bad target length: %d", len(target))
jsonhttp.BadRequest(w, nil)
jsonhttp.BadRequest(w, fmt.Sprintf("hex string target exceeds max length of %d", targetMaxLength*2))
return
}
targets = append(targets, target)
Expand Down
12 changes: 11 additions & 1 deletion pkg/api/pss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,17 @@ func TestPssSend(t *testing.T) {
jsonhttptest.Request(t, client, http.MethodPost, "/pss/send/to/badtarget?recipient="+recipient, http.StatusBadRequest,
jsonhttptest.WithRequestBody(bytes.NewReader(payload)),
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: "Bad Request",
Message: "target is not valid hex string",
Code: http.StatusBadRequest,
}),
)

// If this test needs to be modified (most probably because the max target length changed)
// the please verify that SwarmCommon.yaml -> components -> PssTarget also reflects the correct value
jsonhttptest.Request(t, client, http.MethodPost, "/pss/send/to/123456789abcdf?recipient="+recipient, http.StatusBadRequest,
jsonhttptest.WithRequestBody(bytes.NewReader(payload)),
jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
Message: "hex string target exceeds max length of 6",
Code: http.StatusBadRequest,
}),
)
Expand Down