Verified Commit bf5d8a21 authored by Georg Koppen's avatar Georg Koppen
Browse files

Make that contact field does not contain strange characters that break the service

Patch written by hiro.

Closes: #40027.
parent 6e404499
Pipeline #40406 passed with stage
in 2 minutes and 37 seconds
......@@ -422,7 +422,13 @@ public class DetailsDocument extends Document {
private String contact;
/**
* Santize the contact field if it contains a percent char by removing it.
*/
public void setContact(String contact) {
if ((contact != null) && (contact.indexOf('%') != -1)) {
contact = contact.replace("%", "");
}
this.contact = escapeJson(contact);
}
......
......@@ -127,7 +127,13 @@ public class DetailsStatus extends Document {
private String contact;
/**
* Santize the contact field if it contains a percent char by removing it.
*/
public void setContact(String contact) {
if ((contact != null) && (contact.indexOf('%') != -1)) {
contact = contact.replace("%", "");
}
this.contact = escapeJson(contact);
}
......
......@@ -180,9 +180,6 @@ public class ResponseBuilder {
}
private void write(PrintWriter pw, String format, Object ... args) {
if (format.indexOf('%') != -1) {
format = format.replace("%", "%%");
}
String stringToWrite = String.format(format, args);
this.charsWritten += stringToWrite.length();
pw.write(stringToWrite);
......
......@@ -96,5 +96,46 @@ public class DetailsDocumentWriterTest {
Arrays.asList(GABELMOO_OR_ADDRESS),
document.getUnreachableOrAddresses());
}
}
@Test
public void testContactDetails() {
DetailsStatus status = new DetailsStatus();
status.setRelay(true);
status.setAdvertisedOrAddresses(Arrays.asList(GABELMOO_OR_ADDRESS));
status.setOrAddressesAndPorts(new TreeSet<>(Arrays.asList(
GABELMOO_OR_ADDRESS)));
status.setContact("gabelmoo@torproject.org");
this.documentStore.addDocument(status, GABELMOO_FINGERPRINT);
DetailsDocumentWriter writer = new DetailsDocumentWriter();
writer.writeDocuments(-1L);
assertEquals("One document should be written.", 1,
this.documentStore.getPerformedStoreOperations());
DetailsDocument document = this.documentStore.getDocument(
DetailsDocument.class, GABELMOO_FINGERPRINT);
assertNotNull("There should be a document for the given fingerprint.",
document);
assertEquals("gabelmoo@torproject.org",
document.getContact());
}
@Test
public void testContactDetailsWithPercentCharacters() {
DetailsStatus status = new DetailsStatus();
status.setRelay(true);
status.setAdvertisedOrAddresses(Arrays.asList(GABELMOO_OR_ADDRESS));
status.setOrAddressesAndPorts(new TreeSet<>(Arrays.asList(
GABELMOO_OR_ADDRESS)));
status.setContact("gabelmoo%@torproject.org (% 2+6)");
this.documentStore.addDocument(status, GABELMOO_FINGERPRINT);
DetailsDocumentWriter writer = new DetailsDocumentWriter();
writer.writeDocuments(-1L);
assertEquals("One document should be written.", 1,
this.documentStore.getPerformedStoreOperations());
DetailsDocument document = this.documentStore.getDocument(
DetailsDocument.class, GABELMOO_FINGERPRINT);
assertNotNull("There should be a document for the given fingerprint.",
document);
assertEquals("gabelmoo@torproject.org ( 2+6)",
document.getContact());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment