Skip to content

Commit

Permalink
Improved validateUploadRequest and applied after tmp directory was cr…
Browse files Browse the repository at this point in the history
…eated. (#5818)

Co-authored-by: Corneil du Plessis <[email protected]>
  • Loading branch information
onobc and corneil authored May 17, 2024
1 parent 469025f commit 2ac9bfa
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,20 @@ public void delete(PackageMetadata packageMetadata) {

@Transactional
public PackageMetadata upload(UploadRequest uploadRequest) {
validateUploadRequest(uploadRequest);

Repository localRepositoryToUpload = getRepositoryToUpload(uploadRequest.getRepoName());
Path packageDirPath = null;
try {
packageDirPath = TempFileUtils.createTempDirectory("skipperUpload");
validateUploadRequest(packageDirPath, uploadRequest);
File packageDir = new File(packageDirPath + File.separator + uploadRequest.getName());
packageDir.mkdir();
Path packageFile = Paths
.get(packageDir.getPath() + File.separator + uploadRequest.getName() + "-"
+ uploadRequest.getVersion() + "." + uploadRequest.getExtension());
String fullName = uploadRequest.getName().trim() + "-" + uploadRequest.getVersion().trim() + "." + uploadRequest.getExtension().trim();
Path packageFile = Paths.get(packageDir.getPath() + File.separator + fullName);
Assert.isTrue(packageDir.exists(), "Package directory doesn't exist.");
Files.write(packageFile, uploadRequest.getPackageFileAsBytes());
ZipUtil.unpack(packageFile.toFile(), packageDir);
String unzippedPath = packageDir.getAbsolutePath() + File.separator + uploadRequest.getName()
+ "-" + uploadRequest.getVersion();
String unzippedPath = packageDir.getAbsolutePath() + File.separator + uploadRequest.getName() + "-" + uploadRequest.getVersion();
File unpackagedFile = new File(unzippedPath);
Assert.isTrue(unpackagedFile.exists(), "Package is expected to be unpacked, but it doesn't exist");
Package packageToUpload = this.packageReader.read(unpackagedFile);
Expand Down Expand Up @@ -267,7 +266,7 @@ private Repository getRepositoryToUpload(String repoName) {
return localRepositoryToUpload;
}

private void validateUploadRequest(UploadRequest uploadRequest) {
private void validateUploadRequest(Path packageDirPath, UploadRequest uploadRequest) throws IOException {
Assert.notNull(uploadRequest.getRepoName(), "Repo name can not be null");
Assert.notNull(uploadRequest.getName(), "Name of package can not be null");
Assert.notNull(uploadRequest.getVersion(), "Version can not be null");
Expand All @@ -283,11 +282,16 @@ private void validateUploadRequest(UploadRequest uploadRequest) {
+ uploadRequest.getExtension());
Assert.notNull(uploadRequest.getPackageFileAsBytes(), "Package file as bytes must not be null");
Assert.isTrue(uploadRequest.getPackageFileAsBytes().length != 0, "Package file as bytes must not be empty");
File destinationFile = new File(packageDirPath.toFile(), uploadRequest.getName().trim());
String canonicalDestinationDirPath = packageDirPath.toFile().getCanonicalPath();
String canonicalDestinationFile = destinationFile.getCanonicalPath();
if (!canonicalDestinationFile.startsWith(canonicalDestinationDirPath + File.separator)) {
throw new SkipperException("Entry is outside of the target dir: " + uploadRequest.getName());
}
PackageMetadata existingPackageMetadata = this.packageMetadataRepository.findByRepositoryNameAndNameAndVersion(
uploadRequest.getRepoName().trim(), uploadRequest.getName().trim(), uploadRequest.getVersion().trim());
if (existingPackageMetadata != null) {
throw new SkipperException(String.format("Failed to upload the package. " + "" +
"Package [%s:%s] in Repository [%s] already exists.",
throw new SkipperException(String.format("Failed to upload the package. Package [%s:%s] in Repository [%s] already exists.",
uploadRequest.getName(), uploadRequest.getVersion(), uploadRequest.getRepoName().trim()));
}
}
Expand Down

0 comments on commit 2ac9bfa

Please sign in to comment.