Make history and remote message handling fail closed
This commit is contained in:
+29
-9
@@ -236,15 +236,35 @@ class ConfigStore {
|
||||
fs.renameSync(tmp, this.historyPath);
|
||||
}
|
||||
|
||||
_writeHistoryFileAtomic(arr) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tmp = this.historyPath + '.tmp';
|
||||
fs.writeFile(tmp, JSON.stringify(arr), 'utf-8', (err) => {
|
||||
if (err) return reject(err);
|
||||
try { fs.renameSync(tmp, this.historyPath); } catch (e) { return reject(e); }
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
async _writeHistoryFileAtomic(arr) {
|
||||
const tmp = this.historyPath + '.tmp';
|
||||
let handle;
|
||||
let operationError;
|
||||
try {
|
||||
handle = await fs.promises.open(tmp, 'w');
|
||||
await handle.writeFile(JSON.stringify(arr), 'utf-8');
|
||||
await handle.sync();
|
||||
} catch (error) {
|
||||
operationError = error;
|
||||
}
|
||||
if (handle) {
|
||||
try {
|
||||
await handle.close();
|
||||
} catch (error) {
|
||||
if (!operationError) operationError = error;
|
||||
}
|
||||
}
|
||||
if (operationError) throw operationError;
|
||||
await fs.promises.rename(tmp, this.historyPath);
|
||||
let directoryHandle;
|
||||
try {
|
||||
directoryHandle = await fs.promises.open(path.dirname(this.historyPath), 'r');
|
||||
await directoryHandle.sync();
|
||||
} catch (error) {
|
||||
if (!['EINVAL', 'EISDIR', 'EPERM', 'ENOTSUP'].includes(error.code)) throw error;
|
||||
} finally {
|
||||
if (directoryHandle) await directoryHandle.close();
|
||||
}
|
||||
}
|
||||
|
||||
_quiescedWriteError() {
|
||||
|
||||
@@ -98,6 +98,17 @@ class RemoteServer {
|
||||
const client = this._clients.get(ws);
|
||||
if (!client) return;
|
||||
|
||||
if (!msg || typeof msg !== 'object' || Array.isArray(msg)) {
|
||||
if (!client.authenticated) {
|
||||
authReceived = true;
|
||||
clearTimeout(authTimeout);
|
||||
this._recordFailedAttempt(ip);
|
||||
ws.close(4002, 'Invalid token');
|
||||
this._clients.delete(ws);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!client.authenticated) {
|
||||
authReceived = true;
|
||||
clearTimeout(authTimeout);
|
||||
|
||||
Reference in New Issue
Block a user