Skip to content

Commit ebacd54

Browse files
authored
Merge pull request #853 from sigstore/fix_tuf_target_paths
Handle targets with path elements
2 parents f525b83 + fe49ee8 commit ebacd54

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

sigstore-java/src/main/java/dev/sigstore/tuf/FileSystemTufStore.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import dev.sigstore.tuf.model.*;
2222
import java.io.BufferedWriter;
2323
import java.io.IOException;
24+
import java.net.URLEncoder;
25+
import java.nio.charset.StandardCharsets;
2426
import java.nio.file.Files;
2527
import java.nio.file.Path;
2628
import java.util.Optional;
@@ -65,12 +67,14 @@ public String getIdentifier() {
6567

6668
@Override
6769
public void writeTarget(String targetName, byte[] targetContents) throws IOException {
68-
Files.write(targetsCache.resolve(targetName), targetContents);
70+
var encoded = URLEncoder.encode(targetName, StandardCharsets.UTF_8);
71+
Files.write(targetsCache.resolve(encoded), targetContents);
6972
}
7073

7174
@Override
7275
public byte[] readTarget(String targetName) throws IOException {
73-
return Files.readAllBytes(targetsCache.resolve(targetName));
76+
var encoded = URLEncoder.encode(targetName, StandardCharsets.UTF_8);
77+
return Files.readAllBytes(targetsCache.resolve(encoded));
7478
}
7579

7680
@Override

sigstore-java/src/main/java/dev/sigstore/tuf/TargetReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
public interface TargetReader {
2222

2323
/**
24-
* Reads a TUF target file from the local TUF store
24+
* Reads a TUF target file from the local TUF store. Target names may include path elements and
25+
* the storage engine should be consistent when handling writing and reading these.
2526
*
2627
* @param targetName the name of the target file to read (e.g. ctfe.pub)
2728
* @return the content of the file as bytes

sigstore-java/src/main/java/dev/sigstore/tuf/TargetStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public interface TargetStore extends TargetReader {
2828
String getIdentifier();
2929

3030
/**
31-
* Writes a TUF target to the local target store.
31+
* Writes a TUF target to the local target store. Target names may include path elements and the
32+
* storage engine should be consistent when handling writing and reading these.
3233
*
3334
* @param targetName the name of the target file to write (e.g. ctfe.pub)
3435
* @param targetContents the content of the target file as bytes

sigstore-java/src/main/java/dev/sigstore/tuf/Updater.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import dev.sigstore.tuf.model.Timestamp;
2727
import dev.sigstore.tuf.model.TufMeta;
2828
import java.io.IOException;
29+
import java.nio.file.Paths;
2930
import java.security.InvalidKeyException;
3031
import java.security.NoSuchAlgorithmException;
3132
import java.security.SignatureException;
@@ -478,12 +479,25 @@ void downloadTargets(Targets targets)
478479
}
479480

480481
void downloadTarget(String targetName, TargetData targetData) throws IOException {
482+
var calculatedName = targetName;
483+
var calculatedPath = "";
484+
// if target name includes directories then we have to process the path
485+
if (targetName.contains("/")) {
486+
var targetPath = Paths.get(targetName);
487+
calculatedName = targetPath.getFileName().toString();
488+
calculatedPath = targetPath.getParent().toString();
489+
if (!calculatedPath.endsWith("/")) {
490+
calculatedPath = calculatedPath + "/";
491+
}
492+
}
481493
// 9) Download target up to length specified in bytes. verify against hash.
482494
String versionedTargetName;
483495
if (targetData.getHashes().getSha512() != null) {
484-
versionedTargetName = targetData.getHashes().getSha512() + "." + targetName;
496+
versionedTargetName =
497+
calculatedPath + targetData.getHashes().getSha512() + "." + calculatedName;
485498
} else {
486-
versionedTargetName = targetData.getHashes().getSha256() + "." + targetName;
499+
versionedTargetName =
500+
calculatedPath + targetData.getHashes().getSha256() + "." + calculatedName;
487501
}
488502

489503
var targetBytes = targetFetcher.fetchResource(versionedTargetName, targetData.getLength());

tuf-cli/tuf-cli.xfails

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
test_metadata_bytes_match
2-
test_client_downloads_expected_file_in_sub_dir
32
test_duplicate_sig_keyids
43
test_unusual_role_name[?]
54
test_unusual_role_name[#]

0 commit comments

Comments
 (0)