Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate that ASCII Metadata values contain only printable ASCII Characters #11696

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions api/src/main/java/io/grpc/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,19 @@ public byte[] parseBytes(byte[] serialized) {
* Simple metadata marshaller that encodes strings as is.
*
* <p>This should be used with ASCII strings that only contain the characters listed in the class
* comment of {@link AsciiMarshaller}. Otherwise the output may be considered invalid and
* discarded by the transport, or the call may fail.
* comment of {@link AsciiMarshaller}. Otherwise an {@link IllegalArgumentException} will be
* thrown.
*/
public static final AsciiMarshaller<String> ASCII_STRING_MARSHALLER =
new AsciiMarshaller<String>() {

@Override
public String toAsciiString(String value) {
return value;
checkArgument(
value.chars().allMatch(c -> c >= 0x20 && c <= 0x7E),
"String \"%s\" contains non-printable ASCII characters",
value);
return value.trim();
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions api/src/test/java/io/grpc/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,17 @@ public void createFromPartial() {
assertSame(anotherSalmon, h2.get(KEY_IMMUTABLE));
}

@Test
public void failNonPrintableAsciiCharacters() {
String value = "José";

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("String \"" + value + "\" contains non-printable ASCII characters");

Metadata metadata = new Metadata();
metadata.put(Metadata.Key.of("test-non-printable", Metadata.ASCII_STRING_MARSHALLER), value);
}

private static final class Fish {
private String name;

Expand Down
Loading