Skip to content

Commit 2aa789d

Browse files
committed
Fix validating files with leading /
IB-8933 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent bc64a76 commit 2aa789d

5 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/SignatureXAdES_B.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,11 @@ int initXmlSecCallback()
154154
return {};
155155
}
156156

157-
auto find = [name](auto files) -> const DataFile* {
157+
string uriName = File::fromUriPath(name);
158+
auto find = [&uriName](auto files) -> const DataFile* {
158159
for(const DataFile *file: files)
159160
{
160-
if(file->fileName() == name)
161+
if(file->fileName() == uriName)
161162
return file;
162163
}
163164
return {};
@@ -506,10 +507,7 @@ void SignatureXAdES_B::validate(const string &policy) const
506507
EXCEPTION_ADD(exception, "Reference '%.*s' ID missing", int(uri.size()), uri.data());
507508
else
508509
{
509-
string uriPath = File::fromUriPath(uri);
510-
if(uriPath.front() == '/')
511-
uriPath.erase(0);
512-
signatureref.emplace(uriPath, mimeinfo[string("#").append(ref["Id"])]);
510+
signatureref.emplace(File::fromUriPath(uri), mimeinfo[string("#").append(ref["Id"])]);
513511
}
514512
}
515513
if(!signedInfoFound)

src/SignatureXAdES_LTA.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ void SignatureXAdES_LTA::calcArchiveDigest(const Digest &digest, string_view can
5454
}
5555

5656
string uriPath = File::fromUriPath(uri);
57-
if(uriPath.front() == '/')
58-
uriPath.erase(0);
59-
6057
auto files = bdoc->dataFiles();
6158
auto file = find_if(files.cbegin(), files.cend(), [&uriPath](DataFile *file) {
6259
return file->fileName() == uriPath;

src/util/File.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ constexpr bool fromHexChar(auto pos, auto end, auto &value)
336336

337337
string File::fromUriPath(string_view path)
338338
{
339+
if(!path.empty() && path.front() == '/')
340+
path.remove_prefix(1);
339341
string ret;
340342
ret.reserve(path.size());
341343
uint8_t value = 0;
7.01 KB
Binary file not shown.

test/libdigidocpp_boost.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,18 @@ BOOST_AUTO_TEST_CASE(manifest_data_file_dot_and_empty_paths_are_rejected)
573573
BOOST_CHECK_THROW(Container::openPtr("dot.asice"), Exception);
574574
BOOST_CHECK_THROW(Container::openPtr("pt-empty.asice"), Exception);
575575
}
576+
577+
BOOST_AUTO_TEST_CASE(LeadingSlashReferenceURI)
578+
{
579+
// Container signed with URI="/test1.txt" (leading slash).
580+
// Without the erase(0,1) fix the reference path is erased to "",
581+
// failing to match the DataFile and throwing during validate().
582+
auto d = Container::openPtr("test-leading-slash-uri.asice");
583+
BOOST_REQUIRE_EQUAL(d->dataFiles().size(), 1U);
584+
BOOST_REQUIRE_EQUAL(d->signatures().size(), 1U);
585+
BOOST_CHECK_EQUAL(d->dataFiles().front()->fileName(), "test1.txt");
586+
BOOST_CHECK_NO_THROW(d->signatures().front()->validate());
587+
}
576588
BOOST_AUTO_TEST_SUITE_END()
577589

578590
BOOST_AUTO_TEST_SUITE(ASiCSTestSuite)

0 commit comments

Comments
 (0)