Skip to content

Commit

Permalink
notify: use execFile with arg array instead of exec to avoid shell-es…
Browse files Browse the repository at this point in the history
  • Loading branch information
vogler committed Nov 9, 2023
1 parent a7cc68b commit 3ddf172
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion notify-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* eslint-disable no-constant-condition */
import { delay, html_game_list, notify } from './util.js';
import { cfg } from './config.js';

const URL_CLAIM = 'https://gaming.amazon.com/home'; // dummy URL

console.debug('NOTIFY:', cfg.notify);

if (true) {
const notify_games = [
// { title: 'Kerbal Space Program', status: 'claimed', url: URL_CLAIM },
// { title: "Shadow Tactics - Aiko's Choice", status: 'claimed', url: URL_CLAIM },
{ title: 'Epistory - Typing Chronicles', status: 'claimed', url: URL_CLAIM },
];
notify(`epic-games:<br>${html_game_list(notify_games)}`);
await notify(`epic-games:<br>${html_game_list(notify_games)}`);
}

if (false) {
Expand Down
14 changes: 10 additions & 4 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ export const prompt = o => enquirer.prompt({ name: 'name', type: 'input', messag
export const confirm = o => prompt({ type: 'confirm', message: 'Continue?', ...o });

// notifications via apprise CLI
import { exec } from 'child_process';
import { execFile } from 'child_process';
import { cfg } from './config.js';

export const notify = html => new Promise((resolve, reject) => {
if (!cfg.notify) return resolve();
const title = cfg.notify_title ? `-t ${cfg.notify_title}` : '';
exec(`apprise ${cfg.notify} -i html '${title}' -b '${html}'`, (error, stdout, stderr) => {
if (!cfg.notify) {
if (cfg.debug) console.debug('notify: NOTIFY is not set!');
return resolve();
}
// const cmd = `apprise '${cfg.notify}' ${title} -i html -b '${html}'`; // this had problems if e.g. ' was used in arg; could have `npm i shell-escape`, but instead using safer execFile which takes args as array instead of exec which spawned a shell to execute the command
const args = [cfg.notify, '-i', 'html', '-b', html];
if (cfg.notify_title) args.push(...['-t', cfg.notify_title]);
if (cfg.debug) console.debug(`apprise ${args.map(a => `'${a}'`).join(' ')}`); // this also doesn't escape, but it's just for info
execFile('apprise', args, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
if (error.message.includes('command not found')) {
Expand Down

0 comments on commit 3ddf172

Please sign in to comment.