Skip to content

Commit 9d17bea

Browse files
committed
Fix UUID decoder bug
The UUID decoder now does NOT allow decoding of a subtype 3 (legacy UUID) BSON Binary value when the UUID decoder's UUID representation is STANDARD.
1 parent 8f866e2 commit 9d17bea

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

bson/src/main/org/bson/internal/UuidHelper.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ public static UUID decodeBinaryToUuid(final byte[] data, final byte type, final
102102
reverseByteArray(data, 8, 8);
103103
break;
104104
case PYTHON_LEGACY:
105-
case STANDARD:
106105
break;
106+
case STANDARD:
107+
throw new BSONException("Can not decode a subtype 3 (UUID legacy) BSON binary when the decoder is configured to use " +
108+
"the standard UUID representation");
107109
default:
108110
throw new BSONException("Unexpected UUID representation");
109111
}

bson/src/test/unit/org/bson/internal/UuidHelperSpecification.groovy

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bson.internal
22

3+
import org.bson.BSONException
34
import org.bson.UuidRepresentation
45
import spock.lang.Specification
56
import spock.lang.Unroll
@@ -31,10 +32,21 @@ class UuidHelperSpecification extends Specification {
3132
uuid == UuidHelper.decodeBinaryToUuid(expectedBytes, (byte) type, uuidRepresentation)
3233

3334
where:
34-
uuid | type | uuidRepresentation
35+
uuid | type | uuidRepresentation
3536
UUID.fromString('08070605-0403-0201-100f-0e0d0c0b0a09') | 3 | UuidRepresentation.JAVA_LEGACY
36-
UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | 4 | UuidRepresentation.STANDARD
3737
UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | 3 | UuidRepresentation.PYTHON_LEGACY
3838
UUID.fromString('04030201-0605-0807-090a-0b0c0d0e0f10') | 3 | UuidRepresentation.C_SHARP_LEGACY
39+
UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | 4 | UuidRepresentation.STANDARD
40+
}
41+
42+
def 'should error when decoding a subtype 3 binary to standard representation'() {
43+
given:
44+
byte[] expectedBytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
45+
46+
when:
47+
UuidHelper.decodeBinaryToUuid(expectedBytes, (byte) 3, UuidRepresentation.STANDARD)
48+
49+
then:
50+
thrown(BSONException)
3951
}
4052
}

0 commit comments

Comments
 (0)