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

Insufficient handling of possible valid Accept headers #23

Open
GoogleCodeExporter opened this issue Feb 9, 2016 · 0 comments
Open

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1. Parsing of the Mime header misses some legitimate accept headers, namely 
quoted tokens which can contain commas, spaces, escaped quotes, and semi-colons.

I've written an implementation in ruby that handles all cases with some speed 
optimizations for the common case where quoted tokens do not occur. I'm just 
going to paste it here in-line instead of attempting to patch every version in 
this library.

ACCEPT_HEADER_REGEXP = /(\S+="(?:\\"|.)*?"[^\s,]*|\S+=[^\s,]+|[^\s;,]+[^\s,]*)/
ACCEPT_PARAMETER_REGEXP = /([^\s;=,]+)=("(?:\\"|.)*?"|[^;]+)/

def parse(header)
  # Only use the complex regexp if the header contains quoted tokens
  formats = if header.index('"')
    header.scan(ACCEPT_HEADER_REGEXP).map(&:first)
  else
    header.split(/,\s*/)
  end

  formats.map { |format|
    # Set default quality
    params = {'q' => 1.0}

    # Split into type and following parameters
    type, accept_params = format.split(";", 2)

    # Correct a standard wildcard error
    type = "*/*" if type == "*"

    if accept_params
      # Only use a complex regexp if the parameters contain quoted tokens
      accept_params = if accept_params.index('"')
        accept_params.scan(ACCEPT_PARAMETER_REGEXP)
      else
        accept_params.split(";").map { |a| a.split("=") }
      end

      accept_params.each { |(key, val)|
        val = if key == 'q'
          val.to_f
        elsif val[0] == '"' and val[-1] == '"'
          val[1..-2].gsub(/\\(.)/, "\\1")
        else
          val
        end
        params[key] = val
      }
    end
    [*type.split("/"), params]
  }
end

Original issue reported on code.google.com by [email protected] on 28 Mar 2014 at 3:34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant