Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: sdk/lib/io/http_headers.dart

Issue 196723020: Add validation to construction of Cookie objects. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/standalone/io/http_headers_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.io; 5 part of dart.io;
6 6
7 class _HttpHeaders implements HttpHeaders { 7 class _HttpHeaders implements HttpHeaders {
8 final Map<String, List<String>> _headers; 8 final Map<String, List<String>> _headers;
9 final String protocolVersion; 9 final String protocolVersion;
10 10
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 skipWS(); 488 skipWS();
489 if (done()) return; 489 if (done()) return;
490 String name = parseName(); 490 String name = parseName();
491 skipWS(); 491 skipWS();
492 if (!expect("=")) { 492 if (!expect("=")) {
493 index = s.indexOf(';', index); 493 index = s.indexOf(';', index);
494 continue; 494 continue;
495 } 495 }
496 skipWS(); 496 skipWS();
497 String value = parseValue(); 497 String value = parseValue();
498 cookies.add(new _Cookie(name, value)); 498 try {
499 cookies.add(new _Cookie(name, value));
500 } catch (_) {
501 // Skip it, invalid cookie data.
502 }
499 skipWS(); 503 skipWS();
500 if (done()) return; 504 if (done()) return;
501 if (!expect(";")) { 505 if (!expect(";")) {
502 index = s.indexOf(';', index); 506 index = s.indexOf(';', index);
503 continue; 507 continue;
504 } 508 }
505 } 509 }
506 } 510 }
507 List<String> values = _headers[HttpHeaders.COOKIE]; 511 List<String> values = _headers[HttpHeaders.COOKIE];
508 if (values != null) { 512 if (values != null) {
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 class _Cookie implements Cookie { 715 class _Cookie implements Cookie {
712 String name; 716 String name;
713 String value; 717 String value;
714 DateTime expires; 718 DateTime expires;
715 int maxAge; 719 int maxAge;
716 String domain; 720 String domain;
717 String path; 721 String path;
718 bool httpOnly = false; 722 bool httpOnly = false;
719 bool secure = false; 723 bool secure = false;
720 724
721 _Cookie([this.name, this.value]); 725 _Cookie([this.name, this.value]) {
726 _validate();
727 }
722 728
723 _Cookie.fromSetCookieValue(String value) { 729 _Cookie.fromSetCookieValue(String value) {
724 // Parse the 'set-cookie' header value. 730 // Parse the 'set-cookie' header value.
725 _parseSetCookieValue(value); 731 _parseSetCookieValue(value);
726 } 732 }
727 733
728 // Parse a 'set-cookie' header value according to the rules in RFC 6265. 734 // Parse a 'set-cookie' header value according to the rules in RFC 6265.
729 void _parseSetCookieValue(String s) { 735 void _parseSetCookieValue(String s) {
730 int index = 0; 736 int index = 0;
731 737
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
799 if (!done()) index++; // Skip the ; character 805 if (!done()) index++; // Skip the ; character
800 } 806 }
801 } 807 }
802 808
803 name = parseName(); 809 name = parseName();
804 if (done() || name.length == 0) { 810 if (done() || name.length == 0) {
805 throw new HttpException("Failed to parse header value [$s]"); 811 throw new HttpException("Failed to parse header value [$s]");
806 } 812 }
807 index++; // Skip the = character. 813 index++; // Skip the = character.
808 value = parseValue(); 814 value = parseValue();
815 _validate();
809 if (done()) return; 816 if (done()) return;
810 index++; // Skip the ; character. 817 index++; // Skip the ; character.
811 parseAttributes(); 818 parseAttributes();
812 } 819 }
813 820
814 String toString() { 821 String toString() {
815 StringBuffer sb = new StringBuffer(); 822 StringBuffer sb = new StringBuffer();
816 sb..write(name)..write("=")..write(value); 823 sb..write(name)..write("=")..write(value);
817 if (expires != null) { 824 if (expires != null) {
818 sb..write("; Expires=")..write(HttpDate.format(expires)); 825 sb..write("; Expires=")..write(HttpDate.format(expires));
819 } 826 }
820 if (maxAge != null) { 827 if (maxAge != null) {
821 sb..write("; Max-Age=")..write(maxAge); 828 sb..write("; Max-Age=")..write(maxAge);
822 } 829 }
823 if (domain != null) { 830 if (domain != null) {
824 sb..write("; Domain=")..write(domain); 831 sb..write("; Domain=")..write(domain);
825 } 832 }
826 if (path != null) { 833 if (path != null) {
827 sb..write("; Path=")..write(path); 834 sb..write("; Path=")..write(path);
828 } 835 }
829 if (secure) sb.write("; Secure"); 836 if (secure) sb.write("; Secure");
830 if (httpOnly) sb.write("; HttpOnly"); 837 if (httpOnly) sb.write("; HttpOnly");
831 return sb.toString(); 838 return sb.toString();
832 } 839 }
840
841 void _validate() {
842 const SEPERATORS = const [
843 "(", ")", "<", ">", "@", ",", ";", ":", "\\",
844 '"', "/", "[", "]", "?", "=", "{", "}"];
845 for (int i = 0; i < name.length; i++) {
846 int codeUnit = name.codeUnits[i];
847 if (codeUnit <= 32 ||
848 codeUnit >= 127 ||
849 SEPERATORS.indexOf(name[i]) >= 0) {
850 throw new FormatException(
851 "Invalid character in cookie name, code unit: '$codeUnit'");
852 }
853 }
854 for (int i = 0; i < value.length; i++) {
855 int codeUnit = value.codeUnits[i];
856 if (!(codeUnit == 0x21 ||
857 (codeUnit >= 0x23 && codeUnit <= 0x2B) ||
858 (codeUnit >= 0x2D && codeUnit <= 0x3A) ||
859 (codeUnit >= 0x3C && codeUnit <= 0x5B) ||
860 (codeUnit >= 0x5D && codeUnit <= 0x7E))) {
861 throw new FormatException(
862 "Invalid character in cookie value, code unit: '$codeUnit'");
863 }
864 }
865 }
833 } 866 }
834 867
835 868
836 class _UnmodifiableMap<K, V> implements Map<K, V> { 869 class _UnmodifiableMap<K, V> implements Map<K, V> {
837 final Map _map; 870 final Map _map;
838 const _UnmodifiableMap(this._map); 871 const _UnmodifiableMap(this._map);
839 872
840 bool containsValue(Object value) => _map.containsValue(value); 873 bool containsValue(Object value) => _map.containsValue(value);
841 bool containsKey(Object key) => _map.containsKey(key); 874 bool containsKey(Object key) => _map.containsKey(key);
842 V operator [](Object key) => _map[key]; 875 V operator [](Object key) => _map[key];
(...skipping 12 matching lines...) Expand all
855 void clear() { 888 void clear() {
856 throw new UnsupportedError("Cannot modify an unmodifiable map"); 889 throw new UnsupportedError("Cannot modify an unmodifiable map");
857 } 890 }
858 void forEach(void f(K key, V value)) => _map.forEach(f); 891 void forEach(void f(K key, V value)) => _map.forEach(f);
859 Iterable<K> get keys => _map.keys; 892 Iterable<K> get keys => _map.keys;
860 Iterable<V> get values => _map.values; 893 Iterable<V> get values => _map.values;
861 int get length => _map.length; 894 int get length => _map.length;
862 bool get isEmpty => _map.isEmpty; 895 bool get isEmpty => _map.isEmpty;
863 bool get isNotEmpty => _map.isNotEmpty; 896 bool get isNotEmpty => _map.isNotEmpty;
864 } 897 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/http_headers_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698