Throw an exception if header is truncated

Signed-off-by: Graham <gpe@openrs2.org>
Graham 3 years ago
parent 73eb30dbf9
commit 1a78ef3c7d
  1. 12
      cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt
  2. 27
      cache/src/test/kotlin/org/openrs2/cache/Js5CompressionTest.kt
  3. BIN
      cache/src/test/resources/org/openrs2/cache/compression/missing-header.dat

@ -108,6 +108,10 @@ public object Js5Compression {
} }
public fun uncompress(input: ByteBuf, key: XteaKey = XteaKey.ZERO): ByteBuf { public fun uncompress(input: ByteBuf, key: XteaKey = XteaKey.ZERO): ByteBuf {
if (input.readableBytes() < 5) {
throw IOException("Missing header")
}
val typeId = input.readUnsignedByte().toInt() val typeId = input.readUnsignedByte().toInt()
val type = Js5CompressionType.fromOrdinal(typeId) val type = Js5CompressionType.fromOrdinal(typeId)
?: throw IOException("Invalid compression type: $typeId") ?: throw IOException("Invalid compression type: $typeId")
@ -167,6 +171,10 @@ public object Js5Compression {
} }
public fun uncompressIfKeyValid(input: ByteBuf, key: XteaKey): ByteBuf? { public fun uncompressIfKeyValid(input: ByteBuf, key: XteaKey): ByteBuf? {
if (input.readableBytes() < 5) {
throw IOException("Missing header")
}
val typeId = input.readUnsignedByte().toInt() val typeId = input.readUnsignedByte().toInt()
val type = Js5CompressionType.fromOrdinal(typeId) val type = Js5CompressionType.fromOrdinal(typeId)
?: throw IOException("Invalid compression type: $typeId") ?: throw IOException("Invalid compression type: $typeId")
@ -358,6 +366,10 @@ public object Js5Compression {
} }
public fun isEmptyLoc(buf: ByteBuf): Boolean { public fun isEmptyLoc(buf: ByteBuf): Boolean {
if (buf.readableBytes() < 5) {
throw IOException("Missing header")
}
val typeId = buf.readUnsignedByte().toInt() val typeId = buf.readUnsignedByte().toInt()
val type = Js5CompressionType.fromOrdinal(typeId) val type = Js5CompressionType.fromOrdinal(typeId)
?: throw IOException("Invalid compression type: $typeId") ?: throw IOException("Invalid compression type: $typeId")

@ -703,6 +703,33 @@ class Js5CompressionTest {
} }
} }
@Test
fun testMissingHeader() {
read("missing-header.dat").use { compressed ->
assertFailsWith<IOException> {
Js5Compression.uncompress(compressed)
}
}
read("missing-header.dat").use { compressed ->
assertFailsWith<IOException> {
Js5Compression.uncompressUnlessEncrypted(compressed)
}
}
read("missing-header.dat").use { compressed ->
assertFailsWith<IOException> {
Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO)
}
}
read("missing-header.dat").use { compressed ->
assertFailsWith<IOException> {
Js5Compression.isEmptyLoc(compressed)
}
}
}
private fun read(name: String): ByteBuf { private fun read(name: String): ByteBuf {
Js5CompressionTest::class.java.getResourceAsStream("compression/$name").use { input -> Js5CompressionTest::class.java.getResourceAsStream("compression/$name").use { input ->
return Unpooled.wrappedBuffer(input.readBytes()) return Unpooled.wrappedBuffer(input.readBytes())

Loading…
Cancel
Save