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

Fix #569 - Manually encode Uri query parameters #596

Merged
merged 3 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions github4s/src/main/scala/github4s/http/Http4sSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,23 @@ object Http4sSyntax {
(self.headers.map(kv => Header(kv._1, kv._2)) ++
self.authHeader.map(kv => Header(kv._1, kv._2))).toList

def toUri(config: GithubConfig): Uri =
Uri.fromString(self.url).getOrElse(Uri.unsafeFromString(config.baseUrl)) =?
self.params.map(kv => (kv._1, List(kv._2)))
def toUri(config: GithubConfig): Uri = {
val queryString = self.params.toList
.map { case (k, v) =>
s"$k=$v"
}
.mkString("&")

//Adding query parameters normally has different encoding than normal Uris.
//To work around this, we create one verbatim from a manually encoded String.
//See: https://github.com/http4s/http4s/issues/4203
val verbatimQuery = Query.fromString(Uri.encode(queryString))

Uri
.fromString(self.url)
.getOrElse(Uri.unsafeFromString(config.baseUrl))
.copy(query = verbatimQuery)
}

}

Expand Down
43 changes: 43 additions & 0 deletions github4s/src/test/scala/github4s/http/Http4sSyntaxSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2016-2020 47 Degrees Open Source <https://www.47deg.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package github4s.http

import github4s.GithubConfig
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import Http4sSyntax._

class Http4sSyntaxSpec extends AnyFlatSpec with Matchers {

"toUri" should "manually encode query parameters to avoid problematic strings" in {
//All of the characters normally exempt from Uri.encode
//that also do not get encoded in query parameters
val knownBadCharacters = "!$&'()*+,;=:@"
val dummyConfig = GithubConfig("", "", "", Map.empty)

//Pulled from https://github.com/47degrees/github4s/issues/569
val goodQueryStr = "repos:47degrees/github4s+type:pr+label:enhancement+state:open"

val baseBuilder = RequestBuilder[Unit]("http://example.com")
val badUri = baseBuilder.withParams(Map("q" -> knownBadCharacters)).toUri(dummyConfig)
val goodUri = baseBuilder.withParams(Map("q" -> goodQueryStr)).toUri(dummyConfig)

//Neither query string should be modified after encoding
badUri.query.renderString shouldBe s"q=$knownBadCharacters"
goodUri.query.renderString shouldBe s"q=$goodQueryStr"
}
}