Skip to content

Commit b411532

Browse files
committed
Restrict File Endpoints from handling Metadata Files
1 parent ca40702 commit b411532

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

workspace-server/src/main/java/gov/nasa/jpl/aerie/workspace/server/WorkspaceBindings.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,16 @@ private HandlerResult handleFileUpload(
623623
boolean overwrite,
624624
final String userId) {
625625
try {
626+
// Verify the user isn't attempting to save a metadata file using the main file api
627+
if(RenderType.isAerieMetadataFile(uploadPath.getFileName().toString())) {
628+
return new HandlerResult(
629+
405,
630+
new FormattedError(
631+
new MalformedRequest("Could not save file.",
632+
"Metadata files may not be uploaded via the file API."
633+
+ " Use the metadata API (located at /metadata/{workspaceId}/<basefilepath>) instead.")));
634+
}
635+
626636
// Report a "Conflict" status if the file already exists and "overwrite" is false
627637
// "overwrite" defaults to "false" if unspecified
628638
if (workspaceService.checkFileExists(workspaceId, uploadPath) && !overwrite) {
@@ -684,6 +694,24 @@ private HandlerResult handleMove(
684694
"'%s' in Workspace %d moved to '%s' in Workspace %d"
685695
.formatted(toMove, sourceWorkspaceId, destinationPath, destinationWorkspaceId));
686696

697+
// Verify the user isn't attempting to move a metadata file using the main file api
698+
if(RenderType.isAerieMetadataFile(toMove.getFileName().toString())) {
699+
return new HandlerResult(
700+
405,
701+
new FormattedError(
702+
new MalformedRequest(
703+
errorMsg,
704+
"Metadata files may not be directly moved via the file API. Move the main file instead.")));
705+
}
706+
707+
// Verify the user isn't attempting to rename a non-metadata file to a metadata file
708+
if(RenderType.isAerieMetadataFile(destinationPath.getFileName().toString())) {
709+
return new HandlerResult(
710+
405,
711+
new FormattedError(
712+
new MalformedRequest(errorMsg, "Normal files may not be renamed to metadata files.")));
713+
}
714+
687715
if (!workspaceService.checkFileExists(sourceWorkspaceId, toMove)) {
688716
return new HandlerResult(
689717
404,
@@ -730,6 +758,24 @@ private HandlerResult handleCopy(
730758
"'%s' in Workspace %d copied to '%s' in Workspace %d"
731759
.formatted(toCopy, sourceWorkspaceId, destinationPath, destinationWorkspaceId));
732760

761+
// Verify the user isn't attempting to copy a metadata file using the main file api
762+
if(RenderType.isAerieMetadataFile(toCopy.getFileName().toString())) {
763+
return new HandlerResult(
764+
405,
765+
new FormattedError(
766+
new MalformedRequest(
767+
errorMsg,
768+
"Metadata files may not be directly copied via the file API. Copy the main file instead.")));
769+
}
770+
771+
// Verify the user isn't attempting to rename a non-metadata file to a metadata file
772+
if(RenderType.isAerieMetadataFile(destinationPath.getFileName().toString())) {
773+
return new HandlerResult(
774+
405,
775+
new FormattedError(
776+
new MalformedRequest(errorMsg, "Normal files may not be renamed to metadata files.")));
777+
}
778+
733779
if (!workspaceService.checkFileExists(sourceWorkspaceId, toCopy)) {
734780
return new HandlerResult(
735781
404,
@@ -764,6 +810,18 @@ private HandlerResult handleCopy(
764810
private HandlerResult handleDelete(int workspaceId, Path filePath) {
765811
try {
766812
final var errorMsg = "Could not delete %s.".formatted(filePath);
813+
814+
// Verify the user isn't attempting to delete a metadata file using the main file api
815+
if(RenderType.isAerieMetadataFile(filePath.getFileName().toString())) {
816+
return new HandlerResult(
817+
405,
818+
new FormattedError(
819+
new MalformedRequest(
820+
errorMsg,
821+
"Metadata files may not be directly deleted via the file API. "
822+
+ "Use the metadata API (located at /metadata/{workspaceId}/<basefilepath>) instead.")));
823+
}
824+
767825
if (!workspaceService.checkFileExists(workspaceId, filePath)) {
768826
return new HandlerResult(404, new FormattedError(filePath.getFileName() + " does not exist."));
769827
}

0 commit comments

Comments
 (0)