Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cpn): validate translated update component name when used for folder name #5079

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 40 additions & 22 deletions companion/src/updates/updateinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,38 +957,26 @@ void UpdateInterface::setReleaseId(QString name)

bool UpdateInterface::setRunFolders()
{
QRegularExpression rx("[0-9a-zA-Z_\\-.]+");
// the validator treats the regexp as "^[0-9a-zA-Z_\-.]+$"
QRegularExpressionValidator v(rx);
int pos = 0;
// translated component names my not be valid folder names
QString compfldr(m_name);

QString fldr(m_repo->releases()->name().trimmed());
if (!validateFolder(compfldr))
compfldr = QString("C%1").arg(m_id);

if (v.validate(fldr, pos) != QValidator::Acceptable) {
fldr.replace("\"", "");
fldr.replace("'", "");
fldr.replace("(", "_");
fldr.replace(")", "_");
fldr.replace("[", "_");
fldr.replace("]", "_");
fldr.replace(" ", "_");
fldr.replace("__", "_");
// release names may my not be valid folder names
QString relfldr(m_repo->releases()->name());

if (v.validate(fldr, pos) != QValidator::Acceptable) {
//m_status->reportProgress(tr("Unable to use release name %1 for directory name using default %2")
// .arg(m_repo->releases()->name().trimmed()).arg(QString("R%1").arg(m_repo->releases()->id())), QtDebugMsg);
fldr = QString("R%1").arg(m_repo->releases()->id());
}
}
if (!validateFolder(relfldr))
relfldr = QString("R%1").arg(m_repo->releases()->id());

m_downloadDir = QString("%1/%2/%3").arg(m_params->downloadDir).arg(m_name).arg(fldr);
m_downloadDir = QString("%1/%2/%3").arg(m_params->downloadDir).arg(compfldr).arg(relfldr);

if (!checkCreateDirectory(m_downloadDir, UPDFLG_Download))
return false;

//m_status->reportProgress(tr("Download directory: %1").arg(m_downloadDir), QtDebugMsg);

m_decompressDir = QString("%1/%2/%3").arg(m_params->decompressDir).arg(m_name).arg(fldr);
m_decompressDir = QString("%1/%2/%3").arg(m_params->decompressDir).arg(compfldr).arg(relfldr);

if (!checkCreateDirectory(m_decompressDir, UPDFLG_Decompress))
return false;
Expand Down Expand Up @@ -1160,6 +1148,36 @@ const QString UpdateInterface::updateFlagToString(const int flag)
}
}

bool UpdateInterface::validateFolder(QString & fldr)
{
QRegularExpression rx("[0-9a-zA-Z_\\-.]+");
// the validator treats the regexp as "^[0-9a-zA-Z_\-.]+$"
QRegularExpressionValidator v(rx);
int pos = 0;

fldr = fldr.trimmed();

if (v.validate(fldr, pos) != QValidator::Acceptable) {
fldr.replace("\"", "");
fldr.replace("'", "");
fldr.replace("(", "_");
fldr.replace(")", "_");
fldr.replace("[", "_");
fldr.replace("]", "_");
fldr.replace(" ", "_");
fldr.replace("__", "_");

if (v.validate(fldr, pos) != QValidator::Acceptable) {
return false;
}
}

if (fldr.isEmpty())
return false;

return true;
}

const QString UpdateInterface::versionCurrent()
{
return g.component[m_id].version();
Expand Down
1 change: 1 addition & 0 deletions companion/src/updates/updateinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class UpdateInterface : public QWidget
bool decompressArchive(const QString & archivePath, const QString & destPath);
int releaseSettingsSave();
bool setRunFolders();
bool validateFolder(QString & fldr);

bool okToRun() { return m_result == PROC_RESULT_SUCCESS && m_stopping == false; }
void runAsyncInstall();
Expand Down