Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where cronet_http sends incorrect HTTP request methods #1058

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkgs/cronet_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## 0.4.3-wip

## 0.4.2

* Require `package:jni >= 0.7.2` to remove a potential buffer overflow.
* Fix a bug where incorrect HTTP request methods were sent.
brianquinlan marked this conversation as resolved.
Show resolved Hide resolved

## 0.4.1

Expand Down
2 changes: 1 addition & 1 deletion pkgs/cronet_http/lib/src/cronet_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class CronetClient extends BaseClient {
jb.UrlRequestCallbackProxy.new1(
_urlRequestCallbacks(request, responseCompleter)),
_executor,
);
)..setHttpMethod(request.method.toJString());

var headers = request.headers;
if (body.isNotEmpty &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'src/redirect_tests.dart';
import 'src/request_body_streamed_tests.dart';
import 'src/request_body_tests.dart';
import 'src/request_headers_tests.dart';
import 'src/request_methods_tests.dart';
import 'src/response_body_streamed_test.dart';
import 'src/response_body_tests.dart';
import 'src/response_headers_tests.dart';
Expand All @@ -27,6 +28,7 @@ export 'src/redirect_tests.dart' show testRedirect;
export 'src/request_body_streamed_tests.dart' show testRequestBodyStreamed;
export 'src/request_body_tests.dart' show testRequestBody;
export 'src/request_headers_tests.dart' show testRequestHeaders;
export 'src/request_methods_tests.dart' show testRequestMethods;
export 'src/response_body_streamed_test.dart' show testResponseBodyStreamed;
export 'src/response_body_tests.dart' show testResponseBody;
export 'src/response_headers_tests.dart' show testResponseHeaders;
Expand Down Expand Up @@ -65,6 +67,7 @@ void testAll(Client Function() clientFactory,
testResponseBodyStreamed(clientFactory(),
canStreamResponseBody: canStreamResponseBody);
testRequestHeaders(clientFactory());
testRequestMethods(clientFactory());
testResponseHeaders(clientFactory());
testResponseStatusLine(clientFactory());
testRedirect(clientFactory(), redirectAlwaysAllowed: redirectAlwaysAllowed);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:stream_channel/stream_channel.dart';

/// Starts an HTTP server that captures the request headers.
///
/// Channel protocol:
/// On Startup:
/// - send port
/// On Request Received:
/// - send the received request method (e.g. GET) as a String
/// When Receive Anything:
/// - exit
void hybridMain(StreamChannel<Object?> channel) async {
late HttpServer server;

server = (await HttpServer.bind('localhost', 0))
..listen((request) async {
request.response.headers.set('Access-Control-Allow-Origin', '*');
if (request.method == 'OPTIONS') {
// Handle a CORS preflight request:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests
request.response.headers
..set('Access-Control-Allow-Methods', '*')
..set('Access-Control-Allow-Headers', '*');
} else {
channel.sink.add(request.method);
}
unawaited(request.response.close());
});

channel.sink.add(server.port);
await channel
.stream.first; // Any writes indicates that the server should exit.
unawaited(server.close());
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:async/async.dart';
import 'package:http/http.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';

import 'request_methods_server_vm.dart'
if (dart.library.html) 'request_methods_server_web.dart';

/// Tests that the [Client] correctly sends HTTP request methods
/// (e.g. GET, HEAD).
void testRequestMethods(Client client) async {
group('request methods', () {
late final String host;
late final StreamChannel<Object?> httpServerChannel;
late final StreamQueue<Object?> httpServerQueue;

setUpAll(() async {
httpServerChannel = await startServer();
httpServerQueue = StreamQueue(httpServerChannel.stream);
host = 'localhost:${await httpServerQueue.next}';
});
tearDownAll(() => httpServerChannel.sink.add(null));

test('custom method', () async {
await client.send(Request(
'CUSTOM',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test case-sensitive method too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that IOClient would not pass that test because it always uppercases the request method:
https://github.com/dart-lang/sdk/blob/0b10bfcde99def6c43d4a306c920ba1ba855d058/sdk/lib/_http/http_impl.dart#L2791

This seems like a bug according to RFC-7232: https://www.rfc-editor.org/rfc/rfc7231#section-4.1

I will add a case-insensitive test and then a flag to disable it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well, another bug, good spot. 👍

Uri.http(host, ''),
));
final method = await httpServerQueue.next as String;
expect('CUSTOM', method);
});

test('delete', () async {
await client.delete(Uri.http(host, ''));
final method = await httpServerQueue.next as String;
expect('DELETE', method);
});

test('get', () async {
await client.get(Uri.http(host, ''));
final method = await httpServerQueue.next as String;
expect('GET', method);
});
test('head', () async {
await client.head(Uri.http(host, ''));
final method = await httpServerQueue.next as String;
expect('HEAD', method);
});

test('patch', () async {
await client.patch(Uri.http(host, ''));
final method = await httpServerQueue.next as String;
expect('PATCH', method);
});

test('post', () async {
await client.post(Uri.http(host, ''));
final method = await httpServerQueue.next as String;
expect('POST', method);
});

test('put', () async {
await client.put(Uri.http(host, ''));
final method = await httpServerQueue.next as String;
expect('PUT', method);
});
});
}