dart-sdk/tests/standalone_2/io/http_cookie_test.dart
William Hesse a9ad427ea2 Validate cookie values wrapped in double-quotes
Servers sometimes send headers with cookie values that are encapsulated in double-quotes. Dart should validate values surrounded with double-quotes instead of throwing a FormatException.

This addresses Issue #33327 directly. I applied the solution to this problem that was [solved in Go's code base](https://github.com/golang/go/blob/master/src/net/http/cookie.go#L369).

Closes #33765
https://github.com/dart-lang/sdk/pull/33765

GitOrigin-RevId: 99672dd07d1f938b1bae063f2e9d99d4c141f684
Change-Id: Ie95a064611b1aa15aea93f5c8d801ecfc7d996c4
Reviewed-on: https://dart-review.googlesource.com/63920
Reviewed-by: Zach Anderson <zra@google.com>
Commit-Queue: Zach Anderson <zra@google.com>
2018-08-10 16:14:15 +00:00

74 lines
2.2 KiB
Dart

// Copyright (c) 2013, 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:expect/expect.dart";
void testCookies() {
var cookies = [
{'abc': 'def'},
{'ABC': 'DEF'},
{'Abc': 'Def'},
{'Abc': 'Def', 'SID': 'sffFSDF4FsdfF56765'}
];
HttpServer.bind("127.0.0.1", 0).then((server) {
server.listen((HttpRequest request) {
// Collect the cookies in a map.
var cookiesMap = {};
request.cookies.forEach((c) => cookiesMap[c.name] = c.value);
int index = int.parse(request.uri.path.substring(1));
Expect.mapEquals(cookies[index], cookiesMap);
// Return the same cookies to the client.
cookiesMap.forEach((k, v) {
request.response.cookies.add(new Cookie(k, v));
});
request.response.close();
});
int count = 0;
HttpClient client = new HttpClient();
for (int i = 0; i < cookies.length; i++) {
client.get("127.0.0.1", server.port, "/$i").then((request) {
// Send the cookies to the server.
cookies[i].forEach((k, v) {
request.cookies.add(new Cookie(k, v));
});
return request.close();
}).then((response) {
// Expect the same cookies back.
var cookiesMap = {};
response.cookies.forEach((c) => cookiesMap[c.name] = c.value);
Expect.mapEquals(cookies[i], cookiesMap);
response.cookies.forEach((c) => Expect.isTrue(c.httpOnly));
response.listen((d) {}, onDone: () {
if (++count == cookies.length) {
client.close();
server.close();
}
});
}).catchError((e, trace) {
String msg = "Unexpected error $e";
if (trace != null) msg += "\nStackTrace: $trace";
Expect.fail(msg);
});
}
});
}
void testValidateCookieWithDoubleQuotes() {
try {
Cookie cookie = Cookie('key', '"double-quoted-value"');
} catch (e) {
Expect.fail("Unexpected error $e.\n"
"Unable to parse cookie with value in double-quote characters.");
}
}
void main() {
testCookies();
testValidateCookieWithDoubleQuotes();
}