Skip to content

Commit

Permalink
Amend the check on IllegalAttachmentFileNameException
Browse files Browse the repository at this point in the history
  • Loading branch information
bhou committed May 3, 2024
1 parent 0dbdd3d commit ac667e5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.core.io.Resource;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand Down Expand Up @@ -94,9 +95,19 @@ public Set<URI> saveAttachments(
final long attachmentSize = attachment.contentLength();
final String filename = attachment.getFilename();

if (filename != null && (filename.contains("/") || filename.contains("\\"))) {
throw new IllegalAttachmentFileNameException("Attachment filename " + filename + " is illegal. "
+ "Filenames should not contain / or \\.");
if (filename != null) {
if ((filename.contains("/") || filename.contains("\\")
|| filename.equals(".") || filename.equals(".."))) {
throw new IllegalAttachmentFileNameException("Attachment filename " + filename + " is illegal. "
+ "Filenames should not be . or .., or contain /, \\.");
}

final File file = createTempFile(String.valueOf(attachmentsBasePath), filename);
if (!file.getAbsolutePath().startsWith(String.valueOf(attachmentsBasePath))
|| file.getAbsolutePath().equals(String.valueOf(attachmentsBasePath))) {
throw new IllegalAttachmentFileNameException("Attachment filename " + filename + " is illegal. "
+ "Filenames should not be a relative path.");
}
}

if (attachmentSize > this.attachmentServiceProperties.getMaxSize().toBytes()) {
Expand Down Expand Up @@ -124,4 +135,9 @@ public Set<URI> saveAttachments(

return setBuilder.build();
}

/* for testing purposes*/
File createTempFile(final String attachmentsBasePath, final String filename) {
return new File(attachmentsBasePath, filename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LocalFileSystemAttachmentServiceImplSpec extends Specification {
this.serviceProperties.setLocationPrefix(this.temporaryFolder.toUri())
this.serviceProperties.setMaxSize(DataSize.ofBytes(100))
this.serviceProperties.setMaxTotalSize(DataSize.ofBytes(150))
this.service = new LocalFileSystemAttachmentServiceImpl(serviceProperties)
this.service = Mockito.spy(new LocalFileSystemAttachmentServiceImpl(serviceProperties))
}

def "saveAttachments with no attachments"() {
Expand Down Expand Up @@ -179,4 +179,46 @@ class LocalFileSystemAttachmentServiceImplSpec extends Specification {
then:
thrown(IllegalAttachmentFileNameException)
}
def "reject attachments with illegal filename is ."() {
Set<Resource> attachments = new HashSet<Resource>()
Resource attachment = Mockito.mock(Resource.class)
Mockito.doReturn(".").when(attachment).getFilename()
attachments.add(attachment)
when:
service.saveAttachments(null, attachments)
then:
thrown(IllegalAttachmentFileNameException)
}
def "reject attachments with illegal filename is .."() {
Set<Resource> attachments = new HashSet<Resource>()
Resource attachment = Mockito.mock(Resource.class)
Mockito.doReturn("..").when(attachment).getFilename()
attachments.add(attachment)
when:
service.saveAttachments(null, attachments)
then:
thrown(IllegalAttachmentFileNameException)
}
def "reject attachments with illegal filename for base path check"() {
Set<Resource> attachments = new HashSet<Resource>()
Resource attachment = Mockito.mock(Resource.class)
Mockito.doReturn("file1.text").when(attachment).getFilename()
File file = Mockito.mock(File.class)
Mockito.doReturn("/a/b/c").when(file).getAbsolutePath()
Mockito.doReturn(file).when(service).createTempFile(Mockito.anyString(), Mockito.anyString());
attachments.add(attachment)
when:
service.saveAttachments(null, attachments)
then:
thrown(IllegalAttachmentFileNameException)
}
}

0 comments on commit ac667e5

Please sign in to comment.