Fix issue where custom separator could be the same as a character in the prefix

This commit is contained in:
Alan Grainger
2026-10-20 09:28:04 +13:00
parent 94cfa9972b
commit fc7670157b
11 changed files with 36 additions and 35 deletions
+1 -3
View File
@@ -15,9 +15,7 @@
"strictNullChecks": true, "strictNullChecks": true,
"lib": [ "lib": [
"DOM", "DOM",
"ES5", "ES2021"
"ES6",
"ES7"
], ],
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"skipLibCheck": true, "skipLibCheck": true,
+5 -4
View File
@@ -117,8 +117,8 @@ visitor's own claim; IPP cannot check it. For what to do with it, see
**Type:** `string` · **Default:** `","` **Type:** `string` · **Default:** `","`
The characters that end each visitor field in a stored filename, between the field and the original name, as in The single character that ends each visitor field in a stored filename, between the field and the original name, as
`ipp_upload_jane-smith,IMG_0042.jpg`. The visitor's name is the only field today. The separator is removed from the in `ipp_upload_jane-smith,IMG_0042.jpg`. The visitor's name is the only field today. The separator is removed from the
visitor's own filename, so it always marks the end of a field. A file sent with no fields has no separator at all. visitor's own filename, so it always marks the end of a field. A file sent with no fields has no separator at all.
A comma reads naturally, but some people name their photos with one, as in `Venice, Italy.jpg`, and that comma is A comma reads naturally, but some people name their photos with one, as in `Venice, Italy.jpg`, and that comma is
@@ -134,8 +134,9 @@ lost. If your visitors do, set `"^"`: it is never written by a camera or a phone
} }
``` ```
Any punctuation that is legal in a filename works, with one or more characters. A letter, digit, hyphen, dot, space Any one punctuation character that is legal in a filename works. More than one character, a letter, digit, hyphen,
or a character that is illegal in a filename (`/ ? < > \ : * | "`) is not accepted, and the default is used instead. dot, space, a character that is illegal in a filename (`/ ? < > \ : * | "`) or a character in
[`filenamePrefix`](#filenameprefix) is not accepted, and the default is used instead.
Changing it later leaves the files already stored with the old one. How the filename is read back is explained in Changing it later leaves the files already stored with the old one. How the filename is read back is explained in
[The separator](/tag-and-review-uploads#the-separator). [The separator](/tag-and-review-uploads#the-separator).
+1 -1
View File
@@ -113,7 +113,7 @@ photo, so the script stops unless you pass `--prefix`.
| `--apply` | off | Add the tags. Without it, the script only reports. | | `--apply` | off | Add the tags. Without it, the script only reports. |
| `--parent <tag>` | `Uploaded` | The parent tag. `--parent "Guests/Wedding"` gives `Guests/Wedding/jane-smith`. | | `--parent <tag>` | `Uploaded` | The parent tag. `--parent "Guests/Wedding"` gives `Guests/Wedding/jane-smith`. |
| `--prefix <text>` | `upload.filenamePrefix` | The prefix the files were stored with, if it differs from the current setting. | | `--prefix <text>` | `upload.filenamePrefix` | The prefix the files were stored with, if it differs from the current setting. |
| `--separator <chars>` | `upload.fieldSeparator` | The separator the files were stored with, if it differs from the current setting. Quote it: `'^'`. | | `--separator <char>` | `upload.fieldSeparator` | The separator the files were stored with, if it differs from the current setting. Quote it: `'^'`. |
### Files uploaded with IPP 4.0.0 ### Files uploaded with IPP 4.0.0
+1 -3
View File
@@ -17,9 +17,7 @@
"strictNullChecks": true, "strictNullChecks": true,
"lib": [ "lib": [
"DOM", "DOM",
"ES5", "ES2021"
"ES6",
"ES7"
], ],
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"skipLibCheck": true, "skipLibCheck": true,
+2 -2
View File
@@ -41,11 +41,11 @@ export function askName (): boolean {
/** /**
* The string that ends each visitor field in a stored filename, from * The string that ends each visitor field in a stored filename, from
* `ipp.upload.fieldSeparator`. The default when the configured value could be * `ipp.upload.fieldSeparator`. The default when the configured value could be
* part of a field or a filename, see `isUsableSeparator`. * part of a field or a filename, or is in the prefix, see `isUsableSeparator`.
*/ */
export function fieldSeparator (): string { export function fieldSeparator (): string {
const configured = getConfigOption('ipp.upload.fieldSeparator', DEFAULT_FIELD_SEPARATOR) const configured = getConfigOption('ipp.upload.fieldSeparator', DEFAULT_FIELD_SEPARATOR)
return isUsableSeparator(configured) ? configured : DEFAULT_FIELD_SEPARATOR return isUsableSeparator(configured, filenamePrefix()) ? configured : DEFAULT_FIELD_SEPARATOR
} }
/** Text shown under the placeholder on the upload page, from `ipp.upload.notice`. Empty hides it. */ /** Text shown under the placeholder on the upload page, from `ipp.upload.notice`. Empty hides it. */
+7 -8
View File
@@ -25,14 +25,13 @@ const MAX_UPLOADER_LENGTH = 32
export const DEFAULT_FIELD_SEPARATOR = ',' export const DEFAULT_FIELD_SEPARATOR = ','
/** /**
* Whether `separator` can mark the end of a field: a string that `sanitize` * Whether `separator` can end a field: one character, kept by `sanitize`, not
* leaves alone, with no slug character, dot or whitespace in it. A letter, * in `prefix`, and never part of a slug, an extension or a trimmed stem.
* digit or hyphen could be part of the name, a dot could be read as the
* extension, and whitespace would be trimmed from the ends of a stem.
*/ */
export function isUsableSeparator (separator: unknown): separator is string { export function isUsableSeparator (separator: unknown, prefix: string): separator is string {
if (typeof separator !== 'string' || separator === '') return false // Counted by code point, so an emoji is one character
if (sanitize(separator) !== separator) return false if (typeof separator !== 'string' || [...separator].length !== 1) return false
if (sanitize(separator) !== separator || storedPrefix(prefix).includes(separator)) return false
return !/[\p{L}\p{N}\-.\s]/u.test(separator) return !/[\p{L}\p{N}\-.\s]/u.test(separator)
} }
@@ -79,7 +78,7 @@ export function uploadFilename (raw: unknown, prefix: string, uploader = '', sep
if (!parts) return undefined if (!parts) return undefined
const extension = parts[2] const extension = parts[2]
// The separator is removed from the visitor's name so it only ever marks a field // The separator is removed from the visitor's name so it only ever marks a field
const stem = sanitize(parts[1]).split(separator).join('').trim() const stem = sanitize(parts[1]).replaceAll(separator, '').trim()
// An empty stem is a dotfile; a leading dot is a traversal remnant. // An empty stem is a dotfile; a leading dot is a traversal remnant.
if (!stem || stem.startsWith('.')) return undefined if (!stem || stem.startsWith('.')) return undefined
+3 -3
View File
@@ -28,12 +28,12 @@ const PERMISSIONS = 'asset.read, user.read, tag.create and tag.asset'
const USAGE = `Tag visitor uploads in Immich by the name in their filename. const USAGE = `Tag visitor uploads in Immich by the name in their filename.
Usage: node dist/tag-uploads.js [--apply] [--parent <tag>] [--prefix <text>] [--separator <chars>] Usage: node dist/tag-uploads.js [--apply] [--parent <tag>] [--prefix <text>] [--separator <char>]
--apply Add the tags. Without it, only report what would be tagged. --apply Add the tags. Without it, only report what would be tagged.
--parent <tag> Parent tag for the visitor tags. Default "${DEFAULT_PARENT}". --parent <tag> Parent tag for the visitor tags. Default "${DEFAULT_PARENT}".
--prefix <text> Filename prefix to look for. Default ipp.upload.filenamePrefix. --prefix <text> Filename prefix to look for. Default ipp.upload.filenamePrefix.
--separator <chars> Field separator to read. Default ipp.upload.fieldSeparator. --separator <char> Field separator to read. Default ipp.upload.fieldSeparator.
IMMICH_API_KEY must hold an API key of the share owner, with the permissions IMMICH_API_KEY must hold an API key of the share owner, with the permissions
${PERMISSIONS}.` ${PERMISSIONS}.`
@@ -235,7 +235,7 @@ export async function main (argv: string[]): Promise<number> {
'Pass --prefix with the prefix the files were stored with.') 'Pass --prefix with the prefix the files were stored with.')
} }
const separator = values.separator ?? fieldSeparator() const separator = values.separator ?? fieldSeparator()
if (!isUsableSeparator(separator)) return usage('"' + separator + '" cannot be a field separator.') if (!isUsableSeparator(separator, prefix)) return usage('"' + separator + '" cannot be a field separator with the prefix "' + prefix + '".')
const parent = (values.parent ?? DEFAULT_PARENT).split('/').map(part => part.trim()).filter(Boolean).join('/') const parent = (values.parent ?? DEFAULT_PARENT).split('/').map(part => part.trim()).filter(Boolean).join('/')
if (!parent) return usage('The parent tag is empty.') if (!parent) return usage('The parent tag is empty.')
+1 -1
View File
@@ -20,7 +20,7 @@ describe('upload options', () => {
it('takes a usable field separator, and falls back for one that could be part of a field', () => { it('takes a usable field separator, and falls back for one that could be part of a field', () => {
setConfig({ ipp: { upload: { fieldSeparator: '^' } } }) setConfig({ ipp: { upload: { fieldSeparator: '^' } } })
expect(fieldSeparator()).toBe('^') expect(fieldSeparator()).toBe('^')
for (const bad of ['', '-', 'a', '.', ' ', '/', 7, null]) { for (const bad of ['', '-', 'a', '.', ' ', '/', '^^', '_', 7, null]) {
setConfig({ ipp: { upload: { fieldSeparator: bad } } }) setConfig({ ipp: { upload: { fieldSeparator: bad } } })
expect(fieldSeparator()).toBe(',') expect(fieldSeparator()).toBe(',')
} }
+12 -7
View File
@@ -66,7 +66,6 @@ describe('uploadFilename', () => {
it('uses the configured separator, and removes that one instead', () => { it('uses the configured separator, and removes that one instead', () => {
expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_', 'jane-smith', '^')).toBe('ipp_upload_jane-smith^IMG_0042.jpg') expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_', 'jane-smith', '^')).toBe('ipp_upload_jane-smith^IMG_0042.jpg')
expect(uploadFilename('Paris, France^2.jpg', 'ipp_upload_', 'jane-smith', '^')).toBe('ipp_upload_jane-smith^Paris, France2.jpg') expect(uploadFilename('Paris, France^2.jpg', 'ipp_upload_', 'jane-smith', '^')).toBe('ipp_upload_jane-smith^Paris, France2.jpg')
expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_', 'jane-smith', '~~')).toBe('ipp_upload_jane-smith~~IMG_0042.jpg')
}) })
it('stays within 254 characters with the extension intact', () => { it('stays within 254 characters with the extension intact', () => {
@@ -113,14 +112,20 @@ describe('uploaderSlug', () => {
describe('isUsableSeparator', () => { describe('isUsableSeparator', () => {
it('accepts punctuation that sanitize keeps', () => { it('accepts punctuation that sanitize keeps', () => {
for (const separator of ['^', ',', '~', '#', '+', '^^', '~,']) expect(isUsableSeparator(separator)).toBe(true) for (const separator of ['^', ',', '~', '#', '+']) expect(isUsableSeparator(separator, 'ipp_upload_')).toBe(true)
}) })
it('rejects anything that could be part of a name or a filename, or is not a string', () => { it('rejects more than one character, anything that could be part of a name or a filename, or a non-string', () => {
for (const separator of ['', 'a', '1', '-', '.', ' ', 'x^', '^ ', '/', ':', '*', '"', '\u0000', 'ñ', '٣']) { for (const separator of ['', 'a', '1', '-', '.', ' ', 'x^', '^ ', '/', ':', '*', '"', '\u0000', 'ñ', '٣', '^^', '~,', '#!']) {
expect(isUsableSeparator(separator)).toBe(false) expect(isUsableSeparator(separator, 'ipp_upload_')).toBe(false)
} }
expect(isUsableSeparator(undefined)).toBe(false) expect(isUsableSeparator(undefined, 'ipp_upload_')).toBe(false)
expect(isUsableSeparator(1)).toBe(false) expect(isUsableSeparator(1, 'ipp_upload_')).toBe(false)
})
it('rejects a character in the prefix, which would hide the prefix from the stacking check', () => {
expect(isUsableSeparator('_', 'ipp_upload_')).toBe(false)
expect(isUsableSeparator(',', 'ipp,upload ')).toBe(false)
expect(isUsableSeparator('_', 'ipp-upload-')).toBe(true)
}) })
}) })
+2
View File
@@ -202,6 +202,8 @@ describe('main', () => {
it('refuses an unknown flag or an unusable separator', async () => { it('refuses an unknown flag or an unusable separator', async () => {
expect(await main(['--aply'])).toBe(2) expect(await main(['--aply'])).toBe(2)
expect(await main(['--separator', '-'])).toBe(2) expect(await main(['--separator', '-'])).toBe(2)
expect(await main(['--separator', '_'])).toBe(2)
expect(errors[errors.length - 1]).toMatch(/with the prefix "ipp_upload_"/)
}) })
it('reads the separator from config, and exits 1 on an Immich error', async () => { it('reads the separator from config, and exits 1 on an Immich error', async () => {
+1 -3
View File
@@ -15,9 +15,7 @@
"strictNullChecks": true, "strictNullChecks": true,
"lib": [ "lib": [
"DOM", "DOM",
"ES5", "ES2021"
"ES6",
"ES7"
], ],
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"skipLibCheck": true, "skipLibCheck": true,