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

Feature/server forbidden filenames #6965

Merged
merged 13 commits into from
Aug 21, 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
40 changes: 37 additions & 3 deletions src/gui/invalidfilenamedialog.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/gui/invalidfilenamedialog.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/gui/invalidfilenamedialog.cpp

File src/gui/invalidfilenamedialog.cpp does not conform to Custom style guidelines. (lines 99, 103)
* Copyright (C) by Felix Weilbach <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -64,7 +64,12 @@

namespace OCC {

InvalidFilenameDialog::InvalidFilenameDialog(AccountPtr account, Folder *folder, QString filePath, FileLocation fileLocation, QWidget *parent)
InvalidFilenameDialog::InvalidFilenameDialog(AccountPtr account,

Check warning on line 67 in src/gui/invalidfilenamedialog.cpp

View workflow job for this annotation

GitHub Actions / build

src/gui/invalidfilenamedialog.cpp:67:1 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: , _ui, _account, _filePath, _relativeFilePath, _originalFileName, _newFilename,
Folder *folder,
QString filePath,

Check warning on line 69 in src/gui/invalidfilenamedialog.cpp

View workflow job for this annotation

GitHub Actions / build

src/gui/invalidfilenamedialog.cpp:69:46 [bugprone-easily-swappable-parameters]

2 adjacent parameters of 'InvalidFilenameDialog' of similar type ('int') are easily swapped by mistake
FileLocation fileLocation,
InvalidMode invalidMode,
QWidget *parent)
: QDialog(parent)
, _ui(new Ui::InvalidFilenameDialog)
, _account(account)
Expand All @@ -89,8 +94,37 @@
_ui->descriptionLabel->setTextFormat(Qt::PlainText);
_ui->errorLabel->setTextFormat(Qt::PlainText);

_ui->descriptionLabel->setText(tr("The file \"%1\" could not be synced because the name contains characters which are not allowed on this system.").arg(_originalFileName));
_ui->explanationLabel->setText(tr("The following characters are not allowed on the system: * \" | & ? , ; : \\ / ~ < > leading/trailing spaces"));
switch (invalidMode) {
case InvalidMode::SystemInvalid:
_ui->descriptionLabel->setText(tr("The file \"%1\" could not be synced because the name contains characters which are not allowed on this system.").arg(_originalFileName));
_ui->explanationLabel->setText(tr("The following characters are not allowed on the system: * \" | & ? , ; : \\ / ~ < > leading/trailing spaces"));
break;
case InvalidMode::ServerInvalid:
_ui->descriptionLabel->setText(tr("The file \"%1\" could not be synced because the name contains characters which are not allowed on the server.").arg(_originalFileName));

const auto caps = _account->capabilities();
const auto forbiddenCharacters = caps.forbiddenFilenameCharacters();
const auto forbiddenBasenames = caps.forbiddenFilenameBasenames();
const auto forbiddenFilenames = caps.forbiddenFilenames();
const auto forbiddenExtensions = caps.forbiddenFilenameExtensions();

auto explanations = QStringList();

if (!forbiddenCharacters.isEmpty()) {
explanations.append(tr("The following characters are not allowed: %1").arg(forbiddenCharacters.join(" ")));
}
if (!forbiddenBasenames.isEmpty()) {
explanations.append(tr("The following basenames are not allowed: %1").arg(forbiddenBasenames.join(" ")));
}
if (!forbiddenFilenames.isEmpty()) {
explanations.append(tr("The following filenames are not allowed: %1").arg(forbiddenFilenames.join(" ")));
}
if (!forbiddenExtensions.isEmpty()) {
explanations.append(tr("The following file extensions are not allowed: %1").arg(forbiddenExtensions.join(" ")));
}
_ui->explanationLabel->setText(explanations.join("\n"));
break;
}
_ui->filenameLineEdit->setText(filePathFileInfo.fileName());

connect(_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
Expand Down
11 changes: 10 additions & 1 deletion src/gui/invalidfilenamedialog.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/gui/invalidfilenamedialog.h

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/gui/invalidfilenamedialog.h

File src/gui/invalidfilenamedialog.h does not conform to Custom style guidelines. (lines 42, 43, 44)
* Copyright (C) by Felix Weilbach <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -14,7 +14,7 @@

#pragma once

#include <accountfwd.h>

Check failure on line 17 in src/gui/invalidfilenamedialog.h

View workflow job for this annotation

GitHub Actions / build

src/gui/invalidfilenamedialog.h:17:10 [clang-diagnostic-error]

'accountfwd.h' file not found
#include <account.h>

#include <memory>
Expand All @@ -39,8 +39,17 @@
Default = 0,
NewLocalFile,
};
enum class InvalidMode {
SystemInvalid,
ServerInvalid
};

explicit InvalidFilenameDialog(AccountPtr account, Folder *folder, QString filePath, FileLocation fileLocation = FileLocation::Default, QWidget *parent = nullptr);
explicit InvalidFilenameDialog(AccountPtr account,
Folder *folder,
QString filePath,
FileLocation fileLocation = FileLocation::Default,
InvalidMode invalidMode = InvalidMode::SystemInvalid,
QWidget *parent = nullptr);

~InvalidFilenameDialog() override;

Expand Down
14 changes: 10 additions & 4 deletions src/gui/tray/activitylistmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/gui/tray/activitylistmodel.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/gui/tray/activitylistmodel.cpp

File src/gui/tray/activitylistmodel.cpp does not conform to Custom style guidelines. (lines 732, 733, 846, 847, 848, 849, 850)
* Copyright (C) by Klaas Freitag <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -729,9 +729,12 @@
const auto fileLocation = activity._syncFileItemStatus == SyncFileItem::FileNameInvalidOnServer
? InvalidFilenameDialog::FileLocation::NewLocalFile
: InvalidFilenameDialog::FileLocation::Default;
const auto invalidMode = activity._syncFileItemStatus == SyncFileItem::FileNameInvalidOnServer
? InvalidFilenameDialog::InvalidMode::ServerInvalid
: InvalidFilenameDialog::InvalidMode::SystemInvalid;

_currentInvalidFilenameDialog = new InvalidFilenameDialog(_accountState->account(), folder,
folderDir.filePath(activity._file), fileLocation);
folderDir.filePath(activity._file), fileLocation, invalidMode);
connect(_currentInvalidFilenameDialog, &InvalidFilenameDialog::accepted, folder, [folder]() {
folder->scheduleThisFolderSoon();
});
Expand Down Expand Up @@ -840,9 +843,12 @@
if (action._verb == "WEB") {
Utility::openBrowser(QUrl(action._link));
return;
} else if (action._verb == "FIX_CONFLICT_LOCALLY" &&
activity._type == Activity::SyncFileItemType &&
(activity._syncFileItemStatus == SyncFileItem::Conflict || activity._syncFileItemStatus == SyncFileItem::FileNameClash)) {
} else if (((action._verb == "FIX_CONFLICT_LOCALLY" || action._verb == "RENAME_LOCAL_FILE") &&
activity._type == Activity::SyncFileItemType &&
(activity._syncFileItemStatus == SyncFileItem::Conflict ||
activity._syncFileItemStatus == SyncFileItem::FileNameClash ||
activity._syncFileItemStatus == SyncFileItem::FileNameInvalid ||
activity._syncFileItemStatus == SyncFileItem::FileNameInvalidOnServer))) {
slotTriggerDefaultAction(activityIndex);
return;
} else if (action._verb == ActivityLink::WhitelistFolderVerb && !activity._file.isEmpty()) { // _folder == folder alias/name, _file == folder/file path
Expand Down
10 changes: 9 additions & 1 deletion src/gui/tray/usermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@
return item->_status == SyncFileItem::Conflict && !Utility::isConflictFile(item->_file);
}

void User::processCompletedSyncItem(const Folder *folder, const SyncFileItemPtr &item)

Check warning on line 759 in src/gui/tray/usermodel.cpp

View workflow job for this annotation

GitHub Actions / build

src/gui/tray/usermodel.cpp:759:12 [readability-function-cognitive-complexity]

function 'processCompletedSyncItem' has cognitive complexity of 38 (threshold 25)
{
if (item->_direction == SyncFileItem::Down && item->_instruction == CSYNC_INSTRUCTION_SYNC) {
qCDebug(lcActivity) << "Skipping activities about changes coming from server.";
Expand Down Expand Up @@ -848,7 +848,15 @@
_activityModel->addIgnoredFileToList(activity);
} else {
// add 'protocol error' to activity list
if (item->_status == SyncFileItem::Status::FileNameInvalid) {
if (item->_status == SyncFileItem::Status::FileNameInvalid || item->_status == SyncFileItem::Status::FileNameInvalidOnServer) {
ActivityLink buttonActivityLink;
buttonActivityLink._label = tr("Rename file");
buttonActivityLink._link = activity._link.toString();
buttonActivityLink._verb = "RENAME_LOCAL_FILE";
buttonActivityLink._primary = true;

activity._links = {buttonActivityLink};

showDesktopNotification(item->_file, activity._subject, activity._id);
} else if (item->_status == SyncFileItem::Conflict || item->_status == SyncFileItem::FileNameClash) {
ActivityLink buttonActivityLink;
Expand Down
20 changes: 20 additions & 0 deletions src/libsync/capabilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ QStringList Capabilities::blacklistedFiles() const
return _capabilities["files"].toMap()["blacklisted_files"].toStringList();
}

QStringList Capabilities::forbiddenFilenames() const
{
return _capabilities["files"].toMap()["forbidden_filenames"].toStringList();
}

QStringList Capabilities::forbiddenFilenameCharacters() const
{
return _capabilities["files"].toMap()["forbidden_filename_characters"].toStringList();
}

QStringList Capabilities::forbiddenFilenameBasenames() const
{
return _capabilities["files"].toMap()["forbidden_filename_basenames"].toStringList();
}

QStringList Capabilities::forbiddenFilenameExtensions() const
{
return _capabilities["files"].toMap()["forbidden_filename_extensions"].toStringList();
}

/*-------------------------------------------------------------------------------------*/

// Direct Editing
Expand Down
5 changes: 5 additions & 0 deletions src/libsync/capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#ifndef CAPABILITIES_H
#define CAPABILITIES_H

#include "owncloudlib.h"

Check failure on line 19 in src/libsync/capabilities.h

View workflow job for this annotation

GitHub Actions / build

src/libsync/capabilities.h:19:10 [clang-diagnostic-error]

'owncloudlib.h' file not found

#include <QVariantMap>
#include <QStringList>
Expand Down Expand Up @@ -166,6 +166,11 @@
*/
[[nodiscard]] QStringList blacklistedFiles() const;

[[nodiscard]] QStringList forbiddenFilenameCharacters() const;
[[nodiscard]] QStringList forbiddenFilenameBasenames() const;
[[nodiscard]] QStringList forbiddenFilenameExtensions() const;
[[nodiscard]] QStringList forbiddenFilenames() const;

/**
* Whether conflict files should remain local (default) or should be uploaded.
*/
Expand Down
44 changes: 43 additions & 1 deletion src/libsync/discovery.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/libsync/discovery.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/libsync/discovery.cpp

File src/libsync/discovery.cpp does not conform to Custom style guidelines. (lines 310, 311, 312, 313, 314, 315, 316, 317, 321, 322, 323)
* Copyright (C) by Olivier Goffart <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -292,8 +292,37 @@
}

const auto &localName = entries.localEntry.name;
const auto splitName = localName.split('.');
const auto &baseName = splitName.first();
const auto extension = splitName.size() > 1 ? splitName.last() : QString();
const auto accountCaps = _discoveryData->_account->capabilities();
const auto forbiddenFilenames = accountCaps.forbiddenFilenames();
const auto forbiddenBasenames = accountCaps.forbiddenFilenameBasenames();
const auto forbiddenExtensions = accountCaps.forbiddenFilenameExtensions();
const auto forbiddenChars = accountCaps.forbiddenFilenameCharacters();

const auto hasForbiddenFilename = forbiddenFilenames.contains(localName);
const auto hasForbiddenBasename = forbiddenBasenames.contains(baseName);
const auto hasForbiddenExtension = forbiddenExtensions.contains(extension);

auto forbiddenCharMatch = QString{};
const auto containsForbiddenCharacters =
std::any_of(forbiddenChars.cbegin(),
forbiddenChars.cend(),
[&localName, &forbiddenCharMatch](const QString &charPattern) {
if (localName.contains(charPattern)) {
forbiddenCharMatch = charPattern;
return true;
}
return false;
});

if (excluded == CSYNC_NOT_EXCLUDED && !localName.isEmpty()
&& _discoveryData->_serverBlacklistedFiles.contains(localName)) {
&& (_discoveryData->_serverBlacklistedFiles.contains(localName)
|| hasForbiddenFilename
|| hasForbiddenBasename
|| hasForbiddenExtension
|| containsForbiddenCharacters)) {
excluded = CSYNC_FILE_EXCLUDE_SERVER_BLACKLISTED;
isInvalidPattern = true;
}
Expand Down Expand Up @@ -401,6 +430,19 @@
break;
case CSYNC_FILE_EXCLUDE_SERVER_BLACKLISTED:
item->_errorString = tr("The filename is blacklisted on the server.");
if (hasForbiddenFilename) {
item->_errorString += tr(" Reason: the entire filename is forbidden.");
}
if (hasForbiddenBasename) {
item->_errorString += tr(" Reason: the filename has a forbidden base name (filename start).");
}
if (hasForbiddenExtension) {
item->_errorString += tr(" Reason: the file has a forbidden extension (.%1).").arg(extension);
}
if (containsForbiddenCharacters) {
item->_errorString += tr(" Reason: the filename contains a forbidden character (%1).").arg(forbiddenCharMatch);
}
item->_status = SyncFileItem::FileNameInvalidOnServer;
break;
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/testlocaldiscovery.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*

Check notice on line 1 in test/testlocaldiscovery.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on test/testlocaldiscovery.cpp

File test/testlocaldiscovery.cpp does not conform to Custom style guidelines. (lines 300, 303, 304, 305, 306, 307, 308, 309)
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
*
*/

#include <QtTest>

Check failure on line 8 in test/testlocaldiscovery.cpp

View workflow job for this annotation

GitHub Actions / build

test/testlocaldiscovery.cpp:8:10 [clang-diagnostic-error]

'QtTest' file not found
#include "syncenginetestutils.h"
#include <syncengine.h>
#include <localdiscoverytracker.h>
Expand Down Expand Up @@ -294,6 +294,45 @@
QVERIFY(!fakeFolder.currentRemoteState().find("C/bar"));
}

// Tests the behavior of forbidden filename detection
void testServerForbiddenFilenames()
{
FakeFolder fakeFolder { FileInfo::A12_B12_C12_S12() };
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

fakeFolder.syncEngine().account()->setCapabilities({
{ "files", QVariantMap {
{ "forbidden_filenames", QVariantList { ".foo", "bar" } },
{ "forbidden_filename_characters", QVariantList { "_" } },
{ "forbidden_filename_basenames", QVariantList { "base" } },
{ "forbidden_filename_extensions", QVariantList { "ext" } }
} }
});

fakeFolder.localModifier().insert("C/.foo");
fakeFolder.localModifier().insert("C/bar");
fakeFolder.localModifier().insert("C/moo");
fakeFolder.localModifier().insert("C/.moo");
fakeFolder.localModifier().insert("C/potatopotato.txt");
fakeFolder.localModifier().insert("C/potato_potato.txt");
fakeFolder.localModifier().insert("C/basefilename.txt");
fakeFolder.localModifier().insert("C/base.txt");
fakeFolder.localModifier().insert("C/filename.txt");
fakeFolder.localModifier().insert("C/filename.ext");

QVERIFY(fakeFolder.syncOnce());
QVERIFY(fakeFolder.currentRemoteState().find("C/moo"));
QVERIFY(fakeFolder.currentRemoteState().find("C/.moo"));
QVERIFY(!fakeFolder.currentRemoteState().find("C/.foo"));
QVERIFY(!fakeFolder.currentRemoteState().find("C/bar"));
QVERIFY(fakeFolder.currentRemoteState().find("C/potatopotato.txt"));
QVERIFY(!fakeFolder.currentRemoteState().find("C/potato_potato.txt"));
QVERIFY(fakeFolder.currentRemoteState().find("C/basefilename.txt"));
QVERIFY(!fakeFolder.currentRemoteState().find("C/base.txt"));
QVERIFY(fakeFolder.currentRemoteState().find("C/filename.txt"));
QVERIFY(!fakeFolder.currentRemoteState().find("C/filename.ext"));
}

void testCreateFileWithTrailingSpaces_localAndRemoteTrimmedDoNotExist_renameAndUploadFile()
{
FakeFolder fakeFolder{FileInfo{}};
Expand Down
Loading