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 keepassxc-proxy shutdown with Chromium-based browsers under Windows #2142

Merged
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
2 changes: 1 addition & 1 deletion src/browser/NativeMessagingBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected slots:

protected:
virtual void readLength() = 0;
virtual void readStdIn(const quint32 length) = 0;
virtual bool readStdIn(const quint32 length) = 0;
void readNativeMessages();
QString jsonToString(const QJsonObject& json) const;
void sendReply(const QJsonObject& json);
Expand Down
7 changes: 4 additions & 3 deletions src/browser/NativeMessagingHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ void NativeMessagingHost::readLength()
}
}

void NativeMessagingHost::readStdIn(const quint32 length)
bool NativeMessagingHost::readStdIn(const quint32 length)
{
if (length <= 0) {
return;
return false;
}

QByteArray arr;
Expand All @@ -129,14 +129,15 @@ void NativeMessagingHost::readStdIn(const quint32 length)
int c = std::getchar();
if (c == EOF) {
// message ended prematurely, ignore it and return
return;
return false;
}
arr.append(static_cast<char>(c));
}

if (arr.length() > 0) {
sendReply(m_browserClients.readResponse(arr));
}
return true;
}

void NativeMessagingHost::newLocalConnection()
Expand Down
2 changes: 1 addition & 1 deletion src/browser/NativeMessagingHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public slots:

private:
void readLength();
void readStdIn(const quint32 length);
bool readStdIn(const quint32 length);
void sendReplyToAllClients(const QJsonObject& json);

private slots:
Expand Down
27 changes: 22 additions & 5 deletions src/proxy/NativeMessagingHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,36 @@ NativeMessagingHost::~NativeMessagingHost()
#endif
}

void NativeMessagingHost::readNativeMessages()
{
#ifdef Q_OS_WIN
quint32 length = 0;
while (m_running.load() && !std::cin.eof()) {
length = 0;
std::cin.read(reinterpret_cast<char*>(&length), 4);
if (!readStdIn(length)) {
QCoreApplication::quit();
}
QThread::msleep(1);
}
#endif
}

void NativeMessagingHost::readLength()
{
quint32 length = 0;
std::cin.read(reinterpret_cast<char*>(&length), 4);
if (!std::cin.eof() && length > 0) {
readStdIn(length);
} else {
QCoreApplication::quit();
QCoreApplication::quit();
}
}

void NativeMessagingHost::readStdIn(const quint32 length)
bool NativeMessagingHost::readStdIn(const quint32 length)
{
if (length <= 0) {
return;
return false;
}

QByteArray arr;
Expand All @@ -73,7 +88,7 @@ void NativeMessagingHost::readStdIn(const quint32 length)
int c = std::getchar();
if (c == EOF) {
// message ended prematurely, ignore it and return
return;
return false;
}
arr.append(static_cast<char>(c));
}
Expand All @@ -82,6 +97,8 @@ void NativeMessagingHost::readStdIn(const quint32 length)
m_localSocket->write(arr.constData(), arr.length());
m_localSocket->flush();
}

return true;
}

void NativeMessagingHost::newLocalMessage()
Expand All @@ -92,7 +109,7 @@ void NativeMessagingHost::newLocalMessage()

QByteArray arr = m_localSocket->readAll();
if (!arr.isEmpty()) {
sendReply(arr);
sendReply(arr);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/proxy/NativeMessagingHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public slots:
void socketStateChanged(QLocalSocket::LocalSocketState socketState);

private:
void readNativeMessages();
void readLength();
void readStdIn(const quint32 length);
bool readStdIn(const quint32 length);

private:
QLocalSocket* m_localSocket;
QLocalSocket* m_localSocket;
};

#endif // NATIVEMESSAGINGHOST_H