Skip to content

Commit f7ab587

Browse files
committed
Add extra safety checks to server deletion
1 parent 826835e commit f7ab587

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/commands/start.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class HttpToolkitServer extends Command {
6565
path.join(dataDir, 'client');
6666

6767
// Be careful - if the server path isn't clearly ours somehow, ignore it.
68-
if (!serverUpdatesPath.split(path.sep).includes('httptoolkit-server')) {
69-
reportError(`Unexpected server path (${serverUpdatesPath}), ignoring`);
68+
if (!isOwnedPath(serverUpdatesPath)) {
69+
reportError(`Unexpected server updates path (${serverUpdatesPath}), ignoring`);
7070
return;
7171
}
7272

@@ -114,6 +114,7 @@ class HttpToolkitServer extends Command {
114114
}
115115
}
116116

117+
// Delete a folder recursively, with checks to ensure its safe to do so at every stage
117118
async function deleteFolder(folder: string) {
118119
const contents: string[] = await fs.readdir(folder)
119120
.catch((e) => {
@@ -125,14 +126,25 @@ async function deleteFolder(folder: string) {
125126
contents.map(async (filename) => {
126127
const filePath = path.join(folder, filename);
127128
if ((await fs.lstat(filePath)).isDirectory()) {
128-
await deleteFolder(filePath);
129-
} else {
129+
await deleteFolder(filePath); // Recurse
130+
} else if (isOwnedPath(filePath)) {
130131
await fs.unlink(filePath);
131132
}
132133
})
133134
);
134135

135-
await fs.rmdir(folder);
136+
if (isOwnedPath(folder)) await fs.rmdir(folder);
136137
};
137138

139+
// Before deleting anything anywhere, we check it's an HTK-related path.
140+
// Not a perfect check, but good safety against somehow deleting / or similar.
141+
function isOwnedPath(input: string) {
142+
if (input.split(path.sep).includes('httptoolkit-server')) {
143+
return true;
144+
} else {
145+
reportError(`Unexpected unowned path ${input}`);
146+
return false;
147+
}
148+
}
149+
138150
export = HttpToolkitServer;

0 commit comments

Comments
 (0)