-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fortify
Secret.Wrapped
value protection (#9)
To improve and avoid even more cases we decided to harden the `Wrapped` value for the property wrapper so that even more accidental exposure are avoided. To access the underlying value now you must pass through the `projectedValue` that returns an `UnwrappedSecret`.
- Loading branch information
Showing
8 changed files
with
229 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// MARK: - Redact | ||
|
||
public struct Redactor<Value>: Sendable { | ||
public var redact: @Sendable (Value) -> Value | ||
public var redactForDescription: @Sendable (Value) -> String | ||
public var redactForDebug: @Sendable (Value) -> String | ||
|
||
public init( | ||
redact: @escaping @Sendable (Value) -> Value, | ||
redactForDescription: @escaping @Sendable (Value) -> String = { _ in "************" }, | ||
redactForDebug: @escaping @Sendable (Value) -> String = { _ in | ||
"Secret([REDACTED \(Value.self)])" | ||
} | ||
) { | ||
self.redact = redact | ||
self.redactForDescription = redactForDescription | ||
self.redactForDebug = redactForDebug | ||
} | ||
} | ||
|
||
// MARK: - Redactor<String> | ||
|
||
public extension Redactor where Value == String { | ||
static let `default` = Redactor { @Sendable _ in "************" } | ||
} | ||
|
||
// MARK: - Redactor<Int> | ||
|
||
public extension Redactor where Value == Int { | ||
static let `default` = Redactor { @Sendable _ in -1 } | ||
} | ||
|
||
// MARK: - Redactor<Double> | ||
|
||
public extension Redactor where Value == Double { | ||
static let `default` = Redactor { @Sendable _ in -1.0 } | ||
} | ||
|
||
// MARK: - Redactor<Bool> | ||
|
||
public extension Redactor where Value == Bool { | ||
static let `defaultTrue` = Redactor { @Sendable _ in true } | ||
static let `defaultFalse` = Redactor { @Sendable _ in false } | ||
} |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
// MARK: - Decodable | ||
|
||
extension Secret: Decodable where Wrapped: Decodable & RedactableForDecodable { | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let value = try container.decode(Wrapped.self) | ||
|
||
self = Secret(value, redactor: Wrapped.redactor) | ||
} | ||
} | ||
|
||
public protocol RedactableForDecodable { | ||
static var redactor: Redactor<Self> { get } | ||
} |
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,8 @@ | ||
// MARK: - Encodable | ||
|
||
extension Secret: Encodable where Wrapped: Encodable { | ||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(projectedValue.value) | ||
} | ||
} |
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
Oops, something went wrong.