Skip to content

Formats

Bugs in formats hide in the text a decoder accepts but shouldn’t. In 2014, Git, libgit2 and JGit could be made to overwrite .git/config through a path spelled .Git/config, or git~1 on Windows (CVE-2014-9390). In base64, the bits after the last byte must be zero, or two texts decode to the same bytes, and a signature over one also verifies the other. Random strings almost never hit either case.

This page shows the two formats proptastic draws itself, base64 and hex, and builds the rest from the general words. A format gets its own word only if no general word covers it and it has a rule a regular expression can’t state, such as base64’s pad bits.

bytes() draws byte arrays, and .inBase64() and .inHex() draw them as text. The text is always the bytes’ encoding, so when a property fails it shrinks over the bytes, and every text it tries is valid:

FormatsTest.java
@Test
void aBase64TextDecodesAndEncodesBack() {
forAll(bytes().inBase64())
.check(
text -> {
var bytes = Base64.getDecoder().decode(text);
assertEquals(text, Base64.getEncoder().encodeToString(bytes));
});
}

.inBase64() is the standard alphabet, padded. Pass the encoder your code speaks for another, .inBase64(Base64.getUrlEncoder().withoutPadding()). .inHex() draws lower and upper case.

.nonCanonical() draws what a strict decoder must refuse: non-zero pad bits or wrong padding in base64, a digit too many in hex. Java’s own decoder refuses wrong padding, but not all of it:

FormatsTest.java
@Test
void onlyOneTextDecodesToTheseBytes() {
forAll(bytes().nonEmpty().inBase64().nonCanonical())
.check(
text -> {
byte[] bytes;
try {
bytes = Base64.getDecoder().decode(text);
} catch (IllegalArgumentException refused) {
return; // refusing it is right
}
assertEquals(text, Base64.getEncoder().encodeToString(bytes));
});
}
Terminal window
expected: <AI==> but was: <AA==>
onlyOneTextDecodesToTheseBytes failed after 2 tries (seed 0x5eed)
shrunk counterexample: text = "AI=="
pin it: .example("AI==")
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'FormatsTest.onlyOneTextDecodesToTheseBytes'

AI== decodes to the one zero byte AA== does: its pad bits are set, and Java’s decoder ignores them. If your code signs, hashes or compares the text rather than the bytes, refuse such text before it does.

A colour, a locale or a version number needs no word of its own: the general words build it, and a property says what your code must do with it.

A hex colour is a regular expression, with the plain values a shrink won’t reach given as edges:

FormatsTest.java
static final String HEX = "[0-9a-fA-F]";
static final String COLOUR = "#(%1$s{3,4}|%1$s{6}|%1$s{8})".formatted(HEX);
@Test
void aColourParsesToItsRgb() {
forAll(strings().matching(COLOUR).edges("#000000", "#FFFFFF", "#fff"))
.check(
colour -> {
boolean shortForm = colour.length() <= 5;
assertEquals(shortForm, Colours.isShort(colour));
});
}

A locale’s bug is in the code that runs under it, not in the tag. Run the code under the locales that break it, Turkish and Azerbaijani with their dotless i, and Lithuanian:

FormatsTest.java
@Test
void aTitleLowersTheSameEverywhere() {
var locales =
elements(
Locale.ROOT,
Locale.forLanguageTag("tr"),
Locale.forLanguageTag("az"),
Locale.forLanguageTag("lt"));
forAll(locales)
.check(
locale -> {
var lowered = "TITLE".toLowerCase(locale);
assertEquals("title", lowered);
});
}
Terminal window
expected: <title> but was: <tıtle>
aTitleLowersTheSameEverywhere failed after 3 tries (seed 0x5eed)
shrunk counterexample: locale = tr
pin it: .seed(0x5eed) // no source form for this input
rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'FormatsTest.aTitleLowersTheSameEverywhere'

"TITLE".toLowerCase(Locale.ROOT) is the fix, for text that isn’t for a person to read.

Privacy