Skip to content

Commit

Permalink
Quota resource invalid limit closes connection (backport, fixes #508)
Browse files Browse the repository at this point in the history
Backport of #506
  • Loading branch information
marcelmay committed Dec 14, 2022
1 parent 0b66df3 commit 366e481
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ public void setFlag(String flagString, Flags flags) {
*/
public long number(ImapRequestLineReader request) throws ProtocolException {
String digits = consumeWord(request, new DigitCharValidator());
return Long.parseLong(digits);
try {
return Long.parseLong(digits);
} catch (NumberFormatException ex) {
throw new ProtocolException("Can not parse '" + digits + "' as number", ex);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,32 @@ protected void doProcess(final ImapRequestLineReader request, final ImapResponse
Quota quota = new Quota(root);
parser.consumeChar(request, ' ');
parser.consumeChar(request, '(');
quota.setResourceLimit(parser.astring(request), parser.consumeLong(request));
parseAndUpdateResourceLimit(request, quota);
char c =request.nextWordChar();
if(')' != c) {
quota.setResourceLimit(parser.astring(request), parser.consumeLong(request));
parseAndUpdateResourceLimit(request, quota);
}
parser.consumeChar(request, ')');
session.getHost().getStore().setQuota(
quota, session.getUser().getQualifiedMailboxName());
response.commandComplete(this);
} catch (ProtocolException e) {
response.commandFailed(this, "Can not parse command"+e.getMessage());
response.commandFailed(this,
"Can not parse command " + getName() +": " + e.getMessage());
}
}

private void parseAndUpdateResourceLimit(ImapRequestLineReader request, Quota quota) throws ProtocolException {
final String astring = parser.astring(request);
try {
String value = parser.atomOnly(request);
final long limit = Long.parseLong(value);
if(limit<0) {
throw new ProtocolException("Expected number (positive integer) but got "+limit);
}
quota.setResourceLimit(astring, limit);
} catch(ProtocolException|NumberFormatException ex) {
throw new ProtocolException("Failed to parse quota " + quota.quotaRoot+" resource limit "+astring+" value: "+ex.getMessage(), ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ public void testQuota() throws Exception {
}
}

@Test
public void testQuotaInvalidResourceLimit() throws Exception {
greenMail.setUser("foo@localhost", "pwd");

try (IMAPStore store = greenMail.getImap().createStore()) {
store.connect("foo@localhost", "pwd");
Quota testQuota = new Quota("INBOX");
testQuota.setResourceLimit("MESSAGES", -5L);
assertThatThrownBy(() -> store.setQuota(testQuota))
.hasMessageContaining("NO SETQUOTA failed. Can not parse command SETQUOTA: " +
"Failed to parse quota INBOX resource limit MESSAGES value:" +
" Expected number (positive integer) but got -5");
testQuota.setResourceLimit("MESSAGES", 5L);
store.setQuota(testQuota);
}
}

@Test
public void testQuotaCapability() throws MessagingException {
greenMail.setUser("foo@localhost", "pwd");
Expand Down

0 comments on commit 366e481

Please sign in to comment.