google / guava

Google core libraries for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`MediaType.parse` doesn't like `\r`

boris-petrov opened this issue · comments

Guava 31.1-jre, JDK 18.

MediaType.parse("application/pdf; \rname=\"foo\rbar.pdf\"")

Leads to an exception:

Caused by: java.lang.IllegalArgumentException: Could not parse 'application/pdf; 
name="foo
bar.pdf"'
	at com.google.common.net.MediaType.parse(MediaType.java:1078)
        ...
Caused by: java.lang.IllegalStateException
	at com.google.common.base.Preconditions.checkState(Preconditions.java:486)
	at com.google.common.net.MediaType$Tokenizer.consumeToken(MediaType.java:1101)
	at com.google.common.net.MediaType.parse(MediaType.java:1066)

I understand that these \r symbols don't make much sense there but I've seem them in the wild (coming from Java Mail) so it would be nice to support them.

I don't think it should be the responsibility of the parser to sanitize inputs or trim white space.

But it handles correctly other kinds of whitespace, e.g. MediaType.parse("application/pdf; \n\n\nname=\"foo\n\nbar.pdf\"") works totally fine. Why not fix it also for \r?

From the class docs:

Note that this specifically ... has no support for header-specific considerations such as line folding and comments".

Also it looks like the linear-white-space was chosen specifically:

  /*
   * This matches the same characters as linear-white-space from RFC 822, but we make no effort to
   * enforce any particular rules with regards to line folding as stated in the class docs.
   */
  private static final CharMatcher LINEAR_WHITE_SPACE = CharMatcher.anyOf(" \t\r\n");

/cc @cpovirk in case he has any opinions.