Skip to content

Commit

Permalink
fix(cpn): validate translated update component name when used for fol…
Browse files Browse the repository at this point in the history
…der name (#5079)
  • Loading branch information
elecpower authored and pfeerick committed May 29, 2024
1 parent 676fd4c commit 5e14ce6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
62 changes: 40 additions & 22 deletions companion/src/updates/updateinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,38 +854,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 @@ -1027,6 +1015,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 @@ -148,4 +148,5 @@ class UpdateInterface : public QWidget
bool decompressArchive(const QString & archivePath, const QString & destPath);
bool releaseSettingsSave();
bool setRunFolders();
bool validateFolder(QString & fldr);
};

0 comments on commit 5e14ce6

Please sign in to comment.