From 7da139be4d32b7f170d83fd9d8622fbeb897cb81 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 29 Mar 2022 22:37:39 -0400 Subject: [PATCH] Redact request paths in error reporter --- Pachyderm/Client.swift | 8 +-- Pachyderm/Model/Timeline.swift | 2 +- Pachyderm/Request/Endpoint.swift | 62 +++++++++++++++++++++ Pachyderm/Request/Request.swift | 6 +- Tusker.xcodeproj/project.pbxproj | 4 ++ Tusker/Views/Toast/ToastConfiguration.swift | 2 +- 6 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 Pachyderm/Request/Endpoint.swift diff --git a/Pachyderm/Client.swift b/Pachyderm/Client.swift index 9baf91d9..93464f33 100644 --- a/Pachyderm/Client.swift +++ b/Pachyderm/Client.swift @@ -106,7 +106,7 @@ public class Client { func createURLRequest(request: Request) -> URLRequest? { guard var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: true) else { return nil } - components.path = request.path + components.path = request.endpoint.path components.queryItems = request.queryParameters.isEmpty ? nil : request.queryParameters.queryItems guard let url = components.url else { return nil } var urlRequest = URLRequest(url: url, timeoutInterval: timeoutInterval) @@ -166,7 +166,7 @@ public class Client { if let url = wellKnown.links.first(where: { $0.rel == "http://nodeinfo.diaspora.software/ns/schema/2.0" }), let components = URLComponents(string: url.href), components.host == self.baseURL.host { - let nodeInfo = Request(method: .get, path: components.path) + let nodeInfo = Request(method: .get, path: Endpoint(stringLiteral: components.path)) self.run(nodeInfo, completion: completion) } } @@ -397,12 +397,12 @@ public class Client { extension Client { public struct Error: LocalizedError { public let requestMethod: Method - public let requestPath: String + public let requestEndpoint: Endpoint public let type: ErrorType init(request: Request, type: ErrorType) { self.requestMethod = request.method - self.requestPath = request.path + self.requestEndpoint = request.endpoint self.type = type } diff --git a/Pachyderm/Model/Timeline.swift b/Pachyderm/Model/Timeline.swift index a515701a..56cc6060 100644 --- a/Pachyderm/Model/Timeline.swift +++ b/Pachyderm/Model/Timeline.swift @@ -17,7 +17,7 @@ public enum Timeline { } extension Timeline { - var endpoint: String { + var endpoint: Endpoint { switch self { case .home: return "/api/v1/timelines/home" diff --git a/Pachyderm/Request/Endpoint.swift b/Pachyderm/Request/Endpoint.swift new file mode 100644 index 00000000..1ae874bf --- /dev/null +++ b/Pachyderm/Request/Endpoint.swift @@ -0,0 +1,62 @@ +// +// Endpoint.swift +// Pachyderm +// +// Created by Shadowfacts on 3/29/22. +// Copyright © 2022 Shadowfacts. All rights reserved. +// + +import Foundation + +public struct Endpoint: ExpressibleByStringInterpolation, CustomStringConvertible { + let components: [Component] + + public init(stringLiteral value: StringLiteralType) { + self.components = [.literal(value)] + } + + public init(stringInterpolation: StringInterpolation) { + self.components = stringInterpolation.components + } + + var path: String { + components.map { + switch $0 { + case .literal(let s), .interpolated(let s): + return s + } + }.joined(separator: "") + } + + public var description: String { + components.map { + switch $0 { + case .literal(let s): + return s + case .interpolated(_): + return "" + } + }.joined(separator: "") + } + + public struct StringInterpolation: StringInterpolationProtocol { + var components = [Component]() + + public init(literalCapacity: Int, interpolationCount: Int) { + } + + public mutating func appendLiteral(_ literal: StringLiteralType) { + components.append(.literal(literal)) + } + + public mutating func appendInterpolation(_ value: String) { + components.append(.interpolated(value)) + } + } + + enum Component { + case literal(String) + case interpolated(String) + } + +} diff --git a/Pachyderm/Request/Request.swift b/Pachyderm/Request/Request.swift index fe2141e7..947d1cca 100644 --- a/Pachyderm/Request/Request.swift +++ b/Pachyderm/Request/Request.swift @@ -10,13 +10,13 @@ import Foundation public struct Request { let method: Method - let path: String + let endpoint: Endpoint let body: Body var queryParameters: [Parameter] - init(method: Method, path: String, body: Body = EmptyBody(), queryParameters: [Parameter] = []) { + init(method: Method, path: Endpoint, body: Body = EmptyBody(), queryParameters: [Parameter] = []) { self.method = method - self.path = path + self.endpoint = path self.body = body self.queryParameters = queryParameters } diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 1594295b..2e780440 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -70,6 +70,7 @@ D6109A0F21459B6900432DC2 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A0E21459B6900432DC2 /* Pagination.swift */; }; D6109A11214607D500432DC2 /* Timeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A10214607D500432DC2 /* Timeline.swift */; }; D6114E0927F3EA3D0080E273 /* CrashReporterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6114E0827F3EA3D0080E273 /* CrashReporterViewController.swift */; }; + D6114E0B27F3F6EA0080E273 /* Endpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6114E0A27F3F6EA0080E273 /* Endpoint.swift */; }; D611C2CF232DC61100C86A49 /* HashtagTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D611C2CD232DC61100C86A49 /* HashtagTableViewCell.swift */; }; D611C2D0232DC61100C86A49 /* HashtagTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D611C2CE232DC61100C86A49 /* HashtagTableViewCell.xib */; }; D61AC1D3232E928600C54D2D /* InstanceSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61AC1D2232E928600C54D2D /* InstanceSelector.swift */; }; @@ -482,6 +483,7 @@ D6109A0E21459B6900432DC2 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = ""; }; D6109A10214607D500432DC2 /* Timeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline.swift; sourceTree = ""; }; D6114E0827F3EA3D0080E273 /* CrashReporterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CrashReporterViewController.swift; sourceTree = ""; }; + D6114E0A27F3F6EA0080E273 /* Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Endpoint.swift; sourceTree = ""; }; D611C2CD232DC61100C86A49 /* HashtagTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashtagTableViewCell.swift; sourceTree = ""; }; D611C2CE232DC61100C86A49 /* HashtagTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HashtagTableViewCell.xib; sourceTree = ""; }; D61AC1D2232E928600C54D2D /* InstanceSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceSelector.swift; sourceTree = ""; }; @@ -874,6 +876,7 @@ D61099D12144B2E600432DC2 /* Body.swift */, D61099D32144B32E00432DC2 /* Parameter.swift */, D61099D52144B4B200432DC2 /* FormAttachment.swift */, + D6114E0A27F3F6EA0080E273 /* Endpoint.swift */, ); path = Request; sourceTree = ""; @@ -2073,6 +2076,7 @@ D61099DC2144BDBF00432DC2 /* Response.swift in Sources */, D61099F72145693500432DC2 /* PushSubscription.swift in Sources */, D61099F5214568C300432DC2 /* Notification.swift in Sources */, + D6114E0B27F3F6EA0080E273 /* Endpoint.swift in Sources */, D61099EF214566C000432DC2 /* Instance.swift in Sources */, D61099D22144B2E600432DC2 /* Body.swift in Sources */, D623A53F2635F6910095BD04 /* Poll.swift in Sources */, diff --git a/Tusker/Views/Toast/ToastConfiguration.swift b/Tusker/Views/Toast/ToastConfiguration.swift index 969cc373..44e41764 100644 --- a/Tusker/Views/Toast/ToastConfiguration.swift +++ b/Tusker/Views/Toast/ToastConfiguration.swift @@ -45,7 +45,7 @@ extension ToastConfiguration { toast.dismissToast(animated: true) let text = """ \(title): - \(error.requestMethod.name) \(error.requestPath) + \(error.requestMethod.name) \(error.requestEndpoint) \(error.type) """