blob: 2adc17ab200eb2288b85035483a371f3e3759957 [file]
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "assertion/references_validator.h"
#include <memory>
#include <string>
#include "absl/log/check.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "assertion/hashed_uri_validator.h"
#include "crypto/default/hasher.h"
#include "crypto/hash.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "jumbf/box.h"
#include "jumbf/uri.h"
#include "proto/actions_assertion.pb.h"
#include "proto/assertion.pb.h"
#include "testing/jumbf_utils.h"
#include "testing/proto_test_utils.h"
#include "testing/test_validation_tracker.h"
namespace credentio {
namespace {
using ::credentio_testing::ParseTextProtoOrDie;
using ::testing::IsEmpty;
using ::testing::UnorderedElementsAre;
constexpr absl::string_view kManifestLabel1 =
"urn:uuid:F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4";
constexpr absl::string_view kManifestPath1 =
"/c2pa/urn:uuid:F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4";
constexpr absl::string_view kManifestLabel2 =
"urn:uuid:AFAFAFAF-0101-0202-0303-ABCDEFFEDCBA";
std::string Digest(absl::string_view data) {
auto hasher = CreateHasher("sha256");
CHECK_OK(hasher);
(*hasher)->Update(data);
return (*hasher)->Digest();
}
TEST(ReferencesValidatorTest, ValidateIngredientV3ThumbnailOkay) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.ingredient"
ingredient_v3 {
thumbnail {
url: "self#jumbf=/c2pa/urn:uuid:AFAFAFAF-0101-0202-0303-ABCDEFFEDCBA/c2pa.thumbnail.ingredient_1.jpg"
algorithm: "sha256"
hash: "%s"
}
}
)pb",
expected_hash));
jumbf::SuperBox thumbnail_box = {
.description =
{
.label = "c2pa.thumbnail.ingredient_1.jpg",
},
.raw_bytes = "00020000foo",
};
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel2, {thumbnail_box})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_TRUE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailureStatuses(), IsEmpty());
}
TEST(ReferencesValidatorTest, ValidateIngredientV3ThumbnailMissing) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.ingredient"
ingredient_v3 {
thumbnail {
url: "self#jumbf=/c2pa/urn:uuid:AFAFAFAF-0101-0202-0303-ABCDEFFEDCBA/c2pa.thumbnail.ingredient_1.jpg"
algorithm: "sha256"
hash: "%s"
}
}
)pb",
expected_hash));
jumbf::SuperBox manifest_store =
CreateManifestStore({CreateStandardManifest(kManifestLabel2, {})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(), UnorderedElementsAre("hashedUri.missing"));
}
TEST(ReferencesValidatorTest, ValidateIngredientV3ThumbnailMismatch) {
TestValidationTracker tracker;
auto expected_hash = Digest("bar");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.ingredient"
ingredient_v3 {
thumbnail {
url: "self#jumbf=/c2pa/urn:uuid:AFAFAFAF-0101-0202-0303-ABCDEFFEDCBA/c2pa.thumbnail.ingredient_1.jpg"
algorithm: "sha256"
hash: "%s"
}
}
)pb",
expected_hash));
jumbf::SuperBox thumbnail_box = {
.description =
{
.label = "c2pa.thumbnail.ingredient_1.jpg",
},
.raw_bytes = "00020000foo",
};
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel2, {thumbnail_box})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(),
UnorderedElementsAre("hashedUri.mismatch"));
}
TEST(ReferencesValidatorTest, ValidateIngredientV3ActiveManifestOkay) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.ingredient.v3"
ingredient_v3 {
active_manifest {
url: "self#jumbf=/c2pa/urn:uuid:AFAFAFAF-0101-0202-0303-ABCDEFFEDCBA"
algorithm: "sha256"
hash: "%s"
}
}
)pb",
expected_hash));
auto manifest_box = CreateStandardManifest(kManifestLabel2, {});
manifest_box.raw_bytes = "00020000foo";
jumbf::SuperBox manifest_store = CreateManifestStore({manifest_box});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_TRUE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(), IsEmpty());
}
TEST(ReferencesValidatorTest,
ValidateIngredientV3ActiveManifestMismatchIgnored) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.ingredient"
ingredient_v3 {
active_manifest {
url: "self#jumbf=/c2pa/urn:uuid:AFAFAFAF-0101-0202-0303-ABCDEFFEDCBA"
algorithm: "sha256"
hash: "%s"
}
}
)pb",
expected_hash));
jumbf::SuperBox manifest_store =
CreateManifestStore({CreateStandardManifest(kManifestLabel2, {})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
EXPECT_TRUE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(), IsEmpty());
}
TEST(ReferencesValidatorTest,
ValidateIngredientV3ClaimSignatureMismatchIgnored) {
TestValidationTracker tracker;
auto expected_hash = Digest("bar");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.ingredient"
ingredient_v3 {
claim_signature {
url: "self#jumbf=/c2pa/urn:uuid:AFAFAFAF-0101-0202-0303-ABCDEFFEDCBA/c2pa.signature"
algorithm: "sha256"
hash: "%s"
}
}
)pb",
expected_hash));
jumbf::SuperBox claim_signature_box = {
.description =
{
.label = "c2pa.signature",
},
.raw_bytes = "00020000foo",
};
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel2, {claim_signature_box})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
EXPECT_TRUE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(), IsEmpty());
}
TEST(ReferencesValidatorTest, ValidateActionsV1IngredientOkay) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions_v1 {
actions {
action: "c2pa.opened",
parameters {
ingredients {
url: "self#jumbf=c2pa.assertions/c2pa.ingredient__1"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox ingredient_box = {
.description =
{
.label = "c2pa.ingredient__1",
},
.raw_bytes = "00020000foo",
};
auto assertion_store = CreateAssertionStoreBox({ingredient_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel1, {assertion_store})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_TRUE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(), IsEmpty());
}
TEST(ReferencesValidatorTest, ValidateActionsV1IngredientMismatch) {
TestValidationTracker tracker;
auto expected_hash = Digest("bar");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions_v1 {
actions {
action: "c2pa.opened",
parameters {
ingredients {
url: "self#jumbf=c2pa.assertions/c2pa.ingredient__1"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox ingredient_box = {
.description =
{
.label = "c2pa.ingredient__1",
},
.raw_bytes = "00020000foo",
};
auto assertion_store = CreateAssertionStoreBox({ingredient_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel1, {assertion_store})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(),
UnorderedElementsAre("hashedUri.mismatch"));
}
TEST(ReferencesValidatorTest, ValidateActionsV2IngredientOkay) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions {
actions {
action: "c2pa.opened",
parameters {
ingredients {
url: "self#jumbf=c2pa.assertions/c2pa.ingredient__1"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox ingredient_box = {
.description =
{
.label = "c2pa.ingredient__1",
},
.raw_bytes = "00020000foo",
};
auto assertion_store = CreateAssertionStoreBox({ingredient_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel1, {assertion_store})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_TRUE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(), IsEmpty());
}
TEST(ReferencesValidatorTest, ValidateActionsV2IngredientMismatch) {
TestValidationTracker tracker;
auto expected_hash = Digest("bar");
auto assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions {
actions {
action: "c2pa.opened",
parameters {
ingredients {
url: "self#jumbf=c2pa.assertions/c2pa.ingredient__1"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox ingredient_box = {
.description =
{
.label = "c2pa.ingredient__1",
},
.raw_bytes = "00020000foo",
};
auto assertion_store = CreateAssertionStoreBox({ingredient_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel1, {assertion_store})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetFailures(),
UnorderedElementsAre("hashedUri.mismatch"));
}
TEST(ReferencesValidatorTest, ValidateActionsV2RelatedAssertionsOkay) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
Assertion assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions {
actions {
action: "c2pa.opened",
parameters {
related_assertions {
url: "self#jumbf=c2pa.assertions/c2pa.other"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox other_assertion_box = {
.description =
{
.label = "c2pa.other",
},
.raw_bytes = "00020000foo",
};
auto assertion_store = CreateAssertionStoreBox({other_assertion_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel1, {assertion_store})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_TRUE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetSuccesses(), IsEmpty());
EXPECT_THAT(tracker.GetFailures(), IsEmpty());
}
TEST(ReferencesValidatorTest, ValidateActionsV2RelatedAssertionsMissing) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
Assertion assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions {
actions {
action: "c2pa.opened",
parameters {
related_assertions {
url: "self#jumbf=c2pa.assertions/c2pa.other"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox manifest_store =
CreateManifestStore({CreateStandardManifest(kManifestLabel1, {})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetSuccesses(), IsEmpty());
EXPECT_THAT(
tracker.GetFailures(),
UnorderedElementsAre("hashedUri.missing", "assertion.action.malformed"));
}
TEST(ReferencesValidatorTest, ValidateActionsV2RelatedAssertionsMismatch) {
TestValidationTracker tracker;
auto expected_hash = Digest("bar");
Assertion assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions {
actions {
action: "c2pa.opened",
parameters {
related_assertions {
url: "self#jumbf=c2pa.assertions/c2pa.other"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox other_assertion_box = {
.description =
{
.label = "c2pa.other",
},
.raw_bytes = "00020000foo",
};
auto assertion_store = CreateAssertionStoreBox({other_assertion_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel1, {assertion_store})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetSuccesses(), IsEmpty());
EXPECT_THAT(
tracker.GetFailures(),
UnorderedElementsAre("hashedUri.mismatch", "assertion.action.malformed"));
}
TEST(ReferencesValidatorTest, ValidateActionsV2RelatedAssertionsIngredient) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
Assertion assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions {
actions {
action: "c2pa.opened",
parameters {
related_assertions {
url: "self#jumbf=c2pa.assertions/c2pa.ingredient__1"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
jumbf::SuperBox ingredient_box = {
.description =
{
.label = "c2pa.ingredient__1",
},
.raw_bytes = "00020000foo",
};
jumbf::SuperBox assertion_store = CreateAssertionStoreBox({ingredient_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel1, {assertion_store})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetSuccesses(), IsEmpty());
EXPECT_THAT(tracker.GetFailures(),
UnorderedElementsAre("assertion.action.malformed"));
}
TEST(ReferencesValidatorTest,
ValidateActionsV2RelatedAssertionsInSeparateManifest) {
TestValidationTracker tracker;
auto expected_hash = Digest("foo");
Assertion assertion = ParseTextProtoOrDie<Assertion>(absl::StrFormat(
R"pb(
label: "c2pa.action"
actions {
actions {
action: "c2pa.opened",
parameters {
related_assertions {
url: "self#jumbf=c2pa.assertions/c2pa.other"
algorithm: "sha256"
hash: "%s"
}
}
}
}
)pb",
expected_hash));
assertion.mutable_actions()
->mutable_actions(0)
->mutable_parameters()
->mutable_related_assertions(0)
->set_url(absl::StrCat("self#jumbf=/c2pa/", kManifestLabel2,
"/c2pa.assertions/c2pa.other"));
jumbf::SuperBox ingredient_box = {
.description =
{
.label = "c2pa.other",
},
.raw_bytes = "00020000foo",
};
jumbf::SuperBox assertion_store = CreateAssertionStoreBox({ingredient_box});
jumbf::SuperBox manifest_store = CreateManifestStore(
{CreateStandardManifest(kManifestLabel2, {assertion_store}),
CreateStandardManifest(kManifestLabel1, {})});
jumbf::UriResolver uri_resolver =
jumbf::UriResolver::WithSingleRootChild(&manifest_store);
HashedUriValidator hashed_uri_validator(std::string(kManifestPath1),
uri_resolver);
ReferencesValidator references_validator(&hashed_uri_validator,
kManifestLabel1, &tracker.tracker());
ASSERT_FALSE(references_validator.Validate(assertion));
EXPECT_THAT(tracker.GetSuccesses(), IsEmpty());
EXPECT_THAT(tracker.GetFailures(),
UnorderedElementsAre("assertion.action.malformed"));
}
} // namespace
} // namespace credentio