lemonlabsuk / scala-uri

Simple scala library for building and parsing URIs
Other
305 stars 34 forks source link

Parsing of empty 'data:,' URLs #486

Closed johnrevill closed 1 year ago

johnrevill commented 1 year ago

Describe the bug An exception is thrown when trying to parse a 'data:' URL that has empty data. This happens for either percent encoded or Base64 encoded forms of the data URL.

In RFC 2397 which describes the data URL scheme, it appears to allow URLs with empty data, such as "data:," or "data:;base64,".

It's a bit of an edge case, so I had a search around about this and came across a couple of things:

To Reproduce In version 4.0.2 (and I think also earlier versions from 2.x.x onwards), either of these lines:

DataUrl.parse("data:,")
DataUrl.parse("data:;base64,")

... will throw this exception:

io.lemonlabs.uri.parsing.UriParsingException: Invalid Data Url could not be parsed. Error(6,NonEmptyList(ExpectedFailureAt(6,)))
  at io.lemonlabs.uri.parsing.UrlParser.$anonfun$mapParseError$1(UrlParser.scala:381)
  at scala.util.Success.flatMap(Try.scala:258)
  at io.lemonlabs.uri.parsing.UrlParser.mapParseError(UrlParser.scala:379)
  at io.lemonlabs.uri.parsing.UrlParser.parseDataUrl(UrlParser.scala:408)
  at io.lemonlabs.uri.parsing.UrlParser$.parseDataUrl(UrlParser.scala:477)
  at io.lemonlabs.uri.DataUrl$.parseTry(Uri.scala:1308)
  at io.lemonlabs.uri.DataUrl$.parse(Uri.scala:1302)
  ... 40 elided

Also, I think as a consequence of this and how the parsing work under the hood, using Url.parse with an empty data URL doesn't produce a DataUrl as would be expected. e.g. the following two lines

Url.parse("data:,").getClass.getSimpleName
Url.parse("data:;base64,").getClass.getSimpleName

... give the unexpected result:

val res0: String = SimpleUrlWithoutAuthority

Whereas any other non-empty data URL gives the result:

val res0: String = DataUrl

This looked like it might just require a small tweak in the code so I tried my hand at a potential fix. I'll raise a PR for it shortly in case it's helpful and of course if you agree that it's even a bug.

johnrevill commented 1 year ago

Ok, just raised a PR at https://github.com/lemonlabsuk/scala-uri/pull/487 Hope that's all correct - it's been a long time since using github!