Skip to content

Commit a4ae7b2

Browse files
committed
NET-614 IMAP fails to quote/encode mailbox names
NET-615 IMAPClient could simplify using empty arguments git-svn-id: https://svn.apache.org/repos/asf/commons/proper/net/trunk@1842969 13f79535-47bb-0310-9956-ffa450edef68
1 parent c4dd9fd commit a4ae7b2

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ This is mainly a bug-fix release. See further details below.
7474
The examples are not part of the public API, so this does not affect compatibility.
7575
7676
">
77+
<action issue="NET-615" type="add" dev="sebb">
78+
IMAPClient could simplify using empty arguments
79+
</action>
80+
<action issue="NET-614" type="add" dev="sebb">
81+
IMAP fails to quote/encode mailbox names
82+
</action>
7783
<action issue="NET-643" type="fix" dev="sebb" due-to="Vasily">
7884
NPE when closing telnet stream
7985
</action>

src/main/java/org/apache/commons/net/imap/IMAP.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,5 +467,36 @@ protected String generateCommandID()
467467
}
468468
return res;
469469
}
470+
471+
/**
472+
* Quote an input string if necessary.
473+
* If the string is enclosed in double-quotes it is assumed
474+
* to be quoted already and is returned unchanged.
475+
* If it is the empty string, "" is returned.
476+
* If it contains a space
477+
* then it is enclosed in double quotes, escaping the
478+
* characters backslash and double-quote.
479+
*
480+
* @param input the value to be quoted, may be null
481+
* @return the quoted value
482+
*/
483+
static String quoteString(String input) {
484+
if (input == null) { // Don't throw NPE here
485+
return null;
486+
}
487+
if (input.isEmpty()) {
488+
return "\"\""; // return the string ""
489+
}
490+
// Length check is necessary to ensure a lone double-quote is quoted
491+
if (input.length() > 1 && input.startsWith("\"") && input.endsWith("\"")) {
492+
return input; // Assume already quoted
493+
}
494+
if (input.contains(" ")) {
495+
// quoted strings must escape \ and "
496+
return "\"" + input.replaceAll("([\\\\\"])", "\\\\$1") + "\"";
497+
}
498+
return input;
499+
500+
}
470501
}
471502
/* kate: indent-width 4; replace-tabs on; */

src/main/java/org/apache/commons/net/imap/IMAPClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,32 @@ public boolean unsubscribe(String mailboxName) throws IOException
184184

185185
/**
186186
* Send a LIST command to the server.
187-
* @param refName The reference name.
187+
* Quotes the parameters if necessary.
188+
* @param refName The reference name
189+
* If empty, indicates that the mailbox name is interpreted as by SELECT.
188190
* @param mailboxName The mailbox name.
191+
* If empty, this is a special request to
192+
* return the hierarchy delimiter and the root name of the name given
193+
* in the reference
189194
* @return {@code true} if the command was successful,{@code false} if not.
190195
* @throws IOException If a network I/O error occurs.
191196
*/
192197
public boolean list(String refName, String mailboxName) throws IOException
193198
{
194-
return doCommand (IMAPCommand.LIST, refName + " " + mailboxName);
199+
return doCommand (IMAPCommand.LIST, quoteString(refName) + " " + quoteString(mailboxName));
195200
}
196201

197202
/**
198203
* Send an LSUB command to the server.
204+
* Quotes the parameters if necessary.
199205
* @param refName The reference name.
200206
* @param mailboxName The mailbox name.
201207
* @return {@code true} if the command was successful,{@code false} if not.
202208
* @throws IOException If a network I/O error occurs.
203209
*/
204210
public boolean lsub(String refName, String mailboxName) throws IOException
205211
{
206-
return doCommand (IMAPCommand.LSUB, refName + " " + mailboxName);
212+
return doCommand (IMAPCommand.LSUB, quoteString(refName) + " " + quoteString(mailboxName));
207213
}
208214

209215
/**

0 commit comments

Comments
 (0)