Skip to content

Commit

Permalink
fix encoding of long terms in the subject (#284)
Browse files Browse the repository at this point in the history
For a long term with no whitespace the term was not
getting broken up and would be longer than allowed
by some mail systems causing the base64 encoded data
to show as the subject. This changes it to just wrap
based on the length and ignore whitespace.
  • Loading branch information
brharrington authored Nov 19, 2016
1 parent 79795ac commit 1dcbf8d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 52 deletions.
21 changes: 12 additions & 9 deletions iep-ses/src/main/java/com/netflix/iep/ses/EncodingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,23 @@ static String wrap(String s, int offset, int maxLength) {
static String wrapBase64(String s, int offset, int maxLength) {
String prefix = "=?UTF-8?B?";
String suffix = "?=";
String wrapped = wrap(s, offset, maxLength - prefix.length() - suffix.length());
String[] lines = wrapped.split("[\r\n]+");

int length = maxLength - prefix.length() - suffix.length() - 1;
int start = 0;
int end = length - offset;
StringBuilder builder = new StringBuilder();
builder.append(prefix)
.append(base64(lines[0].getBytes(StandardCharsets.UTF_8)))
.append(suffix);
for (int i = 1; i < lines.length; ++i) {
do {
String part = s.substring(start, Math.min(end, s.length()));
builder.append(EncodingUtils.CRLF)
.append(' ')
.append(prefix)
.append(base64(lines[i].getBytes(StandardCharsets.UTF_8)))
.append(base64(part.getBytes(StandardCharsets.UTF_8)))
.append(suffix);
}
return builder.toString();
start = end;
end += length;
} while (start < s.length());

return builder.toString().trim();
}

/** Base64 encode the input data. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ public void tooLongSubject() throws IOException {
.withTextBody("Body of the email message."));
}

@Test
public void tooLongSubjectNoSpace() throws IOException {
// https://tools.ietf.org/html/rfc5322#section-2.1.1
String base = "Too_long_subject_no_space__";
String subject = base;
while (subject.length() < 256) {
subject += base;
}
checkMessage("tooLongSubjectNoSpace", new EmailRequestBuilder()
.withSource(FROM)
.withToAddresses(TO)
.withSubject(subject)
.withTextBody("Body of the email message."));
}

@Test
public void subjectWithUtf8() throws IOException {
String subject = "警报";
Expand Down
19 changes: 9 additions & 10 deletions iep-ses/src/test/resources/longSubject
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ Content-Type: multipart/mixed;
boundary="331239ab-8a31-4cc6-84d6-5557f96ebc3a"
From: bob@example.com
To: andrew@example.com
Subject: =?UTF-8?B?VGVzdCBtZXNzYWdlIHdpdGggYSBsb25n?=
=?UTF-8?B?IHJlcGVhdGVkIHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRo?=
=?UTF-8?B?IGEgbG9uZyByZXBlYXRlZCBzdWJqZWN0LiBUZXN0?=
=?UTF-8?B?IG1lc3NhZ2Ugd2l0aCBhIGxvbmcgcmVwZWF0ZWQ=?=
=?UTF-8?B?IHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRoIGEgbG9uZw==?=
=?UTF-8?B?IHJlcGVhdGVkIHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRo?=
=?UTF-8?B?IGEgbG9uZyByZXBlYXRlZCBzdWJqZWN0LiBUZXN0?=
=?UTF-8?B?IG1lc3NhZ2Ugd2l0aCBhIGxvbmcgcmVwZWF0ZWQ=?=
=?UTF-8?B?IHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRoIGEgbG9uZw==?=
=?UTF-8?B?IHJlcGVhdGVkIHN1YmplY3Qu?=
Subject: =?UTF-8?B?VGVzdCBtZXNzYWdlIHdpdGggYSBsb25nIHJlcA==?=
=?UTF-8?B?ZWF0ZWQgc3ViamVjdC4gVGVzdCBtZXNzYWdlIHdpdGggYSBsbw==?=
=?UTF-8?B?bmcgcmVwZWF0ZWQgc3ViamVjdC4gVGVzdCBtZXNzYWdlIHdpdA==?=
=?UTF-8?B?aCBhIGxvbmcgcmVwZWF0ZWQgc3ViamVjdC4gVGVzdCBtZXNzYQ==?=
=?UTF-8?B?Z2Ugd2l0aCBhIGxvbmcgcmVwZWF0ZWQgc3ViamVjdC4gVGVzdA==?=
=?UTF-8?B?IG1lc3NhZ2Ugd2l0aCBhIGxvbmcgcmVwZWF0ZWQgc3ViamVjdA==?=
=?UTF-8?B?LiBUZXN0IG1lc3NhZ2Ugd2l0aCBhIGxvbmcgcmVwZWF0ZWQgcw==?=
=?UTF-8?B?dWJqZWN0LiBUZXN0IG1lc3NhZ2Ugd2l0aCBhIGxvbmcgcmVwZQ==?=
=?UTF-8?B?YXRlZCBzdWJqZWN0LiA=?=

--331239ab-8a31-4cc6-84d6-5557f96ebc3a
Content-Type: text/plain; charset=UTF-8
Expand Down
62 changes: 29 additions & 33 deletions iep-ses/src/test/resources/tooLongSubject
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,35 @@ Content-Type: multipart/mixed;
boundary="331239ab-8a31-4cc6-84d6-5557f96ebc3a"
From: bob@example.com
To: andrew@example.com
Subject: =?UTF-8?B?VGVzdCBtZXNzYWdlIHdpdGggYSBsb25n?=
=?UTF-8?B?IHJlcGVhdGVkIHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRo?=
=?UTF-8?B?IGEgbG9uZyByZXBlYXRlZCBzdWJqZWN0LiBUZXN0?=
=?UTF-8?B?IG1lc3NhZ2Ugd2l0aCBhIGxvbmcgcmVwZWF0ZWQ=?=
=?UTF-8?B?IHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRoIGEgbG9uZw==?=
=?UTF-8?B?IHJlcGVhdGVkIHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRo?=
=?UTF-8?B?IGEgbG9uZyByZXBlYXRlZCBzdWJqZWN0LiBUZXN0?=
=?UTF-8?B?IG1lc3NhZ2Ugd2l0aCBhIGxvbmcgcmVwZWF0ZWQ=?=
=?UTF-8?B?IHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRoIGEgbG9uZw==?=
=?UTF-8?B?IHJlcGVhdGVkIHN1YmplY3QuIFRlc3QgbWVzc2FnZSB3aXRo?=
=?UTF-8?B?IGEgbG9uZyByZXBlYXRlZCBzdWJqZWN0Li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-Type: text/plain; charset=UTF-8
Expand Down
20 changes: 20 additions & 0 deletions iep-ses/src/test/resources/tooLongSubjectNoSpace
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="331239ab-8a31-4cc6-84d6-5557f96ebc3a"
From: bob@example.com
To: andrew@example.com
Subject: =?UTF-8?B?VG9vX2xvbmdfc3ViamVjdF9ub19zcGFjZV9fVA==?=
=?UTF-8?B?b29fbG9uZ19zdWJqZWN0X25vX3NwYWNlX19Ub29fbG9uZ19zdQ==?=
=?UTF-8?B?YmplY3Rfbm9fc3BhY2VfX1Rvb19sb25nX3N1YmplY3Rfbm9fcw==?=
=?UTF-8?B?cGFjZV9fVG9vX2xvbmdfc3ViamVjdF9ub19zcGFjZV9fVG9vXw==?=
=?UTF-8?B?bG9uZ19zdWJqZWN0X25vX3NwYWNlX19Ub29fbG9uZ19zdWJqZQ==?=
=?UTF-8?B?Y3Rfbm9fc3BhY2VfX1Rvb19sb25nX3N1YmplY3Rfbm9fc3BhYw==?=
=?UTF-8?B?ZV9fVG9vX2xvbmdfc3ViamVjdF9ub19zcGFjZV9fVG9vX2xvbg==?=
=?UTF-8?B?Z19zdWJqZWN0X25vX3NwYWNlX18=?=

--331239ab-8a31-4cc6-84d6-5557f96ebc3a
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

Qm9keSBvZiB0aGUgZW1haWwgbWVzc2FnZS4=
--331239ab-8a31-4cc6-84d6-5557f96ebc3a--

0 comments on commit 1dcbf8d

Please sign in to comment.