Skip to content

Commit 0624ee8

Browse files
authored
Merge pull request #543 from android-password-store/ai-bugs
fix: address some issues found by Copilot
2 parents 9c43391 + 9d9802a commit 0624ee8

5 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/kotlin/kage/crypto/stream/EncryptOutputStream.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ internal class EncryptOutputStream(private val key: ByteArray, private val dst:
6161
throw StreamException("internal error: flush called with partial chunk")
6262
}
6363

64-
if (bufSize == 0 && !nonceIsZero(this.nonce)) throw StreamException("chunk cannot be empty")
64+
if (bufSize == 0 && !nonceIsZero(this.nonce))
65+
throw StreamException("only the first chunk can be empty")
6566

6667
if (last) setLastChunkFlag(nonce)
6768

src/kotlin/kage/errors/ParseException.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
*/
66
package kage.errors
77

8-
/** Wrapper type for errors triggered while parsing an [kage.format.AgeStanza] */
8+
/**
9+
* Wrapper type for errors triggered while parsing age-formatted structures (for example,
10+
* [kage.format.AgeHeader], [kage.format.AgeKeyFile], and [kage.format.AgeStanza]).
11+
*/
912
public sealed class ParseException
1013
@JvmOverloads
1114
constructor(message: String? = null, cause: Throwable? = null) : Exception(message, cause)
1215

13-
/** Raised when a non-ASCII string is encountered when parsing an [kage.format.AgeHeader]. */
16+
/**
17+
* Raised when an invalid arbitrary string is encountered when parsing an [kage.format.AgeHeader],
18+
* such as one containing disallowed non-ASCII characters.
19+
*/
1420
public class InvalidArbitraryStringException
1521
@JvmOverloads
1622
constructor(message: String? = null, cause: Throwable? = null) : ParseException(message, cause)

src/test/kotlin/AgeTest.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import java.io.ByteArrayInputStream
1010
import java.io.ByteArrayOutputStream
1111
import java.io.InputStream
1212
import java.security.SecureRandom
13-
import java.util.Random
1413
import kage.crypto.scrypt.ScryptIdentity
1514
import kage.crypto.scrypt.ScryptRecipient
1615
import kage.crypto.stream.EncryptOutputStream.Companion.CHUNK_SIZE
@@ -104,7 +103,7 @@ class AgeTest {
104103

105104
// Encrypt exactly 2 chunks
106105
val i = ByteArray(CHUNK_SIZE * 2)
107-
Random().nextBytes(i)
106+
SecureRandom().nextBytes(i)
108107

109108
val bais = ByteArrayInputStream(i)
110109
val baos = ByteArrayOutputStream()
@@ -123,9 +122,9 @@ class AgeTest {
123122
fun testScryptEncryptDecryptExactBlock() {
124123
val (recipient, identity) = genX25519Identity()
125124

126-
// Encrypt exactly 1 chunks
125+
// Encrypt exactly 1 chunk
127126
val i = ByteArray(CHUNK_SIZE)
128-
Random().nextBytes(i)
127+
SecureRandom().nextBytes(i)
129128

130129
val bais = ByteArrayInputStream(i)
131130
val baos = ByteArrayOutputStream()
@@ -170,7 +169,7 @@ class AgeTest {
170169
val (otherRecipient, otherIdentity) = genX25519Identity()
171170

172171
val payload = ByteArray(1023)
173-
Random().nextBytes(payload)
172+
SecureRandom().nextBytes(payload)
174173

175174
val bais = ByteArrayInputStream(payload)
176175
val ageFile = Age.encrypt(listOf(otherRecipient, recipient), bais)
@@ -188,7 +187,7 @@ class AgeTest {
188187
val (_, otherIdentity) = genX25519Identity()
189188

190189
val payload = ByteArray(1023)
191-
Random().nextBytes(payload)
190+
SecureRandom().nextBytes(payload)
192191

193192
val bais = ByteArrayInputStream(payload)
194193
val ageFile = Age.encrypt(listOf(recipient), bais)
@@ -203,7 +202,7 @@ class AgeTest {
203202
val scryptRecipient = ScryptRecipient("mypass1".toByteArray())
204203

205204
val payload = ByteArray(1023)
206-
Random().nextBytes(payload)
205+
SecureRandom().nextBytes(payload)
207206

208207
val bais = ByteArrayInputStream(payload)
209208

src/test/kotlin/kage/test/utils/TestSuite.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ private constructor(
5757
} while (true)
5858

5959
check(expect != null) { "invalid test file: no 'expect' header found" }
60-
check((expect != Expect.Success || expect != Expect.PayloadFailure) || payloadHash != null) {
61-
// This check verifies that the payload is present except when expectation is either
60+
check((expect != Expect.Success && expect != Expect.PayloadFailure) || payloadHash != null) {
61+
// This check verifies that the payload is required except when expectation is either
6262
// Success or PayloadFailure
6363
"invalid test file: no 'payload' header found"
6464
}

src/test/kotlin/kage/utils/Extensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import org.bouncycastle.crypto.InvalidCipherTextException
2424
private inline fun <reified T : Exception> hasCause(error: Throwable): Boolean {
2525
var cause = error.cause
2626
while (cause != null) {
27-
if (error.cause is T) return true
27+
if (cause is T) return true
2828
cause = cause.cause
2929
}
3030
return false

0 commit comments

Comments
 (0)