Bug 39569

Summary: Validation of ETag by HttpHeaders.Add is too strict
Product: [Mono] Class Libraries Reporter: mdriley
Component: System.Net.HttpAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: 4.2.0 (C6)   
Target Milestone: Untriaged   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

Description mdriley 2016-03-13 12:44:36 UTC
Mono rejects ETag values that include escaped quotes (\"). These values are allowed by the RFC.

RFC2616 defines ETag as:
  ETag = "ETag" ":" entity-tag
where
  entity-tag = [ weak ] opaque-tag
  weak       = "W/"
  opaque-tag = quoted-string
and
  quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
  qdtext         = <any TEXT except <">>
  quoted-pair    = "\" CHAR

That means that something like
  ETag: "\"123\""
is valid.

---

This program works fine on Microsoft .NET 4.6.2. On Mono 4.2.1, the call to Add() throws FormatException.

// mcs test1.cs /reference:System.Net.Http
using System;
using System.Net.Http;

class Test1 {
  public static void Main(string[] args) {
    var m = new HttpResponseMessage();

    string v2 = "\"\\\"123\\\"\"";
    Console.WriteLine("ETag: {0}", v2);
    m.Headers.Add("ETag", v2);  // This throws on Mono.
  }
}

output:
Unhandled Exception:
System.FormatException: One of the identified items was in an invalid format.
  at System.Net.Http.Headers.HttpHeaders.AddInternal (System.String name, IEnumerable`1 values, System.Net.Http.Headers.HeaderInfo headerInfo, Boolean ignoreInvalid) <0x3bf0880 + 0x000c7> in <filename unknown>:0
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, IEnumerable`1 values) <0x3bf0468 + 0x0004b> in <filename unknown>:0
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, System.String value) <0x3bf0400 + 0x00053> in <filename unknown>:0
  at Test1.Main (System.String[] args) <0x3bb0f70 + 0x0006f> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: One of the identified items was in an invalid format.
  at System.Net.Http.Headers.HttpHeaders.AddInternal (System.String name, IEnumerable`1 values, System.Net.Http.Headers.HeaderInfo headerInfo, Boolean ignoreInvalid) <0x3bf0880 + 0x000c7> in <filename unknown>:0
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, IEnumerable`1 values) <0x3bf0468 + 0x0004b> in <filename unknown>:0
  at System.Net.Http.Headers.HttpHeaders.Add (System.String name, System.String value) <0x3bf0400 + 0x00053> in <filename unknown>:0
  at Test1.Main (System.String[] args) <0x3bb0f70 + 0x0006f> in <filename unknown>:0
Comment 1 Marek Safar 2016-03-14 11:39:40 UTC
Fixed in master and Mono 4.4