Skip to content

Commit

Permalink
Better error message when base64-decode fails. Better error when XML …
Browse files Browse the repository at this point in the history
…message has no content. Other error message improvements. Handle empty data attribute in XML file
  • Loading branch information
bepaald committed Mar 3, 2025
1 parent 8c41ad5 commit dd48a7a
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion autoversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
#ifndef VERSION_H_
#define VERSION_H_

#define VERSIONDATE "20250302.160607"
#define VERSIONDATE "20250303.161100"

#endif
5 changes: 2 additions & 3 deletions base64/base64.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2019-2024 Selwin van Dijk
Copyright (C) 2019-2025 Selwin van Dijk
This file is part of signalbackup-tools.
Expand Down Expand Up @@ -63,8 +63,7 @@ inline std::pair<unsigned char*, size_t> Base64::base64StringToBytes(T const &st
std::unique_ptr<unsigned char[]> output(new unsigned char[binarylength]);
if (EVP_DecodeBlock(output.get(), reinterpret_cast<unsigned const char *>(str.data()), str.size()) == -1)
{
Logger::error("Failed to base64 decode data: ",
bepaald::bytesToHexString(reinterpret_cast<unsigned char const *>(str.data()), str.size()));
Logger::error("Failed to base64 decode data (size: ", str.size(), "): ", str.data());
return {nullptr, 0};
}
if (str.empty() || str.back() != '=')
Expand Down
3 changes: 0 additions & 3 deletions signalbackup/exporttofile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ bool SignalBackup::exportBackupToFile(std::string const &filename, std::string c
return false;
}

if (!d_headerframe)
std::cout << "bad" << std::endl;

if (!d_headerframe || !d_fe.init(newpw, d_headerframe->salt(), d_headerframe->salt_length(), d_headerframe->iv(), d_headerframe->iv_length(), d_headerframe->version(), d_verbose))
{
Logger::error("Failed to initialize FileEncryptor");
Expand Down
11 changes: 9 additions & 2 deletions signalbackup/importfromplaintextbackup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ bool SignalBackup::importFromPlaintextBackup(std::unique_ptr<SignalPlaintextBack
std::string body = pt_messages(i, "body");
if (body.empty() && pt_messages.valueAsInt(i, "numattachments") <= 0)
{
Logger::warning("Not inserting message with empty body and no attachments.");
Logger::warning("Not inserting message with empty body and no attachments. (date: ", pt_messages.valueAsInt(i, "date", -1), ")");
continue;
}

Expand Down Expand Up @@ -453,7 +453,7 @@ bool SignalBackup::importFromPlaintextBackup(std::unique_ptr<SignalPlaintextBack
auto amit = attachment_messages.find(attachment_res.valueAsInt(j, "mid", -1));
if (amit == attachment_messages.end()) [[unlikely]]
{
Logger::warning("Found attachment that belongs to no message");
Logger::warning("Found attachment that belongs to no message (mid: ", attachment_res.valueAsInt(j, "mid", -1), ", size: ", size, ")");
continue;
}
long long int new_message_id = amit->second.first;
Expand Down Expand Up @@ -496,6 +496,13 @@ bool SignalBackup::importFromPlaintextBackup(std::unique_ptr<SignalPlaintextBack
else
att_data_size = ptar.dataSize();

// not supported on signal android anyway
if (att_data_size == 0) [[unlikely]]
{
Logger::warning("Not inserting 0 byte attachment");
continue;
}

// add entry to attachment table;
std::any new_aid;
if (!insertRow(d_part_table,
Expand Down
2 changes: 1 addition & 1 deletion signalbackup/ptcreaterecipient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ long long int SignalBackup::ptCreateRecipient(std::unique_ptr<SignalPlaintextBac
gm, std::string());
if (cn.empty()) [[unlikely]]
{
Logger::warning("Unexpectedly got empty contact name for group recipient ", makePrintable(gm));
Logger::warning("Unexpectedly got empty contact name for group recipient ", gm);//makePrintable(gm));
cn = "(unknown)";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ inline int SignalPlainTextBackupAttachmentReader::getAttachment(FrameWithAttachm
inline int SignalPlainTextBackupAttachmentReader::getAttachmentData(unsigned char **data, bool verbose)
{
// read the data if needed
if (d_base64data.empty() && !d_filename.empty())
if (d_size > 0 && d_base64data.empty() && !d_filename.empty())
{
std::ifstream file(std::filesystem::path(d_filename), std::ios_base::binary | std::ios_base::in);
if (!file.is_open())
Expand All @@ -96,15 +96,15 @@ inline int SignalPlainTextBackupAttachmentReader::getAttachmentData(unsigned cha
}
}

if (d_base64data.empty())
if (d_size > 0 && d_base64data.empty()) // filename.empty(), but so is data, while size is > 0
{
Logger::error("SignalPlainTextBackupAttachmentReader has no base64 encoded data");
return 1;
}

unsigned char *attdata;
std::tie(attdata, d_truesize) = Base64::base64StringToBytes(d_base64data);
if (d_truesize == 0 || !attdata)
if (!attdata)
{
Logger::error("Failed to decode base64-encoded attachment from \"", d_filename, "\"");
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,10 @@ SignalPlaintextBackupDatabase::SignalPlaintextBackupDatabase(std::vector<std::st

// do something with data...
XmlDocument::Node::StringOrRef attachmentdata = part.getAttributeStringOrRef("data");
if (attachmentdata.file.empty() && attachmentdata.value.empty()) [[unlikely]]
if (attachmentdata.size > 0 && attachmentdata.file.empty() && attachmentdata.value.empty()) [[unlikely]]
{
Logger::warning("Got data attribute, but no value or reference");
n.print();
continue;
}
std::string ct;
Expand Down
2 changes: 2 additions & 0 deletions signalplaintextbackupdatabase/signalplaintextbackupdatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ inline std::string SignalPlaintextBackupDatabase::normalizePhoneNumber(std::stri
{
result = in;

// convert entities : '&[x][NN];'?

#if __cpp_lib_erase_if >= 202002L
unsigned int removed = std::erase_if(result, [](char c) { return (c < '0' || c > '9') && c != '+'; });
#else
Expand Down
18 changes: 15 additions & 3 deletions xmldocument/xmldocument.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ XmlDocument::XmlDocument(std::string const &filename)
{
attribute_pos = filepos + i;
attribute_size = 0;
///std::cout << "attribute value starts at pos: " << attribute_pos << "(" << attribute_size << ")" << std::endl;
// if (attribute_name_tmp == "data")
// std::cout << "attribute value starts at pos: " << attribute_pos << "(" << attribute_size << ")" << std::endl;
}

/**** NEW METHOD ****/
Expand All @@ -79,21 +80,32 @@ XmlDocument::XmlDocument(std::string const &filename)
attribute_size = attribute_size + (closing_quote - (buffer.get() + i));
attribute_value_tmp.append(buffer.get() + i, attribute_size < Node::s_maxsize ? attribute_size - attribute_value_tmp.size() : 0);

// if (attribute_name_tmp == "data")
// {
// std::cout << " - attribute: '" << attribute_name_tmp << "'='" << attribute_value_tmp << "'" << std::endl;
// std::cout << "filepos : " << filepos << std::endl;
//// std::cout << "i : " << newbufferpos << std::endl;
// //std::cout << "i : " << newbufferpos << std::endl;
// std::cout << "att_pos : " << attribute_pos << std::endl;
// std::cout << "att_size: " << attribute_size << std::endl;
// std::cout << "attribute value ends at pos: " << (filepos + i + (closing_quote - (buffer.get() + i))) << " SIZE: " << attribute_size << std::endl;
// }

Node::StringOrRef attributevalue =
{
(attribute_size < Node::s_maxsize) ? std::move(attribute_value_tmp) : std::string(),
(attribute_size < Node::s_maxsize) ? std::string() : filename,
(attribute_size < Node::s_maxsize) ? -1 : attribute_pos,
(attribute_size < Node::s_maxsize) ? -1 : attribute_size
attribute_size
};

// if (attribute_name_tmp == "data")
// {
// std::cout << attributevalue.file << std::endl;
// std::cout << attributevalue.value << std::endl;
// std::cout << attributevalue.size << std::endl;
// std::cout << attributevalue.pos << std::endl;
// }

d_currentnode->d_attributes[attribute_name_tmp] = std::move(attributevalue);
attribute_value_tmp.clear();
attribute_name_tmp.clear();
Expand Down

0 comments on commit dd48a7a

Please sign in to comment.