!htt #19388 Removing use of InetAddress.getByName
* Remove String apply methods for RemoteAddress (both scala and java) * Added unit test to show that hostnames in the address are treated as invalid by the parser * Added a unit test to show that invalid xff is added as a RawHeader instead of model class
This commit is contained in:
parent
396f4370e9
commit
128b7f87b9
5 changed files with 19 additions and 15 deletions
|
|
@ -26,9 +26,6 @@ public abstract class RemoteAddress {
|
||||||
public static RemoteAddress create(InetSocketAddress address) {
|
public static RemoteAddress create(InetSocketAddress address) {
|
||||||
return akka.http.scaladsl.model.RemoteAddress.apply(address);
|
return akka.http.scaladsl.model.RemoteAddress.apply(address);
|
||||||
}
|
}
|
||||||
public static RemoteAddress create(String address) {
|
|
||||||
return akka.http.scaladsl.model.RemoteAddress.apply(address);
|
|
||||||
}
|
|
||||||
public static RemoteAddress create(byte[] address) {
|
public static RemoteAddress create(byte[] address) {
|
||||||
return akka.http.scaladsl.model.RemoteAddress.apply(address);
|
return akka.http.scaladsl.model.RemoteAddress.apply(address);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,6 @@ object RemoteAddress {
|
||||||
def isUnknown = false
|
def isUnknown = false
|
||||||
}
|
}
|
||||||
|
|
||||||
def apply(s: String): RemoteAddress =
|
|
||||||
try IP(InetAddress.getByName(s)) catch { case _: UnknownHostException ⇒ Unknown }
|
|
||||||
|
|
||||||
def apply(a: InetAddress, port: Option[Int] = None): IP = IP(a, port)
|
def apply(a: InetAddress, port: Option[Int] = None): IP = IP(a, port)
|
||||||
|
|
||||||
def apply(a: InetSocketAddress): IP = IP(a.getAddress, Some(a.getPort))
|
def apply(a: InetSocketAddress): IP = IP(a.getAddress, Some(a.getPort))
|
||||||
|
|
|
||||||
|
|
@ -897,7 +897,6 @@ final case class `WWW-Authenticate`(challenges: immutable.Seq[HttpChallenge]) ex
|
||||||
|
|
||||||
// http://en.wikipedia.org/wiki/X-Forwarded-For
|
// http://en.wikipedia.org/wiki/X-Forwarded-For
|
||||||
object `X-Forwarded-For` extends ModeledCompanion[`X-Forwarded-For`] {
|
object `X-Forwarded-For` extends ModeledCompanion[`X-Forwarded-For`] {
|
||||||
def apply(first: String, more: String*): `X-Forwarded-For` = apply(RemoteAddress(first), more.map(RemoteAddress(_)): _*)
|
|
||||||
def apply(first: RemoteAddress, more: RemoteAddress*): `X-Forwarded-For` = apply(immutable.Seq(first +: more: _*))
|
def apply(first: RemoteAddress, more: RemoteAddress*): `X-Forwarded-For` = apply(immutable.Seq(first +: more: _*))
|
||||||
implicit val addressesRenderer = Renderer.defaultSeqRenderer[RemoteAddress] // cache
|
implicit val addressesRenderer = Renderer.defaultSeqRenderer[RemoteAddress] // cache
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,10 @@ class HttpHeaderParserSpec extends WordSpec with Matchers with BeforeAndAfterAll
|
||||||
parseAndCache("Origin: localhost:8080\r\nx")() shouldEqual RawHeader("origin", "localhost:8080")
|
parseAndCache("Origin: localhost:8080\r\nx")() shouldEqual RawHeader("origin", "localhost:8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"parse and cache an X-Forwarded-For with a hostname in it as a RawHeader" in new TestSetup() {
|
||||||
|
parseAndCache("X-Forwarded-For: 1.2.3.4, akka.io\r\nx")() shouldEqual RawHeader("x-forwarded-for", "1.2.3.4, akka.io")
|
||||||
|
}
|
||||||
|
|
||||||
"parse and cache a raw header" in new TestSetup(primed = false) {
|
"parse and cache a raw header" in new TestSetup(primed = false) {
|
||||||
insert("hello: bob", 'Hello)
|
insert("hello: bob", 'Hello)
|
||||||
val (ixA, headerA) = parseLine("Fancy-Pants: foo\r\nx")
|
val (ixA, headerA) = parseLine("Fancy-Pants: foo\r\nx")
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ import MediaRanges._
|
||||||
import HttpCharsets._
|
import HttpCharsets._
|
||||||
import HttpEncodings._
|
import HttpEncodings._
|
||||||
import HttpMethods._
|
import HttpMethods._
|
||||||
|
import java.net.InetAddress
|
||||||
|
|
||||||
class HttpHeaderSpec extends FreeSpec with Matchers {
|
class HttpHeaderSpec extends FreeSpec with Matchers {
|
||||||
val `application/vnd.spray` = MediaType.applicationBinary("vnd.spray", MediaType.Compressible)
|
val `application/vnd.spray` = MediaType.applicationBinary("vnd.spray", MediaType.Compressible)
|
||||||
|
|
@ -545,14 +546,14 @@ class HttpHeaderSpec extends FreeSpec with Matchers {
|
||||||
}
|
}
|
||||||
|
|
||||||
"X-Forwarded-For" in {
|
"X-Forwarded-For" in {
|
||||||
"X-Forwarded-For: 1.2.3.4" =!= `X-Forwarded-For`("1.2.3.4")
|
"X-Forwarded-For: 1.2.3.4" =!= `X-Forwarded-For`(remoteAddress("1.2.3.4"))
|
||||||
"X-Forwarded-For: 234.123.5.6, 8.8.8.8" =!= `X-Forwarded-For`("234.123.5.6", "8.8.8.8")
|
"X-Forwarded-For: 234.123.5.6, 8.8.8.8" =!= `X-Forwarded-For`(remoteAddress("234.123.5.6"), remoteAddress("8.8.8.8"))
|
||||||
"X-Forwarded-For: 1.2.3.4, unknown" =!= `X-Forwarded-For`(RemoteAddress("1.2.3.4"), RemoteAddress.Unknown)
|
"X-Forwarded-For: 1.2.3.4, unknown" =!= `X-Forwarded-For`(remoteAddress("1.2.3.4"), RemoteAddress.Unknown)
|
||||||
"X-Forwarded-For: 192.0.2.43, 2001:db8:cafe:0:0:0:0:17" =!= `X-Forwarded-For`("192.0.2.43", "2001:db8:cafe::17")
|
"X-Forwarded-For: 192.0.2.43, 2001:db8:cafe:0:0:0:0:17" =!= `X-Forwarded-For`(remoteAddress("192.0.2.43"), remoteAddress("2001:db8:cafe::17"))
|
||||||
"X-Forwarded-For: 1234:5678:9abc:def1:2345:6789:abcd:ef00" =!= `X-Forwarded-For`("1234:5678:9abc:def1:2345:6789:abcd:ef00")
|
"X-Forwarded-For: 1234:5678:9abc:def1:2345:6789:abcd:ef00" =!= `X-Forwarded-For`(remoteAddress("1234:5678:9abc:def1:2345:6789:abcd:ef00"))
|
||||||
"X-Forwarded-For: 1234:567:9a:d:2:67:abc:ef00" =!= `X-Forwarded-For`("1234:567:9a:d:2:67:abc:ef00")
|
"X-Forwarded-For: 1234:567:9a:d:2:67:abc:ef00" =!= `X-Forwarded-For`(remoteAddress("1234:567:9a:d:2:67:abc:ef00"))
|
||||||
"X-Forwarded-For: 2001:db8:85a3::8a2e:370:7334" =!=> "2001:db8:85a3:0:0:8a2e:370:7334"
|
"X-Forwarded-For: 2001:db8:85a3::8a2e:370:7334" =!=> "2001:db8:85a3:0:0:8a2e:370:7334"
|
||||||
"X-Forwarded-For: 1:2:3:4:5:6:7:8" =!= `X-Forwarded-For`("1:2:3:4:5:6:7:8")
|
"X-Forwarded-For: 1:2:3:4:5:6:7:8" =!= `X-Forwarded-For`(remoteAddress("1:2:3:4:5:6:7:8"))
|
||||||
"X-Forwarded-For: ::2:3:4:5:6:7:8" =!=> "0:2:3:4:5:6:7:8"
|
"X-Forwarded-For: ::2:3:4:5:6:7:8" =!=> "0:2:3:4:5:6:7:8"
|
||||||
"X-Forwarded-For: ::3:4:5:6:7:8" =!=> "0:0:3:4:5:6:7:8"
|
"X-Forwarded-For: ::3:4:5:6:7:8" =!=> "0:0:3:4:5:6:7:8"
|
||||||
"X-Forwarded-For: ::4:5:6:7:8" =!=> "0:0:0:4:5:6:7:8"
|
"X-Forwarded-For: ::4:5:6:7:8" =!=> "0:0:0:4:5:6:7:8"
|
||||||
|
|
@ -574,6 +575,10 @@ class HttpHeaderSpec extends FreeSpec with Matchers {
|
||||||
"X-Forwarded-For: 1:2:3:4:5::7:8" =!=> "1:2:3:4:5:0:7:8"
|
"X-Forwarded-For: 1:2:3:4:5::7:8" =!=> "1:2:3:4:5:0:7:8"
|
||||||
"X-Forwarded-For: 1:2:3:4:5:6::8" =!=> "1:2:3:4:5:6:0:8"
|
"X-Forwarded-For: 1:2:3:4:5:6::8" =!=> "1:2:3:4:5:6:0:8"
|
||||||
"X-Forwarded-For: ::" =!=> "0:0:0:0:0:0:0:0"
|
"X-Forwarded-For: ::" =!=> "0:0:0:0:0:0:0:0"
|
||||||
|
"X-Forwarded-For: 1.2.3.4, akka.io" =!=
|
||||||
|
ErrorInfo(
|
||||||
|
"Illegal HTTP header 'X-Forwarded-For': Invalid input 'k', expected HEXDIG, h8, ':', ch16o or cc (line 1, column 11)",
|
||||||
|
"1.2.3.4, akka.io\n ^")
|
||||||
}
|
}
|
||||||
|
|
||||||
"RawHeader" in {
|
"RawHeader" in {
|
||||||
|
|
@ -663,4 +668,6 @@ class HttpHeaderSpec extends FreeSpec with Matchers {
|
||||||
val info = result.errors.head
|
val info = result.errors.head
|
||||||
fail(s"Input `${header.header}` failed to parse:\n${info.summary}\n${info.detail}")
|
fail(s"Input `${header.header}` failed to parse:\n${info.summary}\n${info.detail}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def remoteAddress(ip: String) = RemoteAddress(InetAddress.getByName(ip))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue