Skip to content

Commit

Permalink
fix: add better escape logic for mp3 metadata with ffmpeg
Browse files Browse the repository at this point in the history
fixes #60
  • Loading branch information
lightpohl committed Dec 31, 2023
1 parent eca32b3 commit 7a4289f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion bin/util.js
Expand Up @@ -10,6 +10,22 @@ import { getArchiveFilename, getItemFilename } from "./naming.js";

const execWithPromise = util.promisify(exec);

/*
Escape arguments for a shell command used with exec.
Borrowed from shell-escape: https://github.com/xxorax/node-shell-escape/
*/
const escapeArgForShell = (arg) => {
let result = arg;
if (/[^A-Za-z0-9_/:=-]/.test(result)) {
result = "'" + result.replace(/'/g, "'\\''") + "'";
result = result
.replace(/^(?:'')+/g, "") // unduplicate single-quote at the beginning
.replace(/\\'''/g, "\\'"); // remove non-escaped single-quote if there are enclosed between 2 escaped
}

return result;
};

const getTempPath = (path) => {
return `${path}.tmp`;
};
Expand Down Expand Up @@ -507,7 +523,7 @@ const runFfmpeg = async ({
const metadataString = Object.keys(metaKeysToValues)
.map((key) =>
metaKeysToValues[key]
? `-metadata ${key}="${metaKeysToValues[key].replace(/"/g, '\\"')}"`
? `-metadata ${key}=${escapeArgForShell(metaKeysToValues[key])}`
: null
)
.filter((segment) => !!segment)
Expand Down

0 comments on commit 7a4289f

Please sign in to comment.