Skip to content

Commit

Permalink
[7.0.0] Emit actual hash in same format as expected hash (#20189)
Browse files Browse the repository at this point in the history
Work towards #17803

Closes #20155.

Commit
651c185

PiperOrigin-RevId: 582237355
Change-Id: I65dfabe0b3a7a87c5d85a9d0383d3831bbb56590

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
  • Loading branch information
bazel-io and fmeum authored Nov 14, 2023
1 parent d2ca99b commit 191824c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,26 @@ private InvalidChecksumException(String msg) {

private final KeyType keyType;
private final HashCode hashCode;
private final boolean useSubresourceIntegrity;

private Checksum(KeyType keyType, HashCode hashCode) {
private Checksum(KeyType keyType, HashCode hashCode, boolean useSubresourceIntegrity) {
this.keyType = keyType;
this.hashCode = hashCode;
this.useSubresourceIntegrity = useSubresourceIntegrity;
}

/** Constructs a new Checksum for a given key type and hash, in hex format. */
public static Checksum fromString(KeyType keyType, String hash) throws InvalidChecksumException {
return fromString(keyType, hash, /* useSubresourceIntegrity= */ false);
}

private static Checksum fromString(KeyType keyType, String hash, boolean useSubresourceIntegrity)
throws InvalidChecksumException {
if (!keyType.isValid(hash)) {
throw new InvalidChecksumException(keyType, hash);
}
return new Checksum(keyType, HashCode.fromString(Ascii.toLowerCase(hash)));
return new Checksum(
keyType, HashCode.fromString(Ascii.toLowerCase(hash)), useSubresourceIntegrity);
}

/** Constructs a new Checksum from a hash in Subresource Integrity format. */
Expand Down Expand Up @@ -89,14 +97,19 @@ public static Checksum fromSubresourceIntegrity(String integrity)
"Invalid " + keyType + " SRI checksum '" + integrity + "'");
}

return Checksum.fromString(keyType, HashCode.fromBytes(hash).toString());
return Checksum.fromString(
keyType, HashCode.fromBytes(hash).toString(), /* useSubresourceIntegrity= */ true);
}

public String toSubresourceIntegrity() {
private static String toSubresourceIntegrity(KeyType keyType, HashCode hashCode) {
String encoded = Base64.getEncoder().encodeToString(hashCode.asBytes());
return keyType.getHashName() + "-" + encoded;
}

public String toSubresourceIntegrity() {
return toSubresourceIntegrity(keyType, hashCode);
}

@Override
public String toString() {
return hashCode.toString();
Expand All @@ -109,4 +122,12 @@ public HashCode getHashCode() {
public KeyType getKeyType() {
return keyType;
}

public String emitOtherHashInSameFormat(HashCode otherHash) {
if (useSubresourceIntegrity) {
return toSubresourceIntegrity(keyType, otherHash);
} else {
return otherHash.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ final class HashInputStream extends InputStream {

private final InputStream delegate;
private final Hasher hasher;
private final HashCode code;
private final Checksum checksum;
@Nullable private volatile HashCode actual;

HashInputStream(@WillCloseWhenClosed InputStream delegate, Checksum checksum) {
this.delegate = delegate;
this.hasher = checksum.getKeyType().newHasher();
this.code = checksum.getHashCode();
this.checksum = checksum;
}

@Override
Expand Down Expand Up @@ -83,9 +83,12 @@ private void check() throws IOException {
if (actual == null) {
actual = hasher.hash();
}
if (!code.equals(actual)) {
if (!checksum.getHashCode().equals(actual)) {
throw new UnrecoverableHttpException(
String.format("Checksum was %s but wanted %s", actual, code));
String.format(
"Checksum was %s but wanted %s",
checksum.emitOtherHashInSameFormat(actual),
checksum.emitOtherHashInSameFormat(checksum.getHashCode())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public final class HashOutputStream extends OutputStream {

private final OutputStream delegate;
private final Hasher hasher;
private final HashCode code;
private final Checksum checksum;
@Nullable private volatile HashCode actual;

public HashOutputStream(@WillCloseWhenClosed OutputStream delegate, Checksum checksum) {
this.delegate = delegate;
this.hasher = checksum.getKeyType().newHasher();
this.code = checksum.getHashCode();
this.checksum = checksum;
}

@Override
Expand Down Expand Up @@ -81,9 +81,12 @@ private void check() throws IOException {
if (actual == null) {
actual = hasher.hash();
}
if (!code.equals(actual)) {
if (!checksum.getHashCode().equals(actual)) {
throw new UnrecoverableHttpException(
String.format("Checksum was %s but wanted %s", actual, code));
String.format(
"Checksum was %s but wanted %s",
checksum.emitOtherHashInSameFormat(actual),
checksum.emitOtherHashInSameFormat(checksum.getHashCode())));
}
}
}
3 changes: 2 additions & 1 deletion src/test/shell/bazel/external_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ function test_integrity_incorrect() {
create_workspace_with_default_repos WORKSPACE
touch BUILD
zip -r repo.zip *
integrity="sha256-$(cat repo.zip | openssl dgst -sha256 -binary | openssl base64 -A)"
startup_server $PWD
cd -

Expand All @@ -1118,7 +1119,7 @@ EOF
bazel build @repo//... &> $TEST_log 2>&1 && fail "Expected to fail"
expect_log "Error downloading \\[http://127.0.0.1:$fileserver_port/repo.zip\\] to"
# Bazel translates the integrity value back to the sha256 checksum.
expect_log "but wanted 61a6f762aaf60652cbf332879b8dcc2cfd81be2129a061da957d039eae77f0b0"
expect_log "Checksum was $integrity but wanted sha256-Yab3Yqr2BlLL8zKHm43MLP2BviEpoGHalX0Dnq538LA="
shutdown_server
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/bazel/external_patching_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ EOF

bazel build @ext//... &> $TEST_log 2>&1 && fail "Expected to fail"
expect_log "Error downloading \\[.*/remote.patch\\] to"
expect_log "but wanted 61a6f762aaf60652cbf332879b8dcc2cfd81be2129a061da957d039eae77f0b0"
expect_log "but wanted sha256-Yab3Yqr2BlLL8zKHm43MLP2BviEpoGHalX0Dnq538LA="
}

test_remote_patches_with_same_base_name() {
Expand Down

0 comments on commit 191824c

Please sign in to comment.