!htt #19388 Chanring x-real-ip from raw to real modeled header
* Allows for parsing rules to be applied to vet the ip * Removed last cases of code where the String variant of apply on RemoteAddress is used
This commit is contained in:
parent
7850ad36a5
commit
6d3bb94d09
10 changed files with 84 additions and 10 deletions
|
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package akka.http.javadsl.model.headers;
|
||||
|
||||
/**
|
||||
* Model for the `X-Real-Ip` header.
|
||||
*/
|
||||
public abstract class XRealIp extends akka.http.scaladsl.model.HttpHeader {
|
||||
public abstract akka.http.javadsl.model.RemoteAddress address();
|
||||
|
||||
public static XRealIp create(akka.http.javadsl.model.RemoteAddress address) {
|
||||
return new akka.http.scaladsl.model.headers.X$minusReal$minusIp(((akka.http.scaladsl.model.RemoteAddress) address));
|
||||
}
|
||||
}
|
||||
|
|
@ -154,7 +154,8 @@ private[http] object HeaderParser {
|
|||
"upgrade",
|
||||
"user-agent",
|
||||
"www-authenticate",
|
||||
"x-forwarded-for")
|
||||
"x-forwarded-for",
|
||||
"x-real-ip")
|
||||
|
||||
abstract class Settings {
|
||||
def uriParsingMode: Uri.ParsingMode
|
||||
|
|
|
|||
|
|
@ -221,4 +221,9 @@ private[parser] trait SimpleHeaders { this: Parser with CommonRules with CommonA
|
|||
def addr = rule { (`ip-v4-address` | `ip-v6-address`) ~> (RemoteAddress(_)) | "unknown" ~ push(RemoteAddress.Unknown) }
|
||||
rule { oneOrMore(addr).separatedBy(listSep) ~ EOI ~> (`X-Forwarded-For`(_)) }
|
||||
}
|
||||
|
||||
def `x-real-ip` = rule {
|
||||
(`ip-v4-address` | `ip-v6-address`) ~ EOI ~> (b ⇒ `X-Real-Ip`(RemoteAddress(b)))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -910,3 +910,10 @@ final case class `X-Forwarded-For`(addresses: immutable.Seq[RemoteAddress]) exte
|
|||
/** Java API */
|
||||
def getAddresses: Iterable[jm.RemoteAddress] = addresses.asJava
|
||||
}
|
||||
|
||||
object `X-Real-Ip` extends ModeledCompanion[`X-Real-Ip`]
|
||||
final case class `X-Real-Ip`(address:RemoteAddress) extends jm.headers.XRealIp
|
||||
with RequestHeader {
|
||||
def renderValue[R <: Rendering](r: R): r.type = r ~~ address
|
||||
protected def companion = `X-Real-Ip`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -132,6 +132,10 @@ class HttpHeaderParserSpec extends WordSpec with Matchers with BeforeAndAfterAll
|
|||
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 an X-Real-Ip with a hostname as it's value as a RawHeader" in new TestSetup() {
|
||||
parseAndCache("X-Real-Ip: akka.io\r\nx")() shouldEqual RawHeader("x-real-ip", "akka.io")
|
||||
}
|
||||
|
||||
"parse and cache a raw header" in new TestSetup(primed = false) {
|
||||
insert("hello: bob", 'Hello)
|
||||
val (ixA, headerA) = parseLine("Fancy-Pants: foo\r\nx")
|
||||
|
|
|
|||
|
|
@ -581,6 +581,40 @@ class HttpHeaderSpec extends FreeSpec with Matchers {
|
|||
"1.2.3.4, akka.io\n ^")
|
||||
}
|
||||
|
||||
"X-Real-Ip" in {
|
||||
"X-Real-Ip: 1.2.3.4" =!= `X-Real-Ip`(remoteAddress("1.2.3.4"))
|
||||
"X-Real-Ip: 2001:db8:cafe:0:0:0:0:17" =!= `X-Real-Ip`(remoteAddress("2001:db8:cafe:0:0:0:0:17"))
|
||||
"X-Real-Ip: 1234:5678:9abc:def1:2345:6789:abcd:ef00" =!= `X-Real-Ip`(remoteAddress("1234:5678:9abc:def1:2345:6789:abcd:ef00"))
|
||||
"X-Real-Ip: 1234:567:9a:d:2:67:abc:ef00" =!= `X-Real-Ip`(remoteAddress("1234:567:9a:d:2:67:abc:ef00"))
|
||||
"X-Real-Ip: 2001:db8:85a3::8a2e:370:7334" =!=> "2001:db8:85a3:0:0:8a2e:370:7334"
|
||||
"X-Real-Ip: 1:2:3:4:5:6:7:8" =!= `X-Real-Ip`(remoteAddress("1:2:3:4:5:6:7:8"))
|
||||
"X-Real-Ip: ::2:3:4:5:6:7:8" =!=> "0:2:3:4:5:6:7:8"
|
||||
"X-Real-Ip: ::3:4:5:6:7:8" =!=> "0:0:3:4:5:6:7:8"
|
||||
"X-Real-Ip: ::4:5:6:7:8" =!=> "0:0:0:4:5:6:7:8"
|
||||
"X-Real-Ip: ::5:6:7:8" =!=> "0:0:0:0:5:6:7:8"
|
||||
"X-Real-Ip: ::6:7:8" =!=> "0:0:0:0:0:6:7:8"
|
||||
"X-Real-Ip: ::7:8" =!=> "0:0:0:0:0:0:7:8"
|
||||
"X-Real-Ip: ::8" =!=> "0:0:0:0:0:0:0:8"
|
||||
"X-Real-Ip: 1:2:3:4:5:6:7::" =!=> "1:2:3:4:5:6:7:0"
|
||||
"X-Real-Ip: 1:2:3:4:5:6::" =!=> "1:2:3:4:5:6:0:0"
|
||||
"X-Real-Ip: 1:2:3:4:5::" =!=> "1:2:3:4:5:0:0:0"
|
||||
"X-Real-Ip: 1:2:3:4::" =!=> "1:2:3:4:0:0:0:0"
|
||||
"X-Real-Ip: 1:2:3::" =!=> "1:2:3:0:0:0:0:0"
|
||||
"X-Real-Ip: 1:2::" =!=> "1:2:0:0:0:0:0:0"
|
||||
"X-Real-Ip: 1::" =!=> "1:0:0:0:0:0:0:0"
|
||||
"X-Real-Ip: 1::3:4:5:6:7:8" =!=> "1:0:3:4:5:6:7:8"
|
||||
"X-Real-Ip: 1:2::4:5:6:7:8" =!=> "1:2:0:4:5:6:7:8"
|
||||
"X-Real-Ip: 1:2:3::5:6:7:8" =!=> "1:2:3:0:5:6:7:8"
|
||||
"X-Real-Ip: 1:2:3:4::6:7:8" =!=> "1:2:3:4:0:6:7:8"
|
||||
"X-Real-Ip: 1:2:3:4:5::7:8" =!=> "1:2:3:4:5:0:7:8"
|
||||
"X-Real-Ip: 1:2:3:4:5:6::8" =!=> "1:2:3:4:5:6:0:8"
|
||||
"X-Real-Ip: ::" =!=> "0:0:0:0:0:0:0:0"
|
||||
"X-Real-Ip: akka.io" =!=
|
||||
ErrorInfo(
|
||||
"Illegal HTTP header 'X-Real-Ip': Invalid input 'k', expected HEXDIG, h8, ':', ch16o or cc (line 1, column 2)",
|
||||
"akka.io\n ^")
|
||||
}
|
||||
|
||||
"RawHeader" in {
|
||||
"X-Space-Ranger: no, this rock!" =!= RawHeader("X-Space-Ranger", "no, this rock!")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ package akka.http.scaladsl.model.headers
|
|||
|
||||
import akka.http.impl.util._
|
||||
import org.scalatest._
|
||||
|
||||
import java.net.InetAddress
|
||||
import akka.http.scaladsl.model._
|
||||
|
||||
class HeaderSpec extends FreeSpec with Matchers {
|
||||
|
|
@ -99,7 +99,8 @@ class HeaderSpec extends FreeSpec with Matchers {
|
|||
`Transfer-Encoding`(TransferEncodings.chunked),
|
||||
Upgrade(Vector(UpgradeProtocol("HTTP", Some("2.0")))),
|
||||
`User-Agent`("Akka HTTP Client 2.4"),
|
||||
`X-Forwarded-For`(RemoteAddress("192.168.0.1")))
|
||||
`X-Forwarded-For`(RemoteAddress(InetAddress.getByName("192.168.0.1"))),
|
||||
`X-Real-Ip`(RemoteAddress(InetAddress.getByName("192.168.1.1"))))
|
||||
|
||||
requestHeaders.foreach { header ⇒
|
||||
header shouldBe 'renderInRequests
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue