Skip to content

Commit 8856793

Browse files
committed
MultiPartEmail attach methods now throw an IOException if the given file
is a directory
1 parent bbae17c commit 8856793

6 files changed

Lines changed: 69 additions & 28 deletions

File tree

commons-email2-core/src/main/java/org/apache/commons/mail2/core/EmailUtils.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717

1818
package org.apache.commons.mail2.core;
1919

20+
import java.io.File;
21+
import java.io.IOException;
2022
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
2125
import java.util.BitSet;
2226
import java.util.Collection;
2327
import java.util.Locale;
2428
import java.util.Map;
29+
import java.util.Objects;
2530
import java.util.Random;
2631

2732
/**
@@ -86,6 +91,44 @@ public final class EmailUtils {
8691
SAFE_URL.set('@');
8792
}
8893

94+
/**
95+
* Checks that the given file exists and is not a directory.
96+
*
97+
* @param file the file to check.
98+
* @return the given file if it exists and is not a directory.
99+
* @throws IOException if the given file does not exist or is a directory.
100+
* @since 2.0.0-M2
101+
*/
102+
public static File check(final File file) throws IOException {
103+
Objects.requireNonNull(file, "file");
104+
if (!file.exists()) {
105+
throw new IOException("\"" + file + "\" does not exist");
106+
}
107+
if (file.isDirectory()) {
108+
throw new IOException("\"" + file + "\" is a directory");
109+
}
110+
return file;
111+
}
112+
113+
/**
114+
* Checks that the given file exists and is not a directory.
115+
*
116+
* @param file the file to check.
117+
* @return the given file if it exists and is not a directory.
118+
* @throws IOException if the given file does not exist or is a directory.
119+
* @since 2.0.0-M2
120+
*/
121+
public static Path check(final Path file) throws IOException {
122+
Objects.requireNonNull(file, "file");
123+
if (!Files.exists(file)) {
124+
throw new IOException("\"" + file + "\" does not exist");
125+
}
126+
if (Files.isDirectory(file)) {
127+
throw new IOException("\"" + file + "\" is a directory");
128+
}
129+
return file;
130+
}
131+
89132
/**
90133
* Encodes an input string according to RFC 2392. Unsafe characters are escaped.
91134
*

commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/MultiPartEmail.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.InputStream;
2222
import java.io.UnsupportedEncodingException;
2323
import java.net.URL;
24-
import java.nio.file.Files;
2524
import java.nio.file.OpenOption;
2625
import java.nio.file.Path;
2726
import java.util.Objects;
@@ -202,10 +201,7 @@ public MultiPartEmail attach(final EmailAttachment attachment) throws EmailExcep
202201
String fileName = null;
203202
try {
204203
fileName = attachment.getPath();
205-
final File file = new File(fileName);
206-
if (!file.exists()) {
207-
throw new IOException("\"" + fileName + "\" does not exist");
208-
}
204+
final File file = EmailUtils.check(new File(fileName));
209205
result = attach(new FileDataSource(file), attachment.getName(), attachment.getDescription(), attachment.getDisposition());
210206
} catch (final IOException e) {
211207
throw new EmailException("Cannot attach file \"" + fileName + "\"", e);
@@ -225,14 +221,11 @@ public MultiPartEmail attach(final EmailAttachment attachment) throws EmailExcep
225221
* @since 1.3
226222
*/
227223
public MultiPartEmail attach(final File file) throws EmailException {
228-
final String fileName = file.getAbsolutePath();
229224
try {
230-
if (!file.exists()) {
231-
throw new IOException("\"" + fileName + "\" does not exist");
232-
}
225+
EmailUtils.check(file);
233226
return attach(new FileDataSource(file), file.getName(), null, EmailAttachment.ATTACHMENT);
234227
} catch (final IOException e) {
235-
throw new EmailException("Cannot attach file \"" + fileName + "\"", e);
228+
throw new EmailException("Cannot attach file \"" + file + "\"", e);
236229
}
237230
}
238231

@@ -248,9 +241,7 @@ public MultiPartEmail attach(final File file) throws EmailException {
248241
public MultiPartEmail attach(final Path file, final OpenOption... options) throws EmailException {
249242
final Path fileName = file.toAbsolutePath();
250243
try {
251-
if (!Files.exists(file)) {
252-
throw new IOException("\"" + fileName + "\" does not exist");
253-
}
244+
EmailUtils.check(file);
254245
return attach(new PathDataSource(file, FileTypeMap.getDefaultFileTypeMap(), options), Objects.toString(file.getFileName(), null), null,
255246
EmailAttachment.ATTACHMENT);
256247
} catch (final IOException e) {

commons-email2-jakarta/src/test/java/org/apache/commons/mail2/jakarta/MultiPartEmailTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ void testAttachFile() throws Exception {
136136
final EmailAttachment attachment4 = new EmailAttachment();
137137
attachment4.setPath("");
138138
assertThrows(EmailException.class, () -> email.attach(attachment4));
139+
attachment4.setPath("ThisFileDoesNotExist.txt");
140+
assertThrows(EmailException.class, () -> email.attach(attachment4));
141+
attachment4.setPath("target"); // a directory, not a file
142+
assertThrows(EmailException.class, () -> email.attach(attachment4));
139143
}
140144

141145
@Test
@@ -179,6 +183,10 @@ void testAttachPath() throws Exception {
179183
final EmailAttachment attachment4 = new EmailAttachment();
180184
attachment4.setPath("");
181185
assertThrows(EmailException.class, () -> email.attach(attachment4));
186+
attachment4.setPath("ThisFileDoesNotExist.txt");
187+
assertThrows(EmailException.class, () -> email.attach(attachment4));
188+
attachment4.setPath("target"); // a directory, not a file
189+
assertThrows(EmailException.class, () -> email.attach(attachment4));
182190
}
183191

184192
/** TODO implement test for GetContainer */

commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/MultiPartEmail.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.InputStream;
2222
import java.io.UnsupportedEncodingException;
2323
import java.net.URL;
24-
import java.nio.file.Files;
2524
import java.nio.file.OpenOption;
2625
import java.nio.file.Path;
2726
import java.util.Objects;
@@ -202,7 +201,7 @@ public MultiPartEmail attach(final EmailAttachment attachment) throws EmailExcep
202201
String fileName = null;
203202
try {
204203
fileName = attachment.getPath();
205-
final File file = new File(fileName);
204+
final File file = EmailUtils.check(new File(fileName));
206205
if (!file.exists()) {
207206
throw new IOException("\"" + fileName + "\" does not exist");
208207
}
@@ -225,14 +224,11 @@ public MultiPartEmail attach(final EmailAttachment attachment) throws EmailExcep
225224
* @since 1.3
226225
*/
227226
public MultiPartEmail attach(final File file) throws EmailException {
228-
final String fileName = file.getAbsolutePath();
229227
try {
230-
if (!file.exists()) {
231-
throw new IOException("\"" + fileName + "\" does not exist");
232-
}
228+
EmailUtils.check(file);
233229
return attach(new FileDataSource(file), file.getName(), null, EmailAttachment.ATTACHMENT);
234230
} catch (final IOException e) {
235-
throw new EmailException("Cannot attach file \"" + fileName + "\"", e);
231+
throw new EmailException("Cannot attach file \"" + file + "\"", e);
236232
}
237233
}
238234

@@ -246,15 +242,12 @@ public MultiPartEmail attach(final File file) throws EmailException {
246242
* @since 1.6.0
247243
*/
248244
public MultiPartEmail attach(final Path file, final OpenOption... options) throws EmailException {
249-
final Path fileName = file.toAbsolutePath();
250245
try {
251-
if (!Files.exists(file)) {
252-
throw new IOException("\"" + fileName + "\" does not exist");
253-
}
246+
EmailUtils.check(file);
254247
return attach(new PathDataSource(file, FileTypeMap.getDefaultFileTypeMap(), options), Objects.toString(file.getFileName(), null), null,
255248
EmailAttachment.ATTACHMENT);
256249
} catch (final IOException e) {
257-
throw new EmailException("Cannot attach file \"" + fileName + "\"", e);
250+
throw new EmailException("Cannot attach file \"" + file + "\"", e);
258251
}
259252
}
260253

@@ -307,7 +300,6 @@ public void buildMimeMessage() throws EmailException {
307300
// before a multipart message can be sent, we must make sure that
308301
// the content for the main body part was actually set. If not,
309302
// an IOException will be thrown during super.send().
310-
311303
final BodyPart body = getPrimaryBodyPart();
312304
try {
313305
body.getContent();
@@ -318,11 +310,9 @@ public void buildMimeMessage() throws EmailException {
318310
// throw new EmailException(e);
319311
}
320312
}
321-
322313
if (subType != null) {
323314
getContainer().setSubType(subType);
324315
}
325-
326316
super.buildMimeMessage();
327317
} catch (final MessagingException e) {
328318
throw new EmailException(e);

commons-email2-javax/src/test/java/org/apache/commons/mail2/javax/MultiPartEmailTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ void testAttachFile() throws Exception {
136136
final EmailAttachment attachment4 = new EmailAttachment();
137137
attachment4.setPath("");
138138
assertThrows(EmailException.class, () -> email.attach(attachment4));
139+
attachment4.setPath("ThisFileDoesNotExist.txt");
140+
assertThrows(EmailException.class, () -> email.attach(attachment4));
141+
attachment4.setPath("target"); // a directory, not a file
142+
assertThrows(EmailException.class, () -> email.attach(attachment4));
139143
}
140144

141145
@Test
@@ -179,6 +183,10 @@ void testAttachPath() throws Exception {
179183
final EmailAttachment attachment4 = new EmailAttachment();
180184
attachment4.setPath("");
181185
assertThrows(EmailException.class, () -> email.attach(attachment4));
186+
attachment4.setPath("ThisFileDoesNotExist.txt");
187+
assertThrows(EmailException.class, () -> email.attach(attachment4));
188+
attachment4.setPath("target"); // a directory, not a file
189+
assertThrows(EmailException.class, () -> email.attach(attachment4));
182190
}
183191

184192
/** TODO implement test for GetContainer */

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<!-- FIX -->
2828
<action type="update" due-to="Derek Wickern, Gary Gregory" dev="ggregory">Handle IllegalArgumentException thrown for invalid email address #328.</action>
2929
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Apache RAT plugin console warnings.</action>
30+
<action type="fix" dev="ggregory" due-to="Gary Gregory, Henri Cook, Sebb">MultiPartEmail attach methods now throw an IOException if the given file is a directory.</action>
3031
<!-- UPDATE -->
3132
<action type="update" due-to="Gary Gregory, Dependabot" dev="ggregory">Bump commons-parent from 72 to 96 #279, #293, #297, #304, #350, #376, #386, #390, #392.</action>
3233
<action type="update" due-to="Gary Gregory" dev="ggregory">Bump org.mockito:mockito-core from 5.13.0 to 5.21.0 #290, #296, #302, #319, #336, #338, #344, #353, #366.</action>

0 commit comments

Comments
 (0)