-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow array parameters in JSONRPC methods
- Loading branch information
Showing
13 changed files
with
95 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package methods | ||
|
||
import ( | ||
"github.com/creachadair/jrpc2" | ||
"github.com/creachadair/jrpc2/handler" | ||
) | ||
|
||
func NewHandler(fn any) jrpc2.Handler { | ||
fi, err := handler.Check(fn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// explicitly disable array arguments since otherwise we cannot add | ||
// new method arguments without breaking backwards compatibility with clients | ||
fi.AllowArray(false) | ||
return fi.Wrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package methods | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/creachadair/jrpc2" | ||
"github.com/creachadair/jrpc2/handler" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type Request struct { | ||
Parameter string `json:"parameter"` | ||
} | ||
|
||
func TestNewHandlerNoArrayParameters(t *testing.T) { | ||
callCount := 0 | ||
f := func(ctx context.Context, request Request) error { | ||
callCount++ | ||
assert.Equal(t, "bar", request.Parameter) | ||
return nil | ||
} | ||
objectRequest := `{ | ||
"jsonrpc": "2.0", | ||
"id": 1, | ||
"method": "foo", | ||
"params": { "parameter": "bar" } | ||
}` | ||
requests, err := jrpc2.ParseRequests([]byte(objectRequest)) | ||
assert.NoError(t, err) | ||
assert.Len(t, requests, 1) | ||
finalObjectRequest := requests[0].ToRequest() | ||
|
||
// object parameters should work with our handlers | ||
customHandler := NewHandler(f) | ||
_, err = customHandler(context.Background(), finalObjectRequest) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, callCount) | ||
|
||
arrayRequest := `{ | ||
"jsonrpc": "2.0", | ||
"id": 1, | ||
"method": "foo", | ||
"params": ["bar"] | ||
}` | ||
requests, err = jrpc2.ParseRequests([]byte(arrayRequest)) | ||
assert.NoError(t, err) | ||
assert.Len(t, requests, 1) | ||
finalArrayRequest := requests[0].ToRequest() | ||
|
||
// Array requests should work with the normal handler, but not with our handlers | ||
stdHandler := handler.New(f) | ||
_, err = stdHandler(context.Background(), finalArrayRequest) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 2, callCount) | ||
|
||
_, err = customHandler(context.Background(), finalArrayRequest) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "invalid parameters") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters