Skip to content

Commit 7af1848

Browse files
freelancer1845Jascha Riedel
and
Jascha Riedel
authored
updates username length to align with the spec (uint8 vs uint16) (#938)
Co-authored-by: Jascha Riedel <[email protected]>
1 parent fdf7fb0 commit 7af1848

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

rsocket-core/src/main/java/io/rsocket/metadata/AuthMetadataCodec.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class AuthMetadataCodec {
1212
static final int STREAM_METADATA_KNOWN_MASK = 0x80; // 1000 0000
1313
static final byte STREAM_METADATA_LENGTH_MASK = 0x7F; // 0111 1111
1414

15-
static final int USERNAME_BYTES_LENGTH = 1;
15+
static final int USERNAME_BYTES_LENGTH = 2;
1616
static final int AUTH_TYPE_ID_LENGTH = 1;
1717

1818
static final char[] EMPTY_CHARS_ARRAY = new char[0];
@@ -81,7 +81,7 @@ public static ByteBuf encodeMetadata(
8181
/**
8282
* Encode a Authentication CompositeMetadata payload using Simple Authentication format
8383
*
84-
* @throws IllegalArgumentException if the username length is greater than 255
84+
* @throws IllegalArgumentException if the username length is greater than 65535
8585
* @param allocator the {@link ByteBufAllocator} to use to create intermediate buffers as needed.
8686
* @param username the char sequence which represents user name.
8787
* @param password the char sequence which represents user password.
@@ -90,9 +90,9 @@ public static ByteBuf encodeSimpleMetadata(
9090
ByteBufAllocator allocator, char[] username, char[] password) {
9191

9292
int usernameLength = CharByteBufUtil.utf8Bytes(username);
93-
if (usernameLength > 255) {
93+
if (usernameLength > 65535) {
9494
throw new IllegalArgumentException(
95-
"Username should be shorter than or equal to 255 bytes length in UTF-8 encoding");
95+
"Username should be shorter than or equal to 65535 bytes length in UTF-8 encoding");
9696
}
9797

9898
int passwordLength = CharByteBufUtil.utf8Bytes(password);
@@ -101,7 +101,7 @@ public static ByteBuf encodeSimpleMetadata(
101101
allocator
102102
.buffer(capacity, capacity)
103103
.writeByte(WellKnownAuthType.SIMPLE.getIdentifier() | STREAM_METADATA_KNOWN_MASK)
104-
.writeByte(usernameLength);
104+
.writeShort(usernameLength);
105105

106106
CharByteBufUtil.writeUtf8(buffer, username);
107107
CharByteBufUtil.writeUtf8(buffer, password);
@@ -235,15 +235,15 @@ public static ByteBuf readPayload(ByteBuf metadata) {
235235
}
236236

237237
/**
238-
* Read up to 257 {@code bytes} from the given {@link ByteBuf} where the first byte is username
239-
* length and the subsequent number of bytes equal to decoded length
238+
* Read up to 65537 {@code bytes} from the given {@link ByteBuf} where the first two bytes
239+
* represent username length and the subsequent number of bytes equal to read length
240240
*
241241
* @param simpleAuthMetadata the given metadata to read username from. Please note, the {@code
242-
* simpleAuthMetadata#readIndex} should be set to the username length byte
242+
* simpleAuthMetadata#readIndex} should be set to the username length position
243243
* @return sliced {@link ByteBuf} or {@link Unpooled#EMPTY_BUFFER} if username length is zero
244244
*/
245245
public static ByteBuf readUsername(ByteBuf simpleAuthMetadata) {
246-
short usernameLength = readUsernameLength(simpleAuthMetadata);
246+
int usernameLength = readUsernameLength(simpleAuthMetadata);
247247

248248
if (usernameLength == 0) {
249249
return Unpooled.EMPTY_BUFFER;
@@ -268,15 +268,15 @@ public static ByteBuf readPassword(ByteBuf simpleAuthMetadata) {
268268
return simpleAuthMetadata.readSlice(simpleAuthMetadata.readableBytes());
269269
}
270270
/**
271-
* Read up to 257 {@code bytes} from the given {@link ByteBuf} where the first byte is username
272-
* length and the subsequent number of bytes equal to decoded length
271+
* Read up to 65537 {@code bytes} from the given {@link ByteBuf} where the first two bytes
272+
* represent username length and the subsequent number of bytes equal to read length
273273
*
274274
* @param simpleAuthMetadata the given metadata to read username from. Please note, the {@code
275275
* simpleAuthMetadata#readIndex} should be set to the username length byte
276276
* @return {@code char[]} which represents UTF-8 username
277277
*/
278278
public static char[] readUsernameAsCharArray(ByteBuf simpleAuthMetadata) {
279-
short usernameLength = readUsernameLength(simpleAuthMetadata);
279+
int usernameLength = readUsernameLength(simpleAuthMetadata);
280280

281281
if (usernameLength == 0) {
282282
return EMPTY_CHARS_ARRAY;
@@ -302,11 +302,10 @@ public static char[] readPasswordAsCharArray(ByteBuf simpleAuthMetadata) {
302302
}
303303

304304
/**
305-
* Read all the remaining {@code bytes} from the given {@link ByteBuf} where the first byte is
306-
* username length and the subsequent number of bytes equal to decoded length
305+
* Read all the remaining {@code bytes} from the given {@link ByteBuf}
307306
*
308307
* @param bearerAuthMetadata the given metadata to read username from. Please note, the {@code
309-
* simpleAuthMetadata#readIndex} should be set to the beginning of the password bytes
308+
* bearerAuthMetadata#readIndex} should be set to the beginning of the password bytes
310309
* @return {@code char[]} which represents UTF-8 password
311310
*/
312311
public static char[] readBearerTokenAsCharArray(ByteBuf bearerAuthMetadata) {
@@ -317,13 +316,13 @@ public static char[] readBearerTokenAsCharArray(ByteBuf bearerAuthMetadata) {
317316
return CharByteBufUtil.readUtf8(bearerAuthMetadata, bearerAuthMetadata.readableBytes());
318317
}
319318

320-
private static short readUsernameLength(ByteBuf simpleAuthMetadata) {
321-
if (simpleAuthMetadata.readableBytes() < 1) {
319+
private static int readUsernameLength(ByteBuf simpleAuthMetadata) {
320+
if (simpleAuthMetadata.readableBytes() < 2) {
322321
throw new IllegalStateException(
323322
"Unable to decode custom username. Not enough readable bytes");
324323
}
325324

326-
short usernameLength = simpleAuthMetadata.readUnsignedByte();
325+
int usernameLength = simpleAuthMetadata.readUnsignedShort();
327326

328327
if (simpleAuthMetadata.readableBytes() < usernameLength) {
329328
throw new IllegalArgumentException(

rsocket-core/src/test/java/io/rsocket/metadata/security/AuthMetadataFlyweightTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class AuthMetadataFlyweightTest {
1212

1313
public static final int AUTH_TYPE_ID_LENGTH = 1;
14-
public static final int USER_NAME_BYTES_LENGTH = 1;
14+
public static final int USER_NAME_BYTES_LENGTH = 2;
1515
public static final String TEST_BEARER_TOKEN =
1616
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpYXQxIjoxNTE2MjM5MDIyLCJpYXQyIjoxNTE2MjM5MDIyLCJpYXQzIjoxNTE2MjM5MDIyLCJpYXQ0IjoxNTE2MjM5MDIyfQ.ljYuH-GNyyhhLcx-rHMchRkGbNsR2_4aSxo8XjrYrSM";
1717

@@ -82,7 +82,7 @@ private static void checkSimpleAuthMetadataEncoding(
8282

8383
Assertions.assertThat(byteBuf.readUnsignedByte() & ~0x80)
8484
.isEqualTo(WellKnownAuthType.SIMPLE.getIdentifier());
85-
Assertions.assertThat(byteBuf.readUnsignedByte()).isEqualTo((short) usernameLength);
85+
Assertions.assertThat(byteBuf.readUnsignedShort()).isEqualTo((short) usernameLength);
8686

8787
Assertions.assertThat(byteBuf.readCharSequence(usernameLength, CharsetUtil.UTF_8))
8888
.isEqualTo(username);
@@ -116,16 +116,22 @@ private static void checkSimpleAuthMetadataEncodingUsingDecoders(
116116

117117
@Test
118118
void shouldThrowExceptionIfUsernameLengthExitsAllowedBounds() {
119-
String username =
119+
StringBuilder usernameBuilder = new StringBuilder();
120+
String usernamePart =
120121
"𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𢴈𢵌𢵧𢺳𣲷𤓓𤶸𤷪𥄫𦉘𦟌𦧲𦧺𧨾𨅝𨈇𨋢𨳊𨳍𨳒𩶘𠜎𠜱𠝹";
122+
for (int i = 0; i < 65535 / usernamePart.length(); i++) {
123+
usernameBuilder.append(usernamePart);
124+
}
121125
String password = "tset1234";
122126

123127
Assertions.assertThatThrownBy(
124128
() ->
125129
AuthMetadataFlyweight.encodeSimpleMetadata(
126-
ByteBufAllocator.DEFAULT, username.toCharArray(), password.toCharArray()))
130+
ByteBufAllocator.DEFAULT,
131+
usernameBuilder.toString().toCharArray(),
132+
password.toCharArray()))
127133
.hasMessage(
128-
"Username should be shorter than or equal to 255 bytes length in UTF-8 encoding");
134+
"Username should be shorter than or equal to 65535 bytes length in UTF-8 encoding");
129135
}
130136

131137
@Test
@@ -243,7 +249,7 @@ void shouldEncodeUsingWellKnownAuthType() {
243249
AuthMetadataFlyweight.encodeMetadata(
244250
ByteBufAllocator.DEFAULT,
245251
WellKnownAuthType.SIMPLE,
246-
ByteBufAllocator.DEFAULT.buffer(3, 3).writeByte(1).writeByte('u').writeByte('p'));
252+
ByteBufAllocator.DEFAULT.buffer().writeShort(1).writeByte('u').writeByte('p'));
247253

248254
checkSimpleAuthMetadataEncoding("u", "p", 1, 1, byteBuf);
249255
}
@@ -254,7 +260,7 @@ void shouldEncodeUsingWellKnownAuthType1() {
254260
AuthMetadataFlyweight.encodeMetadata(
255261
ByteBufAllocator.DEFAULT,
256262
WellKnownAuthType.SIMPLE,
257-
ByteBufAllocator.DEFAULT.buffer().writeByte(1).writeByte('u').writeByte('p'));
263+
ByteBufAllocator.DEFAULT.buffer().writeShort(1).writeByte('u').writeByte('p'));
258264

259265
checkSimpleAuthMetadataEncoding("u", "p", 1, 1, byteBuf);
260266
}
@@ -298,7 +304,7 @@ void shouldCompressMetadata() {
298304
AuthMetadataFlyweight.encodeMetadataWithCompression(
299305
ByteBufAllocator.DEFAULT,
300306
"simple",
301-
ByteBufAllocator.DEFAULT.buffer().writeByte(1).writeByte('u').writeByte('p'));
307+
ByteBufAllocator.DEFAULT.buffer().writeShort(1).writeByte('u').writeByte('p'));
302308

303309
checkSimpleAuthMetadataEncoding("u", "p", 1, 1, byteBuf);
304310
}

0 commit comments

Comments
 (0)