Skip to content

Commit

Permalink
SAK-33684 Handle SMTP FROM that isn’t email better (sakaiproject#5080)
Browse files Browse the repository at this point in the history
Have better handling of a client attempting to send an email where the FROM address isn’t an email address that we can parse. This may be because it’s empty or it’s not an email address.
  • Loading branch information
buckett authored and ottenhoff committed Dec 12, 2017
1 parent ab9b000 commit c05438c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ public MessageHandler create(MessageContext ctx) {

@Override
public void from(String from) throws RejectException {
SplitEmailAddress address = SplitEmailAddress.parse(from);
this.from = address.getLocal() + "@" + address.getDomain();
try {
SplitEmailAddress address = SplitEmailAddress.parse(from);
this.from = address.getLocal() + "@" + address.getDomain();
} catch (IllegalArgumentException iae) {
log.debug("Not allowing return path of: {}", from);
throw new RejectException("Not allowing return path of: "+ from);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String getDomain() {
public static SplitEmailAddress parse(String address) {
int atPos = address.indexOf('@');
if (atPos < 1 || atPos == address.length() -1) {
throw new IllegalArgumentException("Can't find @ or it's at the start or end.");
throw new IllegalArgumentException("Can't find @ or it's at the start or end of: "+ address);
}
String local = address.substring(0, atPos);
Matcher rfcMatcher = BATV_RFC.matcher(local);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,22 @@ public void testNoPermission() throws Exception {
writeData(client, "/no-permission.txt");
}

@Test(expected = SMTPException.class)
public void testNoReturnPath() throws Exception {
// Here there isn't a good email in the from address to make a return path
String reference = "/mailarchive/no-good-mail/main";
SmartClient client = createClient();
client.from("");
}

@Test(expected = SMTPException.class)
public void testEmptyReturnPath() throws Exception {
// Here there isn't a good email in the from address to make a return path
String reference = "/mailarchive/empty-return-path/main";
SmartClient client = createClient();
client.from("");
}

@Test
public void testPostmaster() throws Exception {
// Here there is no postmaster which may happen and should be dealt with
Expand Down

0 comments on commit c05438c

Please sign in to comment.