Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;

import com.ongres.scram.common.ClientFinalMessage;
import com.ongres.scram.common.ClientFirstMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public int getIterationCount() {
throw new ScramParseException(
"nonce must be the 1st element of the server-first-message");
}
if (!isValidNonce(nonce.getValue())) {
throw new ScramParseException("nonce contains invalid characters");
}
if (!nonce.getValue().startsWith(clientNonce)) {
throw new ScramParseException("parsed nonce does not start with client nonce");
}
Expand All @@ -176,6 +179,17 @@ public int getIterationCount() {
salt.getValue(), iterationInt);
}

// RFC 5802 Section 7: printable = %x21-2B / %x2D-7E (printable ASCII excluding comma)
private static boolean isValidNonce(String nonce) {
for (int i = 0; i < nonce.length(); i++) {
char c = nonce.charAt(i);
if (c < 0x21 || c > 0x7E || c == ',') {
return false;
}
}
return true;
}

@Override
StringBuilder writeTo(StringBuilder sb) {
return StringWritableCsv.writeTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import static com.ongres.scram.common.RfcExampleSha1.CLIENT_NONCE;
import static com.ongres.scram.common.RfcExampleSha1.SERVER_FIRST_MESSAGE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.stream.Stream;

import com.ongres.scram.common.exception.ScramParseException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

class ServerFirstMessageTest {
@Test
Expand All @@ -31,4 +36,20 @@ void validParseFrom() throws ScramParseException {

assertEquals(SERVER_FIRST_MESSAGE, serverFirstMessage.toString());
}

static Stream<String> invalidServerNonceSuffixes() {
return Stream.of(
"server,nonce", // printable ASCII except ","
"server nonce", // space (0x20) is below the printable ASCII range
"servernonce" + (char) 0x7F // DEL (0x7F) is above the printable ASCII range
);
}

@ParameterizedTest(name = "nonce with invalid char rejected")
@MethodSource("invalidServerNonceSuffixes")
void invalidNonceCharactersRejected(String serverNonceSuffix) {
String message = "r=" + CLIENT_NONCE + serverNonceSuffix + ",s=QSXCR+Q6sek8bf92,i=4096";
assertThrows(ScramParseException.class,
() -> ServerFirstMessage.parseFrom(message, CLIENT_NONCE));
}
}
Loading