Skip to content

Commit

Permalink
change logic when sanitizing
Browse files Browse the repository at this point in the history
- disallow slashes on windows
- check for file name ending with a space or a dot

closes #1790
  • Loading branch information
mifi committed Dec 5, 2023
1 parent ae8f6d4 commit aa1958c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/OutSegTemplateEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const OutSegTemplateEditor = memo(({ outSegTemplate, setOutSegTemplate, generate
))}
</div>

{error != null && <div style={{ marginBottom: '1em' }}><ErrorIcon color="var(--red9)" size={14} verticalAlign="baseline" /> {i18n.t('There is an error in the file name template:')} {error}</div>}
{error != null && <div style={{ marginBottom: '1em' }}><ErrorIcon color="var(--red9)" size={14} verticalAlign="baseline" /> {error}</div>}

{isMissingExtension && (
<div style={{ marginBottom: '1em' }}>
Expand Down
28 changes: 22 additions & 6 deletions src/util/outputNameTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,37 @@ function getOutSegError({ fileNames, filePath, outputDir, safeOutputFileName })
// eslint-disable-next-line no-restricted-syntax
for (const fileName of fileNames) {
if (!filePath) {
error = 'No file path';
error = i18n.t('No file is loaded');
break;
}

const invalidChars = new Set();

// https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
// note that we allow path separators!
// note that we allow path separators in some cases (see below)
if (isWindows) {
['<', '>', ':', '"', '|', '?', '*'].forEach((char) => invalidChars.add(char));
} else if (isMac) {
// Colon is invalid on windows https://github.com/mifi/lossless-cut/issues/631 and on MacOS, but not Linux https://github.com/mifi/lossless-cut/issues/830
[':'].forEach((char) => invalidChars.add(char));
}

if (safeOutputFileName) invalidChars.add(pathSep);
if (safeOutputFileName) {
if (isWindows) {
// Only when sanitize is "off" shall we allow slashes (you're on your own)
invalidChars.add('/');
invalidChars.add('\\');
} else {
invalidChars.add(pathSep);
}
}

const outPath = pathNormalize(pathJoin(outputDir, fileName));
const sameAsInputPath = outPath === pathNormalize(filePath);
const inPathNormalized = pathNormalize(filePath);
const outPathNormalized = pathNormalize(pathJoin(outputDir, fileName));
const sameAsInputPath = outPathNormalized === inPathNormalized;
const windowsMaxPathLength = 259;
const shouldCheckPathLength = isWindows || isDev;
const shouldCheckFileEnd = isWindows || isDev;

if (fileName.length === 0) {
error = i18n.t('At least one resulting file name has no length');
Expand All @@ -51,7 +61,13 @@ function getOutSegError({ fileNames, filePath, outputDir, safeOutputFileName })
error = i18n.t('At least one resulting file name is the same as the input path');
break;
}
if (shouldCheckPathLength && outPath.length >= windowsMaxPathLength) {
if (shouldCheckFileEnd && /[\s.]$/.test(fileName)) {
// Filenames cannot end in a space or dot on windows
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
error = i18n.t('At least one resulting file name ends with a whitespace character or a dot, which is not allowed.');
break;
}
if (shouldCheckPathLength && outPathNormalized.length >= windowsMaxPathLength) {
error = i18n.t('At least one resulting file will have a too long path');
break;
}
Expand Down

0 comments on commit aa1958c

Please sign in to comment.