Skip to content

Commit

Permalink
Add sanity check on attachment filename
Browse files Browse the repository at this point in the history
  • Loading branch information
bhou committed Apr 27, 2024
1 parent 26f3cc1 commit e07613b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.netflix.genie.common.internal.exceptions.unchecked.GenieJobNotFoundException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieJobSpecificationNotFoundException;
import com.netflix.genie.common.internal.exceptions.unchecked.GenieRuntimeException;
import com.netflix.genie.web.exceptions.checked.IllegalAttachmentFileNameException;
import com.netflix.genie.web.exceptions.checked.AttachmentTooLargeException;
import com.netflix.genie.web.exceptions.checked.IdAlreadyExistsException;
import com.netflix.genie.web.exceptions.checked.JobNotFoundException;
Expand Down Expand Up @@ -138,6 +139,8 @@ public ResponseEntity<GenieCheckedException> handleGenieCheckedException(final G
return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST);
} else if (e instanceof AttachmentTooLargeException) {
return new ResponseEntity<>(e, HttpStatus.PAYLOAD_TOO_LARGE);
} else if (e instanceof IllegalAttachmentFileNameException) {
return new ResponseEntity<>(e, HttpStatus.BAD_REQUEST);
} else {
return new ResponseEntity<>(e, HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.netflix.genie.web.exceptions.checked;

/**
* Exception thrown when the attachment filename is illegal.
*
* @author bhou
*/
public class IllegalAttachmentFileNameException extends SaveAttachmentException {
/**
* Constructor.
*/
public IllegalAttachmentFileNameException() {
super();
}

/**
* Constructor.
*
* @param message The detail message
*/
public IllegalAttachmentFileNameException(final String message) {
super(message);
}

/**
* Constructor.
*
* @param message The detail message
* @param cause The root cause of this exception
*/
public IllegalAttachmentFileNameException(final String message, final Throwable cause) {
super(message, cause);
}

/**
* Constructor.
*
* @param cause The root cause of this exception
*/
public IllegalAttachmentFileNameException(final Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.netflix.genie.web.exceptions.checked.IllegalAttachmentFileNameException;
import com.netflix.genie.web.exceptions.checked.AttachmentTooLargeException;
import com.netflix.genie.web.exceptions.checked.SaveAttachmentException;
import com.netflix.genie.web.properties.AttachmentServiceProperties;
Expand Down Expand Up @@ -93,6 +94,11 @@ public Set<URI> saveAttachments(
final long attachmentSize = attachment.contentLength();
final String filename = attachment.getFilename();

if (filename != null && filename.contains("/")) {
throw new IllegalAttachmentFileNameException("Attachment filename " + filename + " is illegal. "
+ "It should not contain /");
}

if (attachmentSize > this.attachmentServiceProperties.getMaxSize().toBytes()) {
throw new AttachmentTooLargeException("Attachment is too large: " + filename);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GenieWebCheckedExceptionsSpec extends Specification {
exceptionClass | _
AgentLaunchException | _
AttachmentTooLargeException | _
IllegalAttachmentFileNameException | _
IdAlreadyExistsException | _
JobDirectoryManifestNotFoundException | _
JobNotArchivedException | _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
package com.netflix.genie.web.services.impl

import com.google.common.collect.Sets
import com.netflix.genie.web.exceptions.checked.IllegalAttachmentFileNameException
import com.netflix.genie.web.exceptions.checked.AttachmentTooLargeException
import com.netflix.genie.web.exceptions.checked.SaveAttachmentException
import com.netflix.genie.web.properties.AttachmentServiceProperties
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.RandomStringUtils
import org.mockito.Mockito
import org.springframework.core.io.FileSystemResource
import org.springframework.core.io.Resource
import org.springframework.util.unit.DataSize
Expand Down Expand Up @@ -151,4 +153,17 @@ class LocalFileSystemAttachmentServiceImplSpec extends Specification {
then:
thrown(SaveAttachmentException)
}
def "reject attachments with illegal filename"() {
Set<Resource> attachments = Sets.newHashSet()
Resource attachment = Mock(Resource)
Mockito.doReturn("../../../root/breakout.file").when(attachment).getFilename()
attachments.add(attachment)
when:
service.saveAttachments(null, attachments)
then:
thrown(IllegalAttachmentFileNameException)
}
}

0 comments on commit e07613b

Please sign in to comment.