Skip to content

Commit 5162415

Browse files
committed
webui: escape superkey in shell call
#1
1 parent 6f93165 commit 5162415

4 files changed

Lines changed: 22 additions & 16 deletions

File tree

webui/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,22 @@ async function updateStatus() {
4040
}
4141
}
4242

43+
export function escapeShell(cmd) {
44+
if (cmd === '' || cmd === null || cmd === undefined) return "''";
45+
return "'" + String(cmd).replace(/'/g, "'\\''") + "'";
46+
}
47+
4348
function updateSuperkey(key) {
4449
superkey = key;
4550
document.querySelectorAll('.password-field').forEach(field => {
4651
field.value = key;
4752
});
4853
localStorage.setItem('kp-next_superkey', key);
54+
const safeKey = escapeShell(key);
4955
exec(`
50-
key="${key}"
56+
key=${safeKey}
5157
if [ -n "$key" ]; then
52-
echo '${key}' | base64 -w0 > /data/adb/kp-next/key
58+
printf "%s" "$key" | base64 -w0 > /data/adb/kp-next/key
5359
if [ -f "${modDir}/unresolved" ]; then
5460
rm -f "${modDir}/unresolved"
5561
busybox nohup sh "${modDir}/service.sh" &

webui/page/exclude.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { listPackages, getPackagesInfo, exec } from 'kernelsu-alt';
2-
import { modDir, persistDir, superkey } from '../index.js';
2+
import { modDir, persistDir, superkey, escapeShell } from '../index.js';
33
import { getString } from '../language.js';
44
import fallbackIcon from '../icon.png';
55

@@ -217,7 +217,7 @@ async function renderAppList() {
217217
saveTimeout = setTimeout(() => {
218218
saveExcludedList(excludedApps);
219219
}, 500);
220-
exec(`kpatch '${superkey}' exclude_set ${realUid} ${isSelected ? 1 : 0}`, { env: { PATH: `${modDir}/bin` } });
220+
exec(`kpatch ${escapeShell(superkey)} exclude_set ${realUid} ${isSelected ? 1 : 0}`, { env: { PATH: `${modDir}/bin` } });
221221
});
222222

223223
appItemMap.set(appKey, item);

webui/page/kpm.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { exec, spawn, toast } from 'kernelsu-alt';
2-
import { modDir, persistDir, superkey, initInfo, MAX_CHUNK_SIZE, linkRedirect } from '../index.js';
2+
import { modDir, persistDir, superkey, initInfo, MAX_CHUNK_SIZE, linkRedirect, escapeShell } from '../index.js';
33
import { getString } from '../language.js';
44

55
let allKpms = [];
@@ -46,13 +46,13 @@ async function getKpmList() {
4646
}
4747

4848
const listResult = await exec(
49-
`kpatch '${superkey}' kpm list && sh "${modDir}/status.sh" '${superkey}'`,
49+
`kpatch ${escapeShell(superkey)} kpm list && sh "${modDir}/status.sh" ${escapeShell(superkey)}`,
5050
{ env: { PATH: `${modDir}/bin:$PATH` } }
5151
);
5252
const modules = listResult.stdout.trim().split('\n').filter(line => line.trim());
5353

5454
const modulePromises = modules.map(async (moduleName) => {
55-
const infoResult = await exec(`kpatch '${superkey}' kpm info "${moduleName}"`, { env: { PATH: `${modDir}/bin` } });
55+
const infoResult = await exec(`kpatch ${escapeShell(superkey)} kpm info "${moduleName}"`, { env: { PATH: `${modDir}/bin` } });
5656
const infoLines = infoResult.stdout.trim().split('\n');
5757

5858
const moduleInfo = {};
@@ -69,7 +69,7 @@ async function getKpmList() {
6969
}
7070

7171
async function controlModule(moduleName, action) {
72-
const result = await exec(`kpatch '${superkey}' kpm ctl0 "${moduleName}" ${action}`, { env: { PATH: `${modDir}/bin` } });
72+
const result = await exec(`kpatch ${escapeShell(superkey)} kpm ctl0 "${moduleName}" ${action}`, { env: { PATH: `${modDir}/bin` } });
7373
toast(result.errno === 0 ? result.stdout : result.stderr);
7474
}
7575

@@ -79,12 +79,12 @@ function forgetModule(moduleName) {
7979

8080
async function unloadModule(moduleName) {
8181
forgetModule(moduleName);
82-
const result = await exec(`kpatch '${superkey}' kpm unload "${moduleName}"`, { env: { PATH: `${modDir}/bin` } });
82+
const result = await exec(`kpatch ${escapeShell(superkey)} kpm unload "${moduleName}"`, { env: { PATH: `${modDir}/bin` } });
8383
return result.errno === 0;
8484
}
8585

8686
async function loadModule(modulePath) {
87-
const result = await exec(`kpatch '${superkey}' kpm load "${modulePath}"`, { env: { PATH: `${modDir}/bin` } });
87+
const result = await exec(`kpatch ${escapeShell(superkey)} kpm load "${modulePath}"`, { env: { PATH: `${modDir}/bin` } });
8888
return result.errno === 0;
8989
}
9090

webui/page/patch.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { exec, spawn, toast } from 'kernelsu-alt';
2-
import { modDir, superkey } from '../index.js';
2+
import { modDir, superkey, escapeShell } from '../index.js';
33
import { handleFileUpload, uploadFile } from './kpm.js';
44
import { getString } from '../language.js';
55

@@ -39,9 +39,9 @@ function parseIni(str) {
3939
async function getInstalledVersion() {
4040
if (superkey === '') return null;
4141
if (import.meta.env.DEV) return uInt2String('c06');
42-
const working = await exec(`kpatch '${superkey}' hello`, { env: { PATH: `${modDir}/bin` } });
42+
const working = await exec(`kpatch ${escapeShell(superkey)} hello`, { env: { PATH: `${modDir}/bin` } });
4343
if (working.stdout.trim() === '') return null;
44-
const version = await exec(`kpatch '${superkey}' kpver`, { env: { PATH: `${modDir}/bin` } });
44+
const version = await exec(`kpatch ${escapeShell(superkey)} kpver`, { env: { PATH: `${modDir}/bin` } });
4545
return uInt2String(version.stdout.trim());
4646
}
4747

@@ -337,23 +337,23 @@ function patch(type) {
337337
if (type === "patch") {
338338
args.push(
339339
`${modDir}/boot_patch.sh`,
340-
`'${superkeyVal}'`,
340+
escapeShell(superkeyVal),
341341
bootDev,
342342
'true'
343343
);
344344

345345
// New kpm
346346
newExtras.forEach(extra => {
347347
args.push('-M', `${modDir}/tmp/${extra.fileName}`);
348-
if (extra.args) args.push('-A', extra.args);
348+
if (extra.args) args.push('-A', escapeShell(extra.args));
349349
if (extra.event) args.push('-V', extra.event);
350350
args.push('-T', 'kpm');
351351
});
352352

353353
// Embeded kpm
354354
existedExtras.forEach(extra => {
355355
args.push('-E', extra.name);
356-
if (extra.args) args.push('-A', extra.args);
356+
if (extra.args) args.push('-A', escapeShell(extra.args));
357357
if (extra.event) args.push('-V', extra.event);
358358
args.push('-T', 'kpm');
359359
});

0 commit comments

Comments
 (0)