From 665bf4d18e119712f6f265f2079ee22f3799dd98 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 1 Sep 2026 21:13:28 -0700 Subject: [PATCH] Fix potential crashing bug (but never seen in production) parsing Feedbin link paging info. Add tests. --- .../Sources/RSWeb/HTTPLinkPagingInfo.swift | 3 ++ .../RSWebTests/HTTPLinkPagingInfoTests.swift | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Modules/RSWeb/Tests/RSWebTests/HTTPLinkPagingInfoTests.swift diff --git a/Modules/RSWeb/Sources/RSWeb/HTTPLinkPagingInfo.swift b/Modules/RSWeb/Sources/RSWeb/HTTPLinkPagingInfo.swift index c91116722..ddab76d1f 100644 --- a/Modules/RSWeb/Sources/RSWeb/HTTPLinkPagingInfo.swift +++ b/Modules/RSWeb/Sources/RSWeb/HTTPLinkPagingInfo.swift @@ -27,6 +27,9 @@ public struct HTTPLinkPagingInfo { var dict: [String: String] = [:] for link in links { let components = link.components(separatedBy: "; ") + guard components.count > 1 else { + continue + } let page = components[0].trimmingCharacters(in: CharacterSet(charactersIn: " <>")) dict[components[1]] = page } diff --git a/Modules/RSWeb/Tests/RSWebTests/HTTPLinkPagingInfoTests.swift b/Modules/RSWeb/Tests/RSWebTests/HTTPLinkPagingInfoTests.swift new file mode 100644 index 000000000..4284a0036 --- /dev/null +++ b/Modules/RSWeb/Tests/RSWebTests/HTTPLinkPagingInfoTests.swift @@ -0,0 +1,50 @@ +// +// HTTPLinkPagingInfoTests.swift +// RSWebTests +// +// Created by Brent Simmons on 9/1/26. +// + +import Foundation +import Testing +import RSWeb + +struct HTTPLinkPagingInfoTests { + + @Test func parsesNextAndLast() throws { + let value = "; rel=\"next\", ; rel=\"last\"" + let info = try makeInfo(linkHeader: value) + #expect(info.nextPage == "https://api.feedbin.com/v2/entries.json?page=2") + #expect(info.lastPage == "https://api.feedbin.com/v2/entries.json?page=5") + } + + @Test func malformedSegmentDoesNotCrash() throws { + // A segment with no "; " separator used to trap on components[1]. + let info = try makeInfo(linkHeader: "") + #expect(info.nextPage == nil) + #expect(info.lastPage == nil) + } + + @Test func mixedValidAndMalformedSegments() throws { + let value = "; rel=\"next\", " + let info = try makeInfo(linkHeader: value) + #expect(info.nextPage == "https://api.feedbin.com/v2/entries.json?page=2") + #expect(info.lastPage == nil) + } + + @Test func noLinkHeader() throws { + let info = try makeInfo(linkHeader: nil) + #expect(info.nextPage == nil) + #expect(info.lastPage == nil) + } + + private func makeInfo(linkHeader: String?) throws -> HTTPLinkPagingInfo { + let url = try #require(URL(string: "https://api.feedbin.com/v2/entries.json")) + var headerFields: [String: String] = [:] + if let linkHeader { + headerFields[HTTPResponseHeader.link] = linkHeader + } + let response = try #require(HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: headerFields)) + return HTTPLinkPagingInfo(urlResponse: response) + } +}