Skip to content

Commit

Permalink
Use QString in is_safe_filename
Browse files Browse the repository at this point in the history
  • Loading branch information
lmoureaux authored and jwrober committed Jan 1, 2024
1 parent 2fcab63 commit 0a6ba6d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
27 changes: 5 additions & 22 deletions utility/shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QString>
#include <QtGlobal>
Expand Down Expand Up @@ -208,29 +209,11 @@ static bool is_ascii(char ch)
Check if the name is safe security-wise. This is intended to be used to
make sure an untrusted filename is safe to be used.
*/
bool is_safe_filename(const char *name)
bool is_safe_filename(const QString &name)
{
int i = 0;

// must not be nullptr or empty
if (!name || *name == '\0') {
return false;
}

for (; '\0' != name[i]; i++) {
if (nullptr == strchr(".@", name[i])
&& nullptr == strchr(base64url, name[i])) {
return false;
}
}

// we don't allow the filename to ascend directories
if (strstr(name, PARENT_DIR_OPERATOR)) {
return false;
}

// Otherwise, it is okay...
return true;
const QRegularExpression regex(QLatin1String("^[\\w_\\-.@]+$"));
return regex.match(name).hasMatch()
&& !name.contains(QLatin1String(PARENT_DIR_OPERATOR));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion utility/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const char *int_to_text(unsigned int number);

bool is_ascii_name(const char *name);
bool is_base64url(const char *s);
bool is_safe_filename(const char *name);
bool is_safe_filename(const QString &name);
void randomize_base64url_string(char *s, size_t n);

char *skip_leading_spaces(char *s);
Expand Down

0 comments on commit 0a6ba6d

Please sign in to comment.