From 005e73ff4983dd281e235df211d79e059cbbb6d6 Mon Sep 17 00:00:00 2001 From: Craig Munro Date: Sun, 8 Nov 2026 16:09:09 -0500 Subject: [PATCH] fix: adopt swift-cidr 0.9.0 and retain range prefix context Migrate to CIDRForm and Swift 6.2, align release tooling and notices, and retain each selected boundary address's input prefix in JSON. Reject equal, canonically equivalent, contained, and mixed-family network pairs before rendering any summary. Cover IPv4 and IPv6 endpoints, reversed inputs, and validation errors. Validation: 22 Debug tests, 22 Release tests, and 230 CLI checks passed. (cherry picked from commit 15aae0c3314b998580b9845662defc7e8566d527) --- .github/workflows/ci.yml | 6 +- .github/workflows/release.yml | 15 ++- Package.resolved | 6 +- Package.swift | 6 +- README.md | 39 ++++-- Sources/cidrwalk/cidrwalk.swift | 22 +++- THIRD_PARTY_NOTICES.txt | 6 +- Tests/cidrwalkTests/cidrwalkTests.swift | 162 +++++++++++++++++++++--- scripts/audit-release-content.sh | 2 +- scripts/check-release.sh | 8 +- 10 files changed, 218 insertions(+), 54 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4f4499..f814bb1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,11 +48,11 @@ jobs: run: swift build -c release --product cidrwalk linux-minimum: - name: Linux minimum toolchain (Swift 6.1) + name: Linux minimum toolchain (Swift 6.2) runs-on: ubuntu-24.04 - # swift:6.1-jammy multi-architecture manifest, resolved 2026-07-24. + # swift:6.2-jammy multi-architecture manifest, resolved 2026-11-08. container: >- - swift:6.1-jammy@sha256:12be34a341945adb64914405ed7b568fcb2d35edbd63a3755007e47dce59456e + swift:6.2-jammy@sha256:7889980d84f47447d109990a6763f3a7997acf2d1059bbd7c56f7e0af6243f73 timeout-minutes: 15 steps: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3834c68..1db005b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,10 +40,10 @@ jobs: - name: Verify reviewed dependency boundary run: | - grep -Fq '.upToNextMinor(from: "0.5.0")' Package.swift - grep -Fq '"version" : "0.5.0"' Package.resolved + grep -Fq '.upToNextMinor(from: "0.9.0")' Package.swift + grep -Fq '"version" : "0.9.0"' Package.resolved grep -Fq \ - '"revision" : "fd5116cef64544dc1f2bab9e93de4e1a7bb57dbb"' \ + '"revision" : "a7f661a313d2e9d97516aaeeb4fb0031a229fb02"' \ Package.resolved - name: Validate version and signed release tag @@ -100,6 +100,9 @@ jobs: needs: metadata runs-on: ${{ matrix.runner }} timeout-minutes: 20 + env: + # CHANGE: The macOS 15 runner default predates swift-cidr 0.9.0's Swift 6.2 floor. + DEVELOPER_DIR: /Applications/Xcode_26.3.app/Contents/Developer strategy: fail-fast: false matrix: @@ -201,11 +204,11 @@ jobs: EXPECTED_ARCH: ${{ matrix.uname_arch }} run: test "$(uname -m)" = "${EXPECTED_ARCH}" - - name: Build with Swift 6.1 on Ubuntu 22.04 + - name: Build with Swift 6.2 on Ubuntu 22.04 env: DOCKER_PLATFORM: ${{ matrix.docker_platform }} run: | - # swift:6.1-jammy multi-architecture manifest, resolved 2026-07-24. + # swift:6.2-jammy multi-architecture manifest, resolved 2026-11-08. docker run --rm \ --platform "${DOCKER_PLATFORM}" \ --user "$(id -u):$(id -g)" \ @@ -213,7 +216,7 @@ jobs: --env CLANG_MODULE_CACHE_PATH=/workspace/.build/clang-module-cache \ --volume "${PWD}:/workspace" \ --workdir /workspace \ - swift:6.1-jammy@sha256:12be34a341945adb64914405ed7b568fcb2d35edbd63a3755007e47dce59456e \ + swift:6.2-jammy@sha256:7889980d84f47447d109990a6763f3a7997acf2d1059bbd7c56f7e0af6243f73 \ bash -c \ 'mkdir -p "${XDG_CACHE_HOME}" "${CLANG_MODULE_CACHE_PATH}" && swift --version && diff --git a/Package.resolved b/Package.resolved index 8d15ab8..51fa7b5 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "82764448774e4712395f724e651840f1b4ee011d32baa83101aa989d60ddfcce", + "originHash" : "1dad1f357580aa446450e95de641d27b46e963abb07bbe88b24b990f6f16cf17", "pins" : [ { "identity" : "swift-argument-parser", @@ -24,8 +24,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/RouteObjects/swift-cidr.git", "state" : { - "revision" : "fd5116cef64544dc1f2bab9e93de4e1a7bb57dbb", - "version" : "0.5.0" + "revision" : "a7f661a313d2e9d97516aaeeb4fb0031a229fb02", + "version" : "0.9.0" } }, { diff --git a/Package.swift b/Package.swift index b5612f5..98590ea 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 6.1 +// swift-tools-version: 6.2 //===----------------------------------------------------------------------===// // @@ -24,10 +24,10 @@ let package = Package( .executable(name: "cidrwalk", targets: ["cidrwalk"]), ], dependencies: [ - // Keep this pre-1.0 dependency inside the reviewed 0.5 serialization line. + // Keep this pre-1.0 dependency inside the reviewed 0.9 compatibility line. .package( url: "https://github.com/RouteObjects/swift-cidr.git", - .upToNextMinor(from: "0.5.0") + .upToNextMinor(from: "0.9.0") ), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.7.0"), ], diff --git a/README.md b/README.md index a830613..ad7aab6 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,8 @@ The command is intentionally narrow and teaching-oriented: Both inputs must be from the same IP address family. Reversed input order is normalized at the CLI boundary before calling into `swift-cidr`. -Version 0.2 adopts `swift-cidr` 0.5's RFC 5952 IPv6 spelling for list, tree, -and JSON output. IPv6 values are compressed deterministically, for example -`2001:db8::1/128`, while aggregation semantics and the CLI grammar remain -unchanged. +IPv6 values use deterministic RFC 5952 compressed text in list, tree, and JSON +output, for example `2001:db8::1/128`. ## CIDR Aggregation @@ -33,8 +31,9 @@ when the range starts or ends inside a larger boundary. The `addresses` subcommand requires `/32` for IPv4 and `/128` for IPv6 because those prefix lengths identify exact host endpoints. The `networks` subcommand -accepts CIDR network prefixes and summarizes the full envelope that covers both -inputs, including adjacent, nested, or reversed networks. +accepts distinct CIDR network prefixes that do not contain one another and +summarizes the full envelope that covers both inputs. Adjacent or separated +networks are accepted in either order. ## Usage @@ -79,7 +78,11 @@ cidrwalk addresses 2001:db8::1/128 2001:db8::f/128 ### Network Prefixes Use `networks` when the inputs are CIDR network prefixes. The output covers both -complete input networks, including reversed, nested, or adjacent inputs. +complete input networks and any gap between them. The networks must use the same +address family, be unequal, and neither may contain the other. Equality is checked +after canonicalizing the inputs, so `10.0.0.1/24` and `10.0.0.0/24` are rejected +as the same network. Validation errors are written to stderr with a nonzero exit +status and no summary on stdout. ```bash cidrwalk networks 192.0.2.0/24 192.0.3.0/24 @@ -124,6 +127,22 @@ cidrwalk addresses 192.168.1.1/32 192.168.1.189/32 --output json } ``` +In network mode, `rangeStart` is the lowest first address and `rangeEnd` is the +highest last address across the two inputs. With swift-cidr 0.9, each JSON boundary +retains the prefix length of the input network that supplies it. These fields are +contextual address values; their prefix lengths may differ. + +| Network inputs | `rangeStart` | `rangeEnd` | +| --- | --- | --- | +| `10.0.0.0/24`, `10.0.2.0/25` | `10.0.0.0/24` | `10.0.2.127/25` | +| `2001:db8::/126`, `2001:db8::8/127` | `2001:db8::/126` | `2001:db8::9/127` | + +This intentionally changes the network-mode JSON boundary convention from earlier +releases using swift-cidr 0.5, which emitted host-length prefixes. The `inputs` +array preserves canonical networks in command-line order. Reversing accepted +inputs leaves the boundary values and summarized prefixes unchanged. Address-mode +JSON continues to use its required `/32` or `/128` host endpoints. + ### Tree Tree output is available with `--output tree`. Indentation is based on @@ -151,9 +170,9 @@ cd cidrwalk swift run cidrwalk --help ``` -The 0.2 package accepts `swift-cidr` versions in the `0.5.x` line -(`>= 0.5.0` and `< 0.6.0`). This protects the command's tested serialization -contract from an unreviewed pre-1.0 minor update. Building requires Swift 6.1 +The package accepts `swift-cidr` versions in the `0.9.x` line +(`>= 0.9.0` and `< 0.10.0`). This protects the command's tested serialization +contract from an unreviewed pre-1.0 minor update. Building requires Swift 6.2 or newer. ## Release Archives diff --git a/Sources/cidrwalk/cidrwalk.swift b/Sources/cidrwalk/cidrwalk.swift index e8b816b..e10b053 100644 --- a/Sources/cidrwalk/cidrwalk.swift +++ b/Sources/cidrwalk/cidrwalk.swift @@ -167,6 +167,7 @@ extension CIDRWalk { ) throws -> String { switch (firstNetwork, secondNetwork) { case (.v4(let first), .v4(let second)): + try validateNetworkPair(first, second) let rangeStart = first.first.address <= second.first.address ? first.first : second.first let rangeEnd = first.last.address >= second.last.address ? first.last : second.last return try render( @@ -178,6 +179,7 @@ extension CIDRWalk { output: output ) case (.v6(let first), .v6(let second)): + try validateNetworkPair(first, second) let rangeStart = first.first.address <= second.first.address ? first.first : second.first let rangeEnd = first.last.address >= second.last.address ? first.last : second.last return try render( @@ -193,6 +195,22 @@ extension CIDRWalk { } } + // Reject redundant inputs before rendering; retained boundaries keep their input prefixes. + private static func validateNetworkPair( + _ first: IPNetwork, + _ second: IPNetwork + ) throws { + guard first != second else { + throw ValidationError("first and second must be different networks (both are \(first)).") + } + if first.contains(subnet: second) { + throw ValidationError("\(first) contains \(second); first and second must not contain one another.") + } + if second.contains(subnet: first) { + throw ValidationError("\(second) contains \(first); first and second must not contain one another.") + } + } + static func printIfNeeded(_ lines: String) { guard !lines.isEmpty else { return } print(lines, terminator: lines.hasSuffix("\n") ? "" : "\n") @@ -207,7 +225,7 @@ extension CIDRWalk { output: OutputFormat ) throws -> String where Prefix: IPPrefix & Encodable, - Input: CIDR & Encodable, + Input: CIDRForm & Encodable, Input.Family == Prefix.Family { let prefixDescriptions = prefixes.map(\.description) @@ -262,7 +280,7 @@ enum SummaryMode: String, Encodable { private struct SummaryPayload: Encodable where Prefix: IPPrefix & Encodable, - Input: CIDR & Encodable, + Input: CIDRForm & Encodable, Input.Family == Prefix.Family { var mode: SummaryMode var family: String diff --git a/THIRD_PARTY_NOTICES.txt b/THIRD_PARTY_NOTICES.txt index c9e415e..95b442e 100644 --- a/THIRD_PARTY_NOTICES.txt +++ b/THIRD_PARTY_NOTICES.txt @@ -12,8 +12,8 @@ Swift Argument Parser 1.7.1 Source: https://github.com/apple/swift-argument-parser License: Apache License 2.0 with Runtime Library Exception -swift-cidr 0.5.0 (CIDR product) - Revision: fd5116cef64544dc1f2bab9e93de4e1a7bb57dbb +swift-cidr 0.9.0 (CIDR product) + Revision: a7f661a313d2e9d97516aaeeb4fb0031a229fb02 Source: https://github.com/RouteObjects/swift-cidr License: Apache License 2.0 @@ -55,7 +55,7 @@ Runtime Library Exception to the Apache License 2.0 The Apple packages identified above with the Runtime Library Exception use the following exception. Linux release binaries are built with the official -Swift 6.1 Ubuntu 22.04 toolchain and statically link portions of the Swift +Swift 6.2 Ubuntu 22.04 toolchain and statically link portions of the Swift runtime and standard libraries under the same license and exception. As an exception, if you use this Software to compile your source code and diff --git a/Tests/cidrwalkTests/cidrwalkTests.swift b/Tests/cidrwalkTests/cidrwalkTests.swift index 266d8cb..8645f33 100644 --- a/Tests/cidrwalkTests/cidrwalkTests.swift +++ b/Tests/cidrwalkTests/cidrwalkTests.swift @@ -11,6 +11,8 @@ // //===----------------------------------------------------------------------===// +import ArgumentParser +import Foundation import Testing @testable import cidrwalk @@ -179,18 +181,46 @@ func networksNormalizesReversedInputs() throws { #expect(output == "192.0.2.0/23") } -@Test("Networks command summarizes nested IPv4 networks by envelope") -func networksSummarizesNestedIPv4NetworksByEnvelope() throws { - let firstNetwork = try CIDRWalk.parseNetwork("10.0.0.0/24", label: "first") - let secondNetwork = try CIDRWalk.parseNetwork("10.0.0.0/25", label: "second") - - let output = try CIDRWalk.summarizeNetworks( - firstNetwork: firstNetwork, - secondNetwork: secondNetwork, - output: .list - ) +@Test( + "Networks command rejects containment in either input order", + arguments: [ + ("10.0.0.0/24", "10.0.0.0/25"), + ("10.0.0.0/24", "10.0.0.128/25"), + ("2001:db8::/126", "2001:db8::/127"), + ("2001:db8::/126", "2001:db8::2/127"), + ("0.0.0.0/0", "10.0.0.0/24"), + ("::/0", "2001:db8::/64"), + ] +) +func networksRejectsContainment(outer: String, inner: String) throws { + for (first, second) in [(outer, inner), (inner, outer)] { + try expectNetworkValidationError( + first: first, + second: second, + message: "\(outer) contains \(inner); first and second must not contain one another." + ) + } +} - #expect(output == "10.0.0.0/24") +@Test( + "Networks command rejects identical and canonically equivalent networks", + arguments: [ + ("10.0.0.0/24", "10.0.0.0/24", "10.0.0.0/24"), + ("2001:db8::/126", "2001:db8::/126", "2001:db8::/126"), + ("10.0.0.1/24", "10.0.0.254/24", "10.0.0.0/24"), + ("2001:db8::1/126", "2001:db8::3/126", "2001:db8::/126"), + ("0.0.0.0/0", "0.0.0.0/0", "0.0.0.0/0"), + ("::/0", "::/0", "::/0"), + ] +) +func networksRejectsEquivalentInputs(first: String, second: String, canonical: String) throws { + for (firstText, secondText) in [(first, second), (second, first)] { + try expectNetworkValidationError( + first: firstText, + second: secondText, + message: "first and second must be different networks (both are \(canonical))." + ) + } } @Test("Networks command summarizes adjacent IPv6 networks") @@ -245,14 +275,14 @@ func networksRejectsMalformedInput() { @Test("Networks command rejects mixed-family input") func networksRejectsMixedFamilies() throws { - let firstNetwork = try CIDRWalk.parseNetwork("192.0.2.0/24", label: "first") - let secondNetwork = try CIDRWalk.parseNetwork("2001:db8::/64", label: "second") - - #expect(throws: Error.self) { - try CIDRWalk.summarizeNetworks( - firstNetwork: firstNetwork, - secondNetwork: secondNetwork, - output: .list + for (first, second) in [ + ("192.0.2.0/24", "2001:db8::/64"), + ("2001:db8::/64", "192.0.2.0/24"), + ] { + try expectNetworkValidationError( + first: first, + second: second, + message: "first and second must use the same address family" ) } } @@ -295,3 +325,97 @@ func rootCommandRejectsPositionalInputsWithoutSubcommand() { _ = try CIDRWalk.parse(["192.0.2.1/32", "192.0.2.2/32"]) } } + +// Decode all JSON fields to verify source prefix context independently of rendering. +@Test( + "Network JSON retains source prefixes and orders boundaries independently of input order", + arguments: [ + ( + "IPv4", "10.0.0.0/24", "10.0.2.0/25", + "10.0.0.0/24", "10.0.2.127/25", + ["10.0.0.0/23", "10.0.2.0/25"] + ), + ( + "IPv6", "2001:db8::/126", "2001:db8::8/127", + "2001:db8::/126", "2001:db8::9/127", + ["2001:db8::/125", "2001:db8::8/127"] + ), + ( + "IPv4", "192.0.2.0/24", "192.0.3.0/24", + "192.0.2.0/24", "192.0.3.255/24", + ["192.0.2.0/23"] + ), + ( + "IPv6", "2001:db8::/127", "2001:db8::2/127", + "2001:db8::/127", "2001:db8::3/127", + ["2001:db8::/126"] + ), + ( + "IPv4", "192.0.2.0/32", "192.0.2.1/32", + "192.0.2.0/32", "192.0.2.1/32", + ["192.0.2.0/31"] + ), + ( + "IPv6", "2001:db8::/128", "2001:db8::1/128", + "2001:db8::/128", "2001:db8::1/128", + ["2001:db8::/127"] + ), + ] +) +func networkJSONPreservesContext( + family: String, + lowerNetwork: String, + upperNetwork: String, + expectedStart: String, + expectedEnd: String, + expectedPrefixes: [String] +) throws { + for inputs in [[lowerNetwork, upperNetwork], [upperNetwork, lowerNetwork]] { + let first = try CIDRWalk.parseNetwork(inputs[0], label: "first") + let second = try CIDRWalk.parseNetwork(inputs[1], label: "second") + let output = try CIDRWalk.summarizeNetworks( + firstNetwork: first, + secondNetwork: second, + output: .json + ) + let data = Data(output.utf8) + let payload = try JSONDecoder().decode(NetworkJSONPayload.self, from: data) + let object = try #require(JSONSerialization.jsonObject(with: data) as? [String: Any]) + + #expect(Set(object.keys) == Set(["mode", "family", "inputs", "rangeStart", "rangeEnd", "prefixes"])) + #expect(payload.mode == "networks") + #expect(payload.family == family) + #expect(payload.inputs == inputs) + #expect(payload.rangeStart == expectedStart) + #expect(payload.rangeEnd == expectedEnd) + #expect(payload.prefixes == expectedPrefixes) + } +} + +private struct NetworkJSONPayload: Decodable { + let mode: String + let family: String + let inputs: [String] + let rangeStart: String + let rangeEnd: String + let prefixes: [String] +} + +// Validate every output mode without changing process-global stdout or stderr. +private func expectNetworkValidationError(first: String, second: String, message: String) throws { + let firstNetwork = try CIDRWalk.parseNetwork(first, label: "first") + let secondNetwork = try CIDRWalk.parseNetwork(second, label: "second") + + for output in [OutputFormat.list, .tree, .json] { + do { + _ = try CIDRWalk.summarizeNetworks( + firstNetwork: firstNetwork, + secondNetwork: secondNetwork, + output: output + ) + Issue.record("Expected a validation error for \(first), \(second), and \(output)") + } catch let error as ValidationError { + #expect(error.message == message) + } + } +} diff --git a/scripts/audit-release-content.sh b/scripts/audit-release-content.sh index 512a0a2..4cb3f50 100755 --- a/scripts/audit-release-content.sh +++ b/scripts/audit-release-content.sh @@ -92,7 +92,7 @@ audit_dependency_notices() { done <<'EOF' swift-argument-parser|Swift Argument Parser|1.7.1|626b5b7b2f45e1b0b1c6f4a309296d1d21d7311b swift-atomics|Swift Atomics|1.3.0|b601256eab081c0f92f059e12818ac1d4f178ff7 -swift-cidr|swift-cidr|0.5.0|fd5116cef64544dc1f2bab9e93de4e1a7bb57dbb +swift-cidr|swift-cidr|0.9.0|a7f661a313d2e9d97516aaeeb4fb0031a229fb02 swift-collections|Swift Collections|1.5.1|fea17c02d767f46b23070fdfdacc28a03a39232a swift-nio|SwiftNIO|2.100.0|57c0a08a331aaea9f5d7a932ad94ef43be942a95 swift-system|Swift System|1.6.4|7c6ad0fc39d0763e0b699210e4124afd5041c5df diff --git a/scripts/check-release.sh b/scripts/check-release.sh index 4156299..711437e 100755 --- a/scripts/check-release.sh +++ b/scripts/check-release.sh @@ -30,10 +30,10 @@ done grep -Fq "static let version = \"${VERSION}\"" Sources/cidrwalk/cidrwalk.swift || fail "The executable version does not match ${VERSION}." -grep -Fq '.upToNextMinor(from: "0.5.0")' Package.swift || - fail "The swift-cidr requirement is not pinned to the 0.5 minor line." -grep -Fq '"revision" : "fd5116cef64544dc1f2bab9e93de4e1a7bb57dbb"' Package.resolved || - fail "Package.resolved does not contain the reviewed swift-cidr 0.5.0 revision." +grep -Fq '.upToNextMinor(from: "0.9.0")' Package.swift || + fail "The swift-cidr requirement is not pinned to the 0.9 minor line." +grep -Fq '"revision" : "a7f661a313d2e9d97516aaeeb4fb0031a229fb02"' Package.resolved || + fail "Package.resolved does not contain the reviewed swift-cidr 0.9.0 revision." while IFS= read -r swift_file; do grep -q 'SPDX-License-Identifier: Apache-2.0' "${swift_file}" ||