Skip to content
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
80 changes: 50 additions & 30 deletions src/dbus/applicationservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void unescapeEnvs(QVariantMap &options) noexcept
options.insert("env", result);
}

} // namespace
} // namespace

void ApplicationService::appendExtraEnvironments(QVariantMap &runtimeOptions) const noexcept
{
Expand Down Expand Up @@ -1098,6 +1098,8 @@ void ApplicationService::resetEntry(DesktopEntry *newEntry) noexcept
emit xCreatedByChanged();
}

enum class SpliterState : uint8_t { Normal, InSingleQuote, InDoubleQuotes };

std::optional<QStringList> ApplicationService::splitExecArguments(QStringView str) noexcept
{
if (str.isEmpty()) {
Expand All @@ -1108,54 +1110,72 @@ std::optional<QStringList> ApplicationService::splitExecArguments(QStringView st
QString currentToken;
currentToken.reserve(str.size());

bool inQuotes{false};
auto curState{SpliterState::Normal};
bool hasToken{false};

for (const auto *it = str.begin(); it != str.end(); ++it) {
const auto c = *it;

if (c == u'\\') {
if ((it + 1) == str.end()) {
qWarning() << "Exec parsing error: Backslash at end of line";
return std::nullopt;
}
switch (curState) {
case SpliterState::Normal: {
if (c == u'\\') {
if (++it == str.end()) {
return std::nullopt;
}

const auto next = *(++it);
currentToken.append(*it);
hasToken = true;
} else if (c == u'"') {
curState = SpliterState::InDoubleQuotes;
hasToken = true;
} else if (c == u'\'') {
qWarning() << "Exec parsing: Single quote detected. "
"Note: Single quotes are not part of the Desktop Entry Spec, "
"supporting them for GIO/Shell compatibility.";
curState = SpliterState::InSingleQuote;
hasToken = true;
} else if (c.isSpace()) {
if (hasToken) {
args.append(currentToken);
currentToken.clear();
hasToken = false;
}
} else {
currentToken.append(c);
hasToken = true;
}
} break;
case SpliterState::InDoubleQuotes: {
if (c == u'\\') {
if (++it == str.end()) {
return std::nullopt;
}

if (inQuotes) {
const auto next = *it;
if (next == u'"' || next == u'\\' || next == u'$' || next == u'`') {
currentToken.append(next);
} else {
currentToken.append(u'\\');
currentToken.append(next);
}
} else if (c == u'"') {
curState = SpliterState::Normal;
} else {
currentToken.append(next);
currentToken.append(c);
}
hasToken = true;
continue;
}

if (c == u'"') {
inQuotes = !inQuotes;
hasToken = true;
continue;
}

if (c.isSpace() && !inQuotes) {
if (hasToken) {
args.append(currentToken);
currentToken.clear();
hasToken = false;
} break;
case SpliterState::InSingleQuote: {
if (c == u'\'') {
curState = SpliterState::Normal;
} else {
currentToken.append(c);
}
} else {
currentToken.append(c);
hasToken = true;
} break;
}
}

if (inQuotes) {
qWarning() << "Exec parsing error: Unterminated double quote";
if (curState != SpliterState::Normal) {
qWarning() << "Exec parsing error: Unterminated quote";
return std::nullopt;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/ut_escape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ TEST(ApplicationServiceTest, SplitExecArguments_PassPhase2_Spec)
{R"(myapp --icon=%i --file %f)",
QStringList{"myapp", "--icon=%i", "--file", "%f"},
"Field codes can be part of an argument"},
{
R"(sh -c 'if [ -e %f ]; then echo %f; fi')",
QStringList{"sh", "-c", "if [ -e %f ]; then echo %f; fi"},
"Single quotes are preserved But we should handle it because we need to compatible with GIO",
},
{R"(myapp "unclosed quote)", std::nullopt, "Unclosed quote should return nullopt"},
{R"(myapp \)", std::nullopt, "Trailing backslash is illegal"}};

Expand Down
Loading