json-rpc: add validation for json-rpc request

This commit is contained in:
yosoyubik 2021-04-29 11:23:50 +02:00
parent d8a24d7d6b
commit 843fe4ce38
2 changed files with 31 additions and 32 deletions

View File

@ -70,43 +70,15 @@
:: TODO: method not supported
::
not-found:gen
?~ json-rpc=(validate-json-rpc body.req)
?~ rpc-request=(validate-request:json-rpc body.req parse-method)
:: TODO: malformed request
::
not-found:gen
(process-rpc-request:do u.json-rpc)
:: TODO: validate that format is e.g. 'get-point'
:: TODO: maybe replace with getPoint and translate to %term
(process-rpc-request:do u.rpc-request)
:: TODO: validate that format is e.g. 'getPoint'
:: TODO: maybe use getPoint and translate to %get-point
::
++ parse-method |=(t=@t t)
:: TODO: move to library
::
++ validate-json-rpc
|= body=(unit octs)
^- (unit request:rpc)
?~ body ~
?~ jon=(de-json:html q.u.body) ~
:: ignores non-object responses
::
:: ?. ?=([%o *] json) ~|([%format-not-valid json] !!)
?. ?=([%o *] u.jon) ~
%- some
%. u.jon
=, dejs:format
%- ot
:~ ['id' no]
['jsonrpc' so]
['method' (cu parse-method so)]
::
:- 'params'
|= =json
^- request-params:rpc
?: =(%a -.json)
[%list ((ar same) json)]
?. =(%o -.json)
!!
[%object ~(tap by ((om same) json))]
==
--
--
::

View File

@ -50,4 +50,31 @@
['message' s+message.response]
==
==
::
++ validate-request
|= [body=(unit octs) parse-method=$-(@t term)]
^- (unit request)
?~ body ~
?~ jon=(de-json:html q.u.body) ~
:: ignores non-object responses
::
:: ?. ?=([%o *] json) ~|([%format-not-valid json] !!)
?. ?=([%o *] u.jon) ~
%- some
%. u.jon
=, dejs:format
%- ot
:~ ['id' no]
['jsonrpc' so]
['method' (cu parse-method so)]
::
:- 'params'
|= =json
^- request-params
?: =(%a -.json)
[%list ((ar same) json)]
?. =(%o -.json)
!!
[%object ~(tap by ((om same) json))]
==
--