-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUMM-2153 Prevent Kronos logic from querying privte IPs
- Loading branch information
Showing
5 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
Tests/DatadogTests/Datadog/Kronos/KronosInternetAddressTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-2020 Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
@testable import Datadog | ||
|
||
class KronosInternetAddressTests: XCTestCase { | ||
func testIfIPv4AddressIsPrivarte() throws { | ||
let privateIPs: [KronosInternetAddress] = try (0..<50).flatMap { _ in | ||
return [ | ||
// random private IPs of class A: 10.0.0.0 — 10.255.255.255 | ||
try .mockIPv4([10, .mockRandom(), .mockRandom(), .mockRandom()]), | ||
// random private IPs of class B: 172.16.0.0 — 172.31.255.255 | ||
try .mockIPv4([172, .mockRandom(min: 16, max: 31), .mockRandom(), .mockRandom()]), | ||
// random private IPs of class C: 192.168.0.0 — 192.168.255.255 | ||
try .mockIPv4([192, 168, .mockRandom(), .mockRandom()]), | ||
] | ||
} | ||
let publicIPs: [KronosInternetAddress] = try (0..<50).flatMap { _ in | ||
return [ | ||
try .mockIPv4([.mockRandom(otherThan: [10, 172, 192]), .mockRandom(), .mockRandom(), .mockRandom()]), | ||
try .mockIPv4([172, .mockRandom(min: 0, max: 15), .mockRandom(), .mockRandom()]), | ||
try .mockIPv4([172, .mockRandom(min: 32, max: 255), .mockRandom(), .mockRandom()]), | ||
try .mockIPv4([192, .mockRandom(otherThan: [168]), .mockRandom(), .mockRandom()]), | ||
] | ||
} | ||
|
||
privateIPs.forEach { ip in | ||
XCTAssertTrue(ip.isPrivate, "\(ip.host ?? "nil") should be private IP") | ||
} | ||
publicIPs.forEach { ip in | ||
XCTAssertFalse(ip.isPrivate, "\(ip.host ?? "nil") should not be private IP") | ||
} | ||
} | ||
|
||
func testIfIPv6AddressIsPrivarte() throws { | ||
let privateIPs: [KronosInternetAddress] = try (0..<50).flatMap { _ in | ||
return [ | ||
// random private IP starting with `fd` prefix | ||
try .mockIPv6([0xfd] + (0..<15).map({ _ in .mockRandom() })) | ||
] | ||
} | ||
let publicIPs: [KronosInternetAddress] = try (0..<50).flatMap { _ in | ||
return [ | ||
// first byte is mocked to avoid having `fd` prefix | ||
try .mockIPv6([.mockRandom(min: 0xf0, otherThan: [0xfd])] + (0..<15).map({ _ in .mockRandom() })), | ||
try .mockIPv6([.mockRandom(max: 0xfc, otherThan: [0xf])] + (0..<15).map({ _ in .mockRandom() })), | ||
] | ||
} | ||
|
||
privateIPs.forEach { ip in | ||
XCTAssertTrue(ip.isPrivate, "\(ip.host ?? "nil") should be private IP") | ||
} | ||
publicIPs.forEach { ip in | ||
XCTAssertFalse(ip.isPrivate, "\(ip.host ?? "nil") should not be private IP") // false-positive on `fd9:18d2:ef6e:e095:ffb6:69d8:dd8:22b9` | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Mocks | ||
|
||
private extension KronosInternetAddress { | ||
static func mockIPv4(_ bytes: [UInt8]) throws -> KronosInternetAddress { | ||
precondition(bytes.count == 4, "Expected 4 bytes") | ||
let numbers = bytes.map { String($0) } | ||
let ipv4String = numbers.joined(separator: ".") // e.g. '192.168.1.1' | ||
let address: KronosInternetAddress? = .mockWith(ipv4String: ipv4String) | ||
return try XCTUnwrap(address, "\(ipv4String) is not a valid IPv4 string") | ||
} | ||
|
||
static func mockIPv6(_ bytes: [UInt8]) throws -> KronosInternetAddress { | ||
precondition(bytes.count == 16, "Expected 16 bytes") | ||
let groups: [String] = (0..<8).map { idx in | ||
let hexA = String(bytes[idx * 2], radix: 16, uppercase: false) | ||
let hexB = String(bytes[idx * 2 + 1], radix: 16, uppercase: false) | ||
return hexA + hexB | ||
} | ||
let ipv6String = groups.joined(separator: ":") // e.g. 'ab:ab:ab:ab:ab:ab:ab:ab' | ||
let address: KronosInternetAddress? = .mockWith(ipv6String: ipv6String) | ||
return try XCTUnwrap(address, "\(ipv6String) is not a valid IPv6 string") | ||
} | ||
|
||
static func mockWith(ipv4String: String) -> KronosInternetAddress? { | ||
var inaddr = in_addr() | ||
guard ipv4String.withCString({ inet_pton(AF_INET, $0, &inaddr) }) == 1 else { | ||
return nil // likely, not an IPv4 string | ||
} | ||
|
||
var addr = sockaddr_in() | ||
addr.sin_len = UInt8(MemoryLayout.size(ofValue: addr)) | ||
addr.sin_family = sa_family_t(AF_INET) | ||
addr.sin_addr = inaddr | ||
return .ipv4(addr) | ||
} | ||
|
||
static func mockWith(ipv6String: String) -> KronosInternetAddress? { | ||
var inaddr = in6_addr() | ||
guard ipv6String.withCString({ inet_pton(AF_INET6, $0, &inaddr) }) == 1 else { | ||
return nil // likely, not an IPv6 string | ||
} | ||
|
||
var addr = sockaddr_in6() | ||
addr.sin6_len = UInt8(MemoryLayout.size(ofValue: addr)) | ||
addr.sin6_family = sa_family_t(AF_INET6) | ||
addr.sin6_addr = inaddr | ||
return .ipv6(addr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters