Skip to content

Commit 238cf8b

Browse files
authored
Fix @sensitive handling in Display implementations on error shapes (#1802)
* Use `Sensitive` wrapper in Debug impl for structures * Fix using the wrong import path for `Sensitive` * Use redactMemberIfNecessary * Fix display implementation on errors to respect @sensitive trait * Don't use Sensitive type just yet * Add entry in changelog * Improve redaction of sensitive error message * Use correct flags in changelog * Run ktlint
1 parent e78da55 commit 238cf8b

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

CHANGELOG.next.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ references = ["smithy-rs#1803"]
3636
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "server"}
3737
author = "LukeMathWalker"
3838

39+
[[smithy-rs]]
40+
message = "Sensitive fields in errors now respect @sensitive trait and are properly redacted."
41+
references = ["smithy-rs#1802"]
42+
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "all" }
43+
author = "jjant"
44+
3945
[[smithy-rs]]
4046
message = "Pokémon Service example code now runs clippy during build."
4147
references = ["smithy-rs#1727"]
@@ -117,7 +123,7 @@ author = "jdisanti"
117123
[[smithy-rs]]
118124
message = "Paginators now stop on encountering a duplicate token by default rather than panic. This behavior can be customized by toggling the `stop_on_duplicate_token` property on the paginator before calling `send`."
119125
references = ["aws-sdk-rust#620", "smithy-rs#1748"]
120-
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "client"}
126+
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "client" }
121127
author = "jdisanti"
122128

123129
[[aws-sdk-rust]]

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/StructureGenerator.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ open class StructureGenerator(
6363
fun render(forWhom: CodegenTarget = CodegenTarget.CLIENT) {
6464
renderStructure()
6565
errorTrait?.also { errorTrait ->
66-
ErrorGenerator(symbolProvider, writer, shape, errorTrait).render(forWhom)
66+
ErrorGenerator(model, symbolProvider, writer, shape, errorTrait).render(forWhom)
6767
}
6868
}
6969

@@ -109,6 +109,7 @@ open class StructureGenerator(
109109
members.forEach { member ->
110110
val memberName = symbolProvider.toMemberName(member)
111111
val fieldValue = member.redactIfNecessary(model, "self.$memberName")
112+
112113
rust(
113114
"formatter.field(${memberName.dq()}, &$fieldValue);",
114115
)

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/error/ErrorGenerator.kt

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package software.amazon.smithy.rust.codegen.core.smithy.generators.error
77

8+
import software.amazon.smithy.model.Model
89
import software.amazon.smithy.model.shapes.StructureShape
910
import software.amazon.smithy.model.traits.ErrorTrait
1011
import software.amazon.smithy.model.traits.RetryableTrait
@@ -19,10 +20,12 @@ import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
1920
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.StdError
2021
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider
2122
import software.amazon.smithy.rust.codegen.core.smithy.isOptional
23+
import software.amazon.smithy.rust.codegen.core.util.REDACTION
2224
import software.amazon.smithy.rust.codegen.core.util.dq
2325
import software.amazon.smithy.rust.codegen.core.util.errorMessageMember
2426
import software.amazon.smithy.rust.codegen.core.util.getTrait
2527
import software.amazon.smithy.rust.codegen.core.util.letIf
28+
import software.amazon.smithy.rust.codegen.core.util.shouldRedact
2629

2730
sealed class ErrorKind {
2831
abstract fun writable(runtimeConfig: RuntimeConfig): Writable
@@ -60,6 +63,7 @@ fun StructureShape.modeledRetryKind(errorTrait: ErrorTrait): ErrorKind? {
6063
}
6164

6265
class ErrorGenerator(
66+
private val model: Model,
6367
private val symbolProvider: RustSymbolProvider,
6468
private val writer: RustWriter,
6569
private val shape: StructureShape,
@@ -118,8 +122,12 @@ class ErrorGenerator(
118122
}
119123
write("write!(f, ${errorDesc.dq()})?;")
120124
messageShape?.let {
121-
ifSet(it, symbolProvider.toSymbol(it), "&self.message") { field ->
122-
write("""write!(f, ": {}", $field)?;""")
125+
if (it.shouldRedact(model)) {
126+
write("""write!(f, ": {}", $REDACTION)?;""")
127+
} else {
128+
ifSet(it, symbolProvider.toSymbol(it), "&self.message") { field ->
129+
write("""write!(f, ": {}", $field)?;""")
130+
}
123131
}
124132
}
125133
write("Ok(())")

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/util/Smithy.kt

+12-7
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,19 @@ fun ServiceShape.hasEventStreamOperations(model: Model): Boolean = operations.an
8484
model.expectShape(id, OperationShape::class.java).isEventStream(model)
8585
}
8686

87-
fun Shape.redactIfNecessary(model: Model, safeToPrint: String): String =
87+
fun Shape.shouldRedact(model: Model): Boolean =
8888
when (this) {
89-
is MemberShape -> model.expectShape(this.target).redactIfNecessary(model, safeToPrint)
90-
else -> if (this.hasTrait<SensitiveTrait>()) {
91-
"*** Sensitive Data Redacted ***".dq()
92-
} else {
93-
safeToPrint
94-
}
89+
is MemberShape -> model.expectShape(this.target).shouldRedact(model)
90+
else -> this.hasTrait<SensitiveTrait>()
91+
}
92+
93+
const val REDACTION = "\"*** Sensitive Data Redacted ***\""
94+
95+
fun Shape.redactIfNecessary(model: Model, safeToPrint: String): String =
96+
if (this.shouldRedact(model)) {
97+
REDACTION
98+
} else {
99+
safeToPrint
95100
}
96101

97102
/*

0 commit comments

Comments
 (0)