Initial work on upload app server
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { describe, it, expect, afterEach } from 'vitest'
|
||||
import { basePathFrom, byteBudget, filenamePrefix, maxFileSize, notifyConfig } from '../src/config'
|
||||
import { resetConfig, setConfig } from './helpers'
|
||||
|
||||
afterEach(resetConfig)
|
||||
|
||||
const MB = 1024 * 1024
|
||||
|
||||
describe('basePathFrom', () => {
|
||||
it('is empty for an own-hostname deployment', () => {
|
||||
expect(basePathFrom('https://upload.example.com')).toBe('')
|
||||
expect(basePathFrom('https://upload.example.com/')).toBe('')
|
||||
expect(basePathFrom(undefined)).toBe('')
|
||||
})
|
||||
|
||||
it('is the URL path for a path-prefix deployment, without a trailing slash', () => {
|
||||
expect(basePathFrom('https://photos.example.com/upload')).toBe('/upload')
|
||||
expect(basePathFrom('https://photos.example.com/a/b/')).toBe('/a/b')
|
||||
})
|
||||
|
||||
it('is empty for a value that is not a URL', () => {
|
||||
expect(basePathFrom('not a url')).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('upload options', () => {
|
||||
it('has the documented defaults', () => {
|
||||
expect(maxFileSize()).toBe(500 * MB)
|
||||
expect(byteBudget()).toBe(10240 * MB)
|
||||
expect(filenamePrefix()).toBe('ipp_upload_')
|
||||
expect(notifyConfig()).toBeUndefined()
|
||||
})
|
||||
|
||||
it('never lets maxFileSize reach zero, which would refuse every file', () => {
|
||||
setConfig({ ipp: { upload: { maxFileSize: 0 } } })
|
||||
expect(maxFileSize()).toBe(MB)
|
||||
})
|
||||
|
||||
it('keeps only string notification headers', () => {
|
||||
setConfig({ ipp: { upload: { notifyUrl: 'https://ntfy.example.com/topic', notifyHeaders: { Authorization: 'Bearer x', Bad: 1 } } } })
|
||||
expect(notifyConfig()).toEqual({ url: 'https://ntfy.example.com/topic', headers: { Authorization: 'Bearer x' }, timeoutMs: 10_000 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { uploadFilename } from '../src/filename'
|
||||
|
||||
describe('uploadFilename', () => {
|
||||
it('keeps an ordinary filename', () => {
|
||||
expect(uploadFilename('IMG_0042.jpg', '')).toBe('IMG_0042.jpg')
|
||||
})
|
||||
|
||||
it('strips path components so traversal cannot survive', () => {
|
||||
expect(uploadFilename('../../etc/passwd.jpg', '')).toBe('passwd.jpg')
|
||||
expect(uploadFilename('C:\\Users\\bob\\holiday.png', '')).toBe('holiday.png')
|
||||
})
|
||||
|
||||
it('removes characters that are illegal in a filename or would break the multipart header', () => {
|
||||
expect(uploadFilename('we"rd:na*me.jpg', '')).toBe('werdname.jpg')
|
||||
})
|
||||
|
||||
it('rejects a name with no extension', () => {
|
||||
expect(uploadFilename('photo', '')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('rejects dotfiles and non-string input', () => {
|
||||
expect(uploadFilename('.htaccess', '')).toBeUndefined()
|
||||
expect(uploadFilename('', '')).toBeUndefined()
|
||||
expect(uploadFilename(undefined, '')).toBeUndefined()
|
||||
expect(uploadFilename(['a.jpg'], '')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('applies the prefix once', () => {
|
||||
expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_')).toBe('ipp_upload_IMG_0042.jpg')
|
||||
expect(uploadFilename('ipp_upload_IMG_0042.jpg', 'ipp_upload_')).toBe('ipp_upload_IMG_0042.jpg')
|
||||
})
|
||||
|
||||
it('sanitises an operator prefix that would break the filename', () => {
|
||||
expect(uploadFilename('IMG_0042.jpg', '../ev"il/')).toBe('..evilIMG_0042.jpg')
|
||||
})
|
||||
|
||||
it('stays within 254 characters with the extension intact', () => {
|
||||
const result = uploadFilename('x'.repeat(400) + '.jpg', 'ipp_upload_') || ''
|
||||
expect(result.startsWith('ipp_upload_')).toBe(true)
|
||||
expect(result.endsWith('.jpg')).toBe(true)
|
||||
expect(result.length).toBeLessThanOrEqual(254)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,23 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { NOT_ACCEPTING_UPLOADS, replyForImmichStatus } from '../src/forward'
|
||||
|
||||
describe('replyForImmichStatus', () => {
|
||||
it('reports a new asset and a duplicate as success', () => {
|
||||
expect(replyForImmichStatus(201, { id: 'a1', status: 'created' })).toMatchObject({ ok: true, status: 'created', id: 'a1' })
|
||||
expect(replyForImmichStatus(200, { id: 'a1', status: 'duplicate' })).toMatchObject({ ok: true, status: 'duplicate', id: 'a1' })
|
||||
})
|
||||
|
||||
it('passes Immich refusing the file through as 400', () => {
|
||||
expect(replyForImmichStatus(400)).toMatchObject({ ok: false, status: 400 })
|
||||
})
|
||||
|
||||
it('reads 401 and 403 as the share no longer accepting uploads', () => {
|
||||
expect(replyForImmichStatus(401)).toEqual({ ok: false, status: 403, reason: NOT_ACCEPTING_UPLOADS })
|
||||
expect(replyForImmichStatus(403)).toEqual({ ok: false, status: 403, reason: NOT_ACCEPTING_UPLOADS })
|
||||
})
|
||||
|
||||
it('reports anything else as an upstream failure', () => {
|
||||
expect(replyForImmichStatus(500)).toMatchObject({ ok: false, status: 502 })
|
||||
expect(replyForImmichStatus(413)).toMatchObject({ ok: false, status: 502 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect, afterEach } from 'vitest'
|
||||
import { KeyType } from '@ipp/core'
|
||||
import { assertPermit, authoriseUpload, UploadPermit } from '../src/gate'
|
||||
import { resetConfig, setConfig, share } from './helpers'
|
||||
|
||||
afterEach(resetConfig)
|
||||
|
||||
describe('authoriseUpload', () => {
|
||||
it('issues a permit only when the share\'s Immich toggle is on', () => {
|
||||
expect(authoriseUpload(share(), 'k', KeyType.key)).not.toBeNull()
|
||||
expect(authoriseUpload(share({ allowUpload: false }), 'k', KeyType.key)).toBeNull()
|
||||
expect(authoriseUpload(share({ allowUpload: undefined }), 'k', KeyType.key)).toBeNull()
|
||||
})
|
||||
|
||||
it('refuses slug links when the operator has turned them off', () => {
|
||||
setConfig({ ipp: { allowSlugLinks: false } })
|
||||
expect(authoriseUpload(share(), 'my-slug', KeyType.slug)).toBeNull()
|
||||
expect(authoriseUpload(share(), 'k', KeyType.key)).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('assertPermit', () => {
|
||||
it('passes a permit from authoriseUpload', () => {
|
||||
expect(() => assertPermit(authoriseUpload(share(), 'k', KeyType.key)!)).not.toThrow()
|
||||
})
|
||||
|
||||
it('throws for a permit built by hand', () => {
|
||||
const forged = { link: share(), key: 'k', keyType: KeyType.key } as unknown as UploadPermit
|
||||
expect(() => assertPermit(forged)).toThrow('without a permit')
|
||||
})
|
||||
|
||||
it('throws when the share stopped accepting uploads after the permit was issued', () => {
|
||||
const link = share()
|
||||
const permit = authoriseUpload(link, 'k', KeyType.key)!
|
||||
link.allowUpload = false
|
||||
expect(() => assertPermit(permit)).toThrow('does not allow uploads')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,26 @@
|
||||
import { AlbumType, KeyType, loadConfig, SharedLink } from '@ipp/core'
|
||||
|
||||
/** Load `config` as if it came from the `CONFIG` env var. */
|
||||
export function setConfig (config: Record<string, unknown>) {
|
||||
process.env.CONFIG = JSON.stringify(config)
|
||||
loadConfig()
|
||||
}
|
||||
|
||||
export function resetConfig () {
|
||||
delete process.env.CONFIG
|
||||
loadConfig()
|
||||
}
|
||||
|
||||
export function share (overrides: Partial<SharedLink> = {}): SharedLink {
|
||||
return {
|
||||
id: 'ffffffff-0000-0000-0000-000000000001',
|
||||
key: 'share-key',
|
||||
keyType: KeyType.key,
|
||||
type: AlbumType.album,
|
||||
assets: [],
|
||||
expiresAt: null,
|
||||
allowUpload: true,
|
||||
album: { id: 'aaaaaaaa-0000-0000-0000-000000000002', albumName: 'Holiday 2025' },
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { createWindowBudget } from '../src/limits'
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
describe('createWindowBudget as a rate limit', () => {
|
||||
it('admits up to the limit, then refuses with the seconds left in the window', () => {
|
||||
vi.useFakeTimers()
|
||||
const rate = createWindowBudget({ limit: 3, windowMs: 60_000 })
|
||||
expect([1, 2, 3].map(() => rate('ip:share').ok)).toEqual([true, true, true])
|
||||
vi.advanceTimersByTime(20_000)
|
||||
expect(rate('ip:share')).toEqual({ ok: false, retryAfterSeconds: 40 })
|
||||
})
|
||||
|
||||
it('counts only admitted requests', () => {
|
||||
const rate = createWindowBudget({ limit: 1, windowMs: 60_000 })
|
||||
rate('a')
|
||||
rate('a')
|
||||
rate('a')
|
||||
expect(rate('b').ok).toBe(true)
|
||||
})
|
||||
|
||||
it('starts a fresh window once the old one has passed', () => {
|
||||
vi.useFakeTimers()
|
||||
const rate = createWindowBudget({ limit: 1, windowMs: 60_000 })
|
||||
expect(rate('a').ok).toBe(true)
|
||||
expect(rate('a').ok).toBe(false)
|
||||
vi.advanceTimersByTime(60_000)
|
||||
expect(rate('a').ok).toBe(true)
|
||||
})
|
||||
|
||||
it('treats a limit of 0 as no limit', () => {
|
||||
const rate = createWindowBudget({ limit: 0, windowMs: 60_000 })
|
||||
expect([1, 2, 3, 4].every(() => rate('a').ok)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('createWindowBudget as a byte budget', () => {
|
||||
it('reserves on admission, so parallel uploads cannot overshoot together', () => {
|
||||
const budget = createWindowBudget({ limit: 100, windowMs: 3_600_000 })
|
||||
expect(budget('share', 60).ok).toBe(true)
|
||||
expect(budget('share', 60).ok).toBe(false)
|
||||
expect(budget('share', 40).ok).toBe(true)
|
||||
})
|
||||
|
||||
it('gives a failed upload\'s reservation back, once', () => {
|
||||
const budget = createWindowBudget({ limit: 100, windowMs: 3_600_000 })
|
||||
const first = budget('share', 80)
|
||||
expect(budget('share', 80).ok).toBe(false)
|
||||
if (first.ok) {
|
||||
first.release()
|
||||
first.release()
|
||||
}
|
||||
expect(budget('share', 80).ok).toBe(true)
|
||||
expect(budget('share', 80).ok).toBe(false)
|
||||
})
|
||||
|
||||
it('ignores a release after its window has closed', () => {
|
||||
vi.useFakeTimers()
|
||||
const budget = createWindowBudget({ limit: 100, windowMs: 1000 })
|
||||
const old = budget('share', 100)
|
||||
vi.advanceTimersByTime(1000)
|
||||
expect(budget('share', 100).ok).toBe(true)
|
||||
if (old.ok) old.release()
|
||||
expect(budget('share', 1).ok).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { AlbumType } from '@ipp/core'
|
||||
import { buildNotification } from '../src/notify'
|
||||
import { share } from './helpers'
|
||||
|
||||
const file = { name: 'ipp_upload_IMG_0042.jpg', size: 3145728, type: 'image/jpeg' }
|
||||
const asset = { id: 'bbbbbbbb-0000-0000-0000-000000000003', status: 'created' as const }
|
||||
|
||||
describe('buildNotification', () => {
|
||||
it('names an album share by its album id', () => {
|
||||
expect(buildNotification(share(), file, asset).data.share).toEqual({
|
||||
type: 'album',
|
||||
id: 'aaaaaaaa-0000-0000-0000-000000000002',
|
||||
title: 'Holiday 2025'
|
||||
})
|
||||
})
|
||||
|
||||
it('names an individual share by the link id', () => {
|
||||
const payload = buildNotification(share({ type: AlbumType.individual, album: undefined, description: 'Some photos' }), file, asset)
|
||||
expect(payload.data.share).toEqual({
|
||||
type: 'individual',
|
||||
id: 'ffffffff-0000-0000-0000-000000000001',
|
||||
title: 'Some photos'
|
||||
})
|
||||
})
|
||||
|
||||
it('carries a flat title and message for simple receivers', () => {
|
||||
const payload = buildNotification(share(), file, asset)
|
||||
expect(payload.title).toBe('Photo sent to Holiday 2025')
|
||||
expect(payload.message).toBe('ipp_upload_IMG_0042.jpg (3.0 MB) was uploaded to Holiday 2025.')
|
||||
})
|
||||
|
||||
it('never carries the share key, slug or password', () => {
|
||||
const payload = JSON.stringify(buildNotification(share({ key: 'secret-key', slug: 'secret-slug', password: 'hunter2' }), file, asset))
|
||||
expect(payload).not.toContain('secret-key')
|
||||
expect(payload).not.toContain('secret-slug')
|
||||
expect(payload).not.toContain('hunter2')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,75 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { fileType, formatSize, isUploadableType } from '../src/shared/upload'
|
||||
import { declaredLength, uploadTimestamp } from '../src/receive'
|
||||
|
||||
describe('isUploadableType', () => {
|
||||
it('accepts photos and videos, with or without parameters', () => {
|
||||
expect(isUploadableType('image/jpeg')).toBe(true)
|
||||
expect(isUploadableType('video/quicktime')).toBe(true)
|
||||
expect(isUploadableType('IMAGE/HEIC')).toBe(true)
|
||||
expect(isUploadableType('image/jpeg; charset=binary')).toBe(true)
|
||||
})
|
||||
|
||||
it('refuses SVG, the one image type a browser may execute', () => {
|
||||
expect(isUploadableType('image/svg+xml')).toBe(false)
|
||||
expect(isUploadableType('IMAGE/SVG+XML; charset=utf-8')).toBe(false)
|
||||
})
|
||||
|
||||
it('refuses anything else', () => {
|
||||
expect(isUploadableType('application/octet-stream')).toBe(false)
|
||||
expect(isUploadableType('text/html')).toBe(false)
|
||||
expect(isUploadableType('')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('fileType', () => {
|
||||
it('keeps the type the browser gave', () => {
|
||||
expect(fileType('IMG_0042.HEIC', 'image/heic')).toBe('image/heic')
|
||||
})
|
||||
|
||||
it('derives a type from the extension when the browser gives none', () => {
|
||||
expect(fileType('IMG_0042.HEIC', '')).toBe('image/heic')
|
||||
expect(fileType('DSC_0001.NEF', '')).toBe('image/x-nikon-nef')
|
||||
expect(isUploadableType(fileType('clip.mts', ''))).toBe(true)
|
||||
})
|
||||
|
||||
it('gives up on an unknown extension, so the client refuses the file', () => {
|
||||
expect(fileType('notes.txt', '')).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatSize', () => {
|
||||
it('picks a readable unit', () => {
|
||||
expect(formatSize(512)).toBe('512 bytes')
|
||||
expect(formatSize(3 * 1024)).toBe('3 KB')
|
||||
expect(formatSize(3145728)).toBe('3.0 MB')
|
||||
expect(formatSize(2.5 * 1024 * 1024 * 1024)).toBe('2.5 GB')
|
||||
})
|
||||
})
|
||||
|
||||
describe('declaredLength', () => {
|
||||
it('reads a plain decimal Content-Length', () => {
|
||||
expect(declaredLength('3145728')).toBe(3145728)
|
||||
expect(declaredLength('0')).toBe(0)
|
||||
})
|
||||
|
||||
it('rejects a missing or malformed header', () => {
|
||||
expect(declaredLength(undefined)).toBeUndefined()
|
||||
expect(declaredLength('')).toBeUndefined()
|
||||
expect(declaredLength('12abc')).toBeUndefined()
|
||||
expect(declaredLength('-1')).toBeUndefined()
|
||||
expect(declaredLength('1e9')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('uploadTimestamp', () => {
|
||||
it('passes through a valid timestamp as ISO 8601', () => {
|
||||
expect(uploadTimestamp('2026-09-14T07:18:00.000Z')).toBe('2026-09-14T07:18:00.000Z')
|
||||
})
|
||||
|
||||
it('falls back to now for junk or missing input', () => {
|
||||
for (const input of ['not a date', '', undefined, 42]) {
|
||||
expect(Number.isNaN(Date.parse(uploadTimestamp(input)))).toBe(false)
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user