Skip to content

Commit 36ca85f

Browse files
jpeletiernonsense
authored andcommitted
swarm/api: Fix #18007, missing signature should return HTTP 400 (#18008)
1 parent b351655 commit 36ca85f

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎swarm/api/http/server.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
484484
return
485485
}
486486

487-
if updateRequest.IsUpdate() {
487+
switch {
488+
case updateRequest.IsUpdate():
488489
// Verify that the signature is intact and that the signer is authorized
489490
// to update this feed
490491
// Check this early, to avoid creating a feed and then not being able to set its first update.
@@ -497,9 +498,8 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
497498
respondError(w, r, err.Error(), http.StatusInternalServerError)
498499
return
499500
}
500-
}
501-
502-
if query.Get("manifest") == "1" {
501+
fallthrough
502+
case query.Get("manifest") == "1":
503503
// we create a manifest so we can retrieve feed updates with bzz:// later
504504
// this manifest has a special "feed type" manifest, and saves the
505505
// feed identification used to retrieve feed updates later
@@ -519,6 +519,8 @@ func (s *Server) HandlePostFeed(w http.ResponseWriter, r *http.Request) {
519519
fmt.Fprint(w, string(outdata))
520520

521521
w.Header().Add("Content-type", "application/json")
522+
default:
523+
respondError(w, r, "Missing signature in feed update request", http.StatusBadRequest)
522524
}
523525
}
524526

‎swarm/api/http/server_test.go‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,45 @@ func TestBzzFeed(t *testing.T) {
333333
}
334334
urlQuery = testUrl.Query()
335335
body = updateRequest.AppendValues(urlQuery) // this adds all query parameters
336+
goodQueryParameters := urlQuery.Encode() // save the query parameters for a second attempt
337+
338+
// create bad query parameters in which the signature is missing
339+
urlQuery.Del("signature")
336340
testUrl.RawQuery = urlQuery.Encode()
337341

342+
// 1st attempt with bad query parameters in which the signature is missing
338343
resp, err = http.Post(testUrl.String(), "application/octet-stream", bytes.NewReader(body))
339344
if err != nil {
340345
t.Fatal(err)
341346
}
342347
defer resp.Body.Close()
343-
if resp.StatusCode != http.StatusOK {
344-
t.Fatalf("Update returned %s", resp.Status)
348+
expectedCode := http.StatusBadRequest
349+
if resp.StatusCode != expectedCode {
350+
t.Fatalf("Update returned %s. Expected %d", resp.Status, expectedCode)
351+
}
352+
353+
// 2nd attempt with bad query parameters in which the signature is of incorrect length
354+
urlQuery.Set("signature", "0xabcd") // should be 130 hex chars
355+
resp, err = http.Post(testUrl.String(), "application/octet-stream", bytes.NewReader(body))
356+
if err != nil {
357+
t.Fatal(err)
358+
}
359+
defer resp.Body.Close()
360+
expectedCode = http.StatusBadRequest
361+
if resp.StatusCode != expectedCode {
362+
t.Fatalf("Update returned %s. Expected %d", resp.Status, expectedCode)
363+
}
364+
365+
// 3rd attempt, with good query parameters:
366+
testUrl.RawQuery = goodQueryParameters
367+
resp, err = http.Post(testUrl.String(), "application/octet-stream", bytes.NewReader(body))
368+
if err != nil {
369+
t.Fatal(err)
370+
}
371+
defer resp.Body.Close()
372+
expectedCode = http.StatusOK
373+
if resp.StatusCode != expectedCode {
374+
t.Fatalf("Update returned %s. Expected %d", resp.Status, expectedCode)
345375
}
346376

347377
// get latest update through bzz-feed directly

0 commit comments

Comments
 (0)