Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
Allow JSON-RPC id to be Null, Number, or String (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
hazim-j authored Nov 30, 2023
1 parent 376d4c5 commit 0d6192f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/jsonrpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"golang.org/x/text/language"
)

func jsonrpcError(c *gin.Context, code int, message string, data any, id *float64) {
func jsonrpcError(c *gin.Context, code int, message string, data any, id any) {
c.JSON(http.StatusOK, gin.H{
"jsonrpc": "2.0",
"error": gin.H{
Expand All @@ -27,6 +27,18 @@ func jsonrpcError(c *gin.Context, code int, message string, data any, id *float6
c.Abort()
}

// parseRequestId checks if the JSON-RPC request contains an id field that is either NULL, Number, or String.
func parseRequestId(data map[string]any) (any, bool) {
id, ok := data["id"]
_, isFloat64 := id.(float64)
_, isStr := id.(string)

if ok && (id == nil || isFloat64 || isStr) {
return id, true
}
return nil, false
}

// Controller returns a custom Gin middleware that handles incoming JSON-RPC requests via HTTP. It maps the
// RPC method name to struct methods on the given api. For example, if the RPC request has the method field
// set to "namespace_methodName" then the controller will make a call to api.Namespace_methodName with the
Expand Down Expand Up @@ -58,7 +70,7 @@ func Controller(api interface{}) gin.HandlerFunc {
return
}

id, ok := data["id"].(float64)
id, ok := parseRequestId(data)
if !ok {
jsonrpcError(c, -32600, "Invalid Request", "No or invalid 'id' in request", nil)
return
Expand Down

0 comments on commit 0d6192f

Please sign in to comment.