Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redact SignedData and EnvelopedData toString by default and expose helper methods for unredacted values #180

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/src/main/kotlin/app/cash/trifle/SignedData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ data class SignedData internal constructor(
}
).encode()

fun toPlaintextString(): String =
"SignedData(enveloped_data=${envelopedData.toPlaintextString()}, " +
"signature=$signature, " +
"certificates=$certificates)"

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down Expand Up @@ -131,6 +136,9 @@ data class SignedData internal constructor(
data_ = data.toByteString()
).encode()

fun toPlaintextString(): String =
"EnvelopedData(version=$version, signingAlgorithm=$signingAlgorithm, data=$data)"

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand All @@ -151,6 +159,9 @@ data class SignedData internal constructor(
return result
}

override fun toString(): String =
"EnvelopedData(version=$version, signingAlgorithm=$signingAlgorithm, data=[REDACTED])"

internal companion object {
internal const val ENVELOPED_DATA_VERSION: Int = 0

Expand Down
24 changes: 24 additions & 0 deletions common/src/test/kotlin/app/cash/trifle/TrifleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.time.Duration
import kotlin.test.assertContains
import kotlin.test.assertContentEquals

internal class TrifleTest {
@Nested
Expand Down Expand Up @@ -175,6 +177,28 @@ internal class TrifleTest {
assertEquals(endEntity.createSignedData(rawData).envelopedData.data, rawData)
}

@Test
fun `test createSignedData succeeds with SignedData#toString properly redacted`() {
val signedData = endEntity.createSignedData(rawData)

assertTrue(signedData.toString().contains("[REDACTED]"))
assertTrue(signedData.envelopedData.toString().contains("[REDACTED]"))

assertFalse(signedData.toString().contains(rawData.toString()))
assertFalse(signedData.envelopedData.toString().contains(rawData.toString()))
}

@Test
fun `test createSignedData succeeds with SignedData#toPlaintextString not redacted`() {
val signedData = endEntity.createSignedData(rawData)

assertTrue(signedData.toPlaintextString().contains(rawData.toString()))
assertTrue(signedData.envelopedData.toPlaintextString().contains(rawData.toString()))

assertFalse(signedData.toPlaintextString().contains("[REDACTED]"))
assertFalse(signedData.envelopedData.toPlaintextString().contains("[REDACTED]"))
}

@Test
fun `test createSignedData fails with InvalidSignature due to signature not matching on verify`() {
assertThrows<InvalidSignature> {
Expand Down
Loading