Redact request paths in error reporter

This commit is contained in:
Shadowfacts 2022-03-29 22:37:39 -04:00
parent 2444783edf
commit 7da139be4d
6 changed files with 75 additions and 9 deletions

View File

@ -106,7 +106,7 @@ public class Client {
func createURLRequest<Result>(request: Request<Result>) -> URLRequest? { func createURLRequest<Result>(request: Request<Result>) -> URLRequest? {
guard var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: true) else { return nil } 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 components.queryItems = request.queryParameters.isEmpty ? nil : request.queryParameters.queryItems
guard let url = components.url else { return nil } guard let url = components.url else { return nil }
var urlRequest = URLRequest(url: url, timeoutInterval: timeoutInterval) 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" }), if let url = wellKnown.links.first(where: { $0.rel == "http://nodeinfo.diaspora.software/ns/schema/2.0" }),
let components = URLComponents(string: url.href), let components = URLComponents(string: url.href),
components.host == self.baseURL.host { components.host == self.baseURL.host {
let nodeInfo = Request<NodeInfo>(method: .get, path: components.path) let nodeInfo = Request<NodeInfo>(method: .get, path: Endpoint(stringLiteral: components.path))
self.run(nodeInfo, completion: completion) self.run(nodeInfo, completion: completion)
} }
} }
@ -397,12 +397,12 @@ public class Client {
extension Client { extension Client {
public struct Error: LocalizedError { public struct Error: LocalizedError {
public let requestMethod: Method public let requestMethod: Method
public let requestPath: String public let requestEndpoint: Endpoint
public let type: ErrorType public let type: ErrorType
init<ResultType: Decodable>(request: Request<ResultType>, type: ErrorType) { init<ResultType: Decodable>(request: Request<ResultType>, type: ErrorType) {
self.requestMethod = request.method self.requestMethod = request.method
self.requestPath = request.path self.requestEndpoint = request.endpoint
self.type = type self.type = type
} }

View File

@ -17,7 +17,7 @@ public enum Timeline {
} }
extension Timeline { extension Timeline {
var endpoint: String { var endpoint: Endpoint {
switch self { switch self {
case .home: case .home:
return "/api/v1/timelines/home" return "/api/v1/timelines/home"

View File

@ -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 "<redacted>"
}
}.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)
}
}

View File

@ -10,13 +10,13 @@ import Foundation
public struct Request<ResultType: Decodable> { public struct Request<ResultType: Decodable> {
let method: Method let method: Method
let path: String let endpoint: Endpoint
let body: Body let body: Body
var queryParameters: [Parameter] 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.method = method
self.path = path self.endpoint = path
self.body = body self.body = body
self.queryParameters = queryParameters self.queryParameters = queryParameters
} }

View File

@ -70,6 +70,7 @@
D6109A0F21459B6900432DC2 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A0E21459B6900432DC2 /* Pagination.swift */; }; D6109A0F21459B6900432DC2 /* Pagination.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A0E21459B6900432DC2 /* Pagination.swift */; };
D6109A11214607D500432DC2 /* Timeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A10214607D500432DC2 /* Timeline.swift */; }; D6109A11214607D500432DC2 /* Timeline.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6109A10214607D500432DC2 /* Timeline.swift */; };
D6114E0927F3EA3D0080E273 /* CrashReporterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6114E0827F3EA3D0080E273 /* CrashReporterViewController.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 */; }; D611C2CF232DC61100C86A49 /* HashtagTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D611C2CD232DC61100C86A49 /* HashtagTableViewCell.swift */; };
D611C2D0232DC61100C86A49 /* HashtagTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D611C2CE232DC61100C86A49 /* HashtagTableViewCell.xib */; }; D611C2D0232DC61100C86A49 /* HashtagTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D611C2CE232DC61100C86A49 /* HashtagTableViewCell.xib */; };
D61AC1D3232E928600C54D2D /* InstanceSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = D61AC1D2232E928600C54D2D /* InstanceSelector.swift */; }; 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 = "<group>"; }; D6109A0E21459B6900432DC2 /* Pagination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Pagination.swift; sourceTree = "<group>"; };
D6109A10214607D500432DC2 /* Timeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline.swift; sourceTree = "<group>"; }; D6109A10214607D500432DC2 /* Timeline.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timeline.swift; sourceTree = "<group>"; };
D6114E0827F3EA3D0080E273 /* CrashReporterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CrashReporterViewController.swift; sourceTree = "<group>"; }; D6114E0827F3EA3D0080E273 /* CrashReporterViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CrashReporterViewController.swift; sourceTree = "<group>"; };
D6114E0A27F3F6EA0080E273 /* Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Endpoint.swift; sourceTree = "<group>"; };
D611C2CD232DC61100C86A49 /* HashtagTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashtagTableViewCell.swift; sourceTree = "<group>"; }; D611C2CD232DC61100C86A49 /* HashtagTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashtagTableViewCell.swift; sourceTree = "<group>"; };
D611C2CE232DC61100C86A49 /* HashtagTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HashtagTableViewCell.xib; sourceTree = "<group>"; }; D611C2CE232DC61100C86A49 /* HashtagTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HashtagTableViewCell.xib; sourceTree = "<group>"; };
D61AC1D2232E928600C54D2D /* InstanceSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceSelector.swift; sourceTree = "<group>"; }; D61AC1D2232E928600C54D2D /* InstanceSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceSelector.swift; sourceTree = "<group>"; };
@ -874,6 +876,7 @@
D61099D12144B2E600432DC2 /* Body.swift */, D61099D12144B2E600432DC2 /* Body.swift */,
D61099D32144B32E00432DC2 /* Parameter.swift */, D61099D32144B32E00432DC2 /* Parameter.swift */,
D61099D52144B4B200432DC2 /* FormAttachment.swift */, D61099D52144B4B200432DC2 /* FormAttachment.swift */,
D6114E0A27F3F6EA0080E273 /* Endpoint.swift */,
); );
path = Request; path = Request;
sourceTree = "<group>"; sourceTree = "<group>";
@ -2073,6 +2076,7 @@
D61099DC2144BDBF00432DC2 /* Response.swift in Sources */, D61099DC2144BDBF00432DC2 /* Response.swift in Sources */,
D61099F72145693500432DC2 /* PushSubscription.swift in Sources */, D61099F72145693500432DC2 /* PushSubscription.swift in Sources */,
D61099F5214568C300432DC2 /* Notification.swift in Sources */, D61099F5214568C300432DC2 /* Notification.swift in Sources */,
D6114E0B27F3F6EA0080E273 /* Endpoint.swift in Sources */,
D61099EF214566C000432DC2 /* Instance.swift in Sources */, D61099EF214566C000432DC2 /* Instance.swift in Sources */,
D61099D22144B2E600432DC2 /* Body.swift in Sources */, D61099D22144B2E600432DC2 /* Body.swift in Sources */,
D623A53F2635F6910095BD04 /* Poll.swift in Sources */, D623A53F2635F6910095BD04 /* Poll.swift in Sources */,

View File

@ -45,7 +45,7 @@ extension ToastConfiguration {
toast.dismissToast(animated: true) toast.dismissToast(animated: true)
let text = """ let text = """
\(title): \(title):
\(error.requestMethod.name) \(error.requestPath) \(error.requestMethod.name) \(error.requestEndpoint)
\(error.type) \(error.type)
""" """