Skip to content

Commit 2337f99

Browse files
authored
[json_rpc_2] Add strictProtocolChecks parameter to Client (dart-lang#2555)
Closes dart-lang#2422 `Server` and `Peer` already accept a `strictProtocolChecks` flag that allows requests which don't include the `jsonrpc` field. `Client` had no equivalent, so there was no way to interoperate with a peer whose responses omit that field. Add a `strictProtocolChecks` parameter to `Client` and `Client.withoutJson`, defaulting to `true` to match `Server`. When it is `false`, a response missing the `jsonrpc` key is accepted, but a response with a `jsonrpc` key set to anything other than `"2.0"` is still rejected. Pass the flag through `Peer.withoutJson` to the internal `Client`, so `Peer(strictProtocolChecks: false)` relaxes response validation on the client side as well as request validation on the server side.
1 parent c65c69e commit 2337f99

5 files changed

Lines changed: 73 additions & 15 deletions

File tree

‎pkgs/json_rpc_2/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* Add `Parameters.isNotification`, to tell a notification apart from a request
55
with a `null` ID.
66
* A class that implements `Parameters` now needs `id` and `isNotification`.
7+
* Add `strictProtocolChecks` parameter to `Client` and `Client.withoutJson`,
8+
matching the existing parameter on `Server`. When `false`, the client
9+
accepts responses that omit the `jsonrpc` parameter.
710

811
## 4.1.0
912

‎pkgs/json_rpc_2/lib/src/client.dart‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ class Client {
4747
/// endpoint closes the connection.
4848
bool get isClosed => _done.isCompleted;
4949

50+
/// Whether to strictly enforce the JSON-RPC 2.0 specification for received
51+
/// responses.
52+
///
53+
/// If `false`, this [Client] will accept some responses which are not
54+
/// conformant with the JSON-RPC 2.0 specification. In particular, responses
55+
/// missing the `jsonrpc` parameter will be accepted.
56+
final bool strictProtocolChecks;
57+
5058
/// Creates a [Client] that communicates over [channel].
5159
///
5260
/// Note that the client won't begin listening to [channel] until
@@ -55,10 +63,17 @@ class Client {
5563
/// If [idGenerator] is passed, it will be called to generate an ID for each
5664
/// request. Defaults to an auto-incrementing `int`. The value returned must
5765
/// be either an `int` or `String`.
58-
Client(StreamChannel<String> channel, {Object Function()? idGenerator})
66+
///
67+
/// If [strictProtocolChecks] is false, this [Client] will accept some
68+
/// responses which are not conformant with the JSON-RPC 2.0 specification.
69+
/// In particular, responses missing the `jsonrpc` parameter will be
70+
/// accepted.
71+
Client(StreamChannel<String> channel,
72+
{Object Function()? idGenerator, bool strictProtocolChecks = true})
5973
: this.withoutJson(
6074
jsonDocument.bind(channel).transformStream(ignoreFormatExceptions),
61-
idGenerator: idGenerator);
75+
idGenerator: idGenerator,
76+
strictProtocolChecks: strictProtocolChecks);
6277

6378
/// Creates a [Client] that communicates using decoded messages over
6479
/// [_channel].
@@ -72,7 +87,13 @@ class Client {
7287
/// If [_idGenerator] is passed, it will be called to generate an ID for each
7388
/// request. Defaults to an auto-incrementing `int`. The value returned must
7489
/// be either an `int` or `String`.
75-
Client.withoutJson(this._channel, {Object Function()? idGenerator})
90+
///
91+
/// If [strictProtocolChecks] is false, this [Client] will accept some
92+
/// responses which are not conformant with the JSON-RPC 2.0 specification.
93+
/// In particular, responses missing the `jsonrpc` parameter will be
94+
/// accepted.
95+
Client.withoutJson(this._channel,
96+
{Object Function()? idGenerator, this.strictProtocolChecks = true})
7697
: _idGenerator = idGenerator ?? _createIncrementingIdGenerator() {
7798
done.whenComplete(() {
7899
for (var request in _pendingRequests.values) {
@@ -227,7 +248,11 @@ class Client {
227248
/// Determines whether the server's response is valid per the spec.
228249
bool _isResponseValid(Object? response) {
229250
if (response is! Map) return false;
230-
if (response['jsonrpc'] != '2.0') return false;
251+
if (response.containsKey('jsonrpc')) {
252+
if (response['jsonrpc'] != '2.0') return false;
253+
} else if (strictProtocolChecks) {
254+
return false;
255+
}
231256
var id = response['id'];
232257
if (!_pendingRequests.containsKey(id)) return false;
233258
if (response.containsKey('result')) return true;

‎pkgs/json_rpc_2/lib/src/peer.dart‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ class Peer implements Client, Server {
5555
/// Unhandled exceptions in callbacks will be forwarded to [onUnhandledError].
5656
/// If this is not provided, unhandled exceptions will be swallowed.
5757
///
58-
/// If [strictProtocolChecks] is false, the underlying [Server] will accept
59-
/// some requests which are not conformant with the JSON-RPC 2.0
60-
/// specification. In particular, requests missing the `jsonrpc` parameter
61-
/// will be accepted.
58+
/// If [strictProtocolChecks] is false, the underlying [Server] and [Client]
59+
/// will accept some requests and responses which are not conformant with
60+
/// the JSON-RPC 2.0 specification. In particular, requests and responses
61+
/// missing the `jsonrpc` parameter will be accepted.
6262
///
6363
/// If [idGenerator] is passed, it will be called to generate an ID for each
6464
/// request. Defaults to an auto-incrementing `int`. The value returned must
@@ -85,10 +85,10 @@ class Peer implements Client, Server {
8585
/// Unhandled exceptions in callbacks will be forwarded to [onUnhandledError].
8686
/// If this is not provided, unhandled exceptions will be swallowed.
8787
///
88-
/// If [strictProtocolChecks] is false, the underlying [Server] will accept
89-
/// some requests which are not conformant with the JSON-RPC 2.0
90-
/// specification. In particular, requests missing the `jsonrpc` parameter
91-
/// will be accepted.
88+
/// If [strictProtocolChecks] is false, the underlying [Server] and [Client]
89+
/// will accept some requests and responses which are not conformant with
90+
/// the JSON-RPC 2.0 specification. In particular, requests and responses
91+
/// missing the `jsonrpc` parameter will be accepted.
9292
///
9393
/// If [idGenerator] is passed, it will be called to generate an ID for each
9494
/// request. Defaults to an auto-incrementing `int`. The value returned must
@@ -106,7 +106,8 @@ class Peer implements Client, Server {
106106
_clientIncomingForwarder.stream,
107107
_channel.sink,
108108
),
109-
idGenerator: idGenerator);
109+
idGenerator: idGenerator,
110+
strictProtocolChecks: strictProtocolChecks);
110111
}
111112

112113
// Client methods.

‎pkgs/json_rpc_2/test/client/client_test.dart‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,33 @@ void main() {
206206
});
207207
});
208208

209+
group('strict protocol checks disabled', () {
210+
setUp(() => controller = ClientController(strictProtocolChecks: false));
211+
212+
test('accepts a response with no jsonrpc key', () {
213+
controller.expectRequest((request) {
214+
return {'result': 'bar', 'id': request['id']};
215+
});
216+
217+
expect(controller.client.sendRequest('foo'), completion(equals('bar')));
218+
});
219+
220+
test('still rejects a response with the wrong jsonrpc version', () {
221+
controller.expectRequest((request) {
222+
controller.sendResponse({
223+
'jsonrpc': 'wrong version',
224+
'result': 'wrong',
225+
'id': request['id']
226+
});
227+
228+
return pumpEventQueue().then(
229+
(_) => {'jsonrpc': '2.0', 'result': 'right', 'id': request['id']});
230+
});
231+
232+
expect(controller.client.sendRequest('foo'), completion(equals('right')));
233+
});
234+
});
235+
209236
test('with custom String ids', () {
210237
var id = 0;
211238
controller = ClientController(idGenerator: () => 'ID-${id++}');

‎pkgs/json_rpc_2/test/client/utils.dart‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ class ClientController {
2020
/// The client.
2121
late final json_rpc.Client client;
2222

23-
ClientController({Object Function()? idGenerator}) {
23+
ClientController(
24+
{Object Function()? idGenerator, bool strictProtocolChecks = true}) {
2425
client = json_rpc.Client(
2526
StreamChannel(_responseController.stream, _requestController.sink),
26-
idGenerator: idGenerator);
27+
idGenerator: idGenerator,
28+
strictProtocolChecks: strictProtocolChecks);
2729
client.listen();
2830
}
2931

0 commit comments

Comments
 (0)