Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Filter IPv6 addresses by default #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/com/includesecurity/safeurl/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class Configuration {
/** Do secure redirects, revalidate each redirect location first. */
var secureRedirects: Boolean = true

/** Support IPv6, disabled by default since the default blacklist relies on NAT for security */
var supportIPv6: Boolean = false

/** The maximum number of redirects SaveCurl will follow. */
var maxRedirects: Int = 20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,18 @@ object SafeURL {
* @param host hostname or IP address to resolve
* @return an array of IP addresses the hostname/IP resolves to
*/
private def resolve(host: String): Array[String] = {
InetAddress.getAllByName(host) map (_.getHostAddress)
private def resolve(host: String, cfg: Configuration = defaultConfiguration): Array[String] = {
var hosts = InetAddress.getAllByName(host)
if (!cfg.supportIPv6) {
val v4Hosts = hosts filter (_.isInstanceOf[Inet4Address])
if (v4Hosts.isEmpty && !hosts.isEmpty) {
// Treat IPv6-only results as if there was a lookup error,
// doesn't seem to be a way to force an IPv4-only lookup.
throw new UnknownHostException(host + ": Name or service not known");
}
hosts = v4Hosts
}
hosts map (_.getHostAddress)
}

/** Check if the given IP address lies within the subnet given in CIDR notation.
Expand Down Expand Up @@ -259,7 +269,7 @@ object SafeURL {
}

// Validate the IP
val ips = resolve(host)
val ips = resolve(host, cfg)
for (ip <- ips) {
// Note: Doing it this way means that when IP whitelisting is active,
// every IP a given hostname resolves to must be in the whitelist.
Expand Down