Skip to content

A full-featured download manager.

10.4 KB
Raw Blame
import filesystem, {
  chmod,
  mkdir,
  mkdtemp,
  realpath,
  rm,
  stat,
  symlink,
  utimes,
} from 'node:fs/promises'
import { syncBuiltinESMExports } from 'node:module'
import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
  createServerDownloadPathPolicy,
  resolveServerDefaultSaveDir,
} from './download-path-policy'

const roots: string[] = []

async function tempRoot(): Promise<string> {
  const root = await mkdtemp(path.join(os.tmpdir(), 'motrix-save-policy-'))
  roots.push(root)
  return root
}

afterEach(async () => {
  vi.restoreAllMocks()
  syncBuiltinESMExports()
  await Promise.all(
    roots.splice(0).map(async (root) => {
      await chmod(root, 0o700).catch(() => undefined)
      await rm(root, { recursive: true, force: true })
    })
  )
})

describe('resolveServerDefaultSaveDir', () => {
  it('uses and normalizes MOTRIX_DEFAULT_SAVE_DIR', () => {
    expect(
      resolveServerDefaultSaveDir(
        { MOTRIX_DEFAULT_SAVE_DIR: '/downloads/../downloads' },
        '/fallback'
      )
    ).toBe('/downloads')
  })

  it('rejects a relative MOTRIX_DEFAULT_SAVE_DIR', () => {
    expect(() =>
      resolveServerDefaultSaveDir(
        { MOTRIX_DEFAULT_SAVE_DIR: 'downloads' },
        '/fallback'
      )
    ).toThrow('MOTRIX_DEFAULT_SAVE_DIR must be an absolute path')
  })
})

describe('ServerDownloadPathPolicy', () => {
  it.each([false, true])(
    'prepares an existing volume root whose mkdir reports EPERM (allowlist: %s)',
    async (allowlisted) => {
      const root = await realpath(await tempRoot())
      const realMkdir = filesystem.mkdir
      const mkdirSpy = vi
        .spyOn(filesystem, 'mkdir')
        .mockImplementation(async (...args) => {
          if (args[0] === root) {
            throw Object.assign(new Error('cannot mkdir volume root'), {
              code: 'EPERM',
            })
          }
          return realMkdir(...args)
        })
      syncBuiltinESMExports()
      vi.resetModules()
      const { createServerDownloadPathPolicy: createPolicy } = await import(
        './download-path-policy'
      )

      const policy = await createPolicy({
        defaultSaveDir: root,
        allowedSaveDirsValue: allowlisted ? root : undefined,
      })
      expect(await policy.prepareSaveDir(root)).toBe(root)
      expect(await policy.authorizeDirectory(root)).toMatchObject({
        canonicalPath: root,
      })
      const nested = path.join(root, 'nested', 'downloads')
      expect(await policy.prepareSaveDir(nested)).toBe(nested)
      expect((await stat(nested)).isDirectory()).toBe(true)
      expect(mkdirSpy).toHaveBeenCalledWith(nested, { recursive: true })
      expect(mkdirSpy).not.toHaveBeenCalledWith(root, expect.anything())
    }
  )

  it('still rejects an existing directory when the write probe is denied', async () => {
    const root = await realpath(await tempRoot())
    const error = Object.assign(new Error('write access denied'), {
      code: 'EPERM',
    })
    const openSpy = vi.spyOn(filesystem, 'open').mockRejectedValue(error)
    syncBuiltinESMExports()
    vi.resetModules()
    const { createServerDownloadPathPolicy: createPolicy } = await import(
      './download-path-policy'
    )

    await expect(createPolicy({ defaultSaveDir: root })).rejects.toMatchObject({
      message: `Save directory is not writable: ${root}`,
      cause: error,
    })
    expect(openSpy).toHaveBeenCalledOnce()
  })

  it('reuses the single authorization stat for the authorized symlink target modification time', async () => {
    const root = await tempRoot()
    const observed = vi.spyOn(filesystem, 'stat')
    syncBuiltinESMExports()
    vi.resetModules()
    const { createServerDownloadPathPolicy: createPolicy } = await import(
      './download-path-policy'
    )
    const policy = await createPolicy({
      defaultSaveDir: root,
      allowedSaveDirsValue: root,
    })
    const target = path.join(root, 'target')
    const alias = path.join(root, 'alias')
    await mkdir(target)
    await symlink(target, alias)
    await utimes(target, new Date(1700000000123), new Date(1700000000123))
    const expected = await stat(target)
    observed.mockClear()
    expect(await policy.authorizeDirectory(alias)).toEqual({
      path: alias,
      canonicalPath: await realpath(target),
      rootPath: root,
      modifiedAt: expected.mtimeMs,
    })
    expect(observed).toHaveBeenCalledExactlyOnceWith(await realpath(target))
  })

  it.each([Number.NaN, Infinity, -Infinity])(
    'omits nonfinite metadata without weakening the directory check: %s',
    async (mtimeMs) => {
      const root = await tempRoot()
      const observed = vi.spyOn(filesystem, 'stat')
      syncBuiltinESMExports()
      vi.resetModules()
      const { createServerDownloadPathPolicy: createPolicy } = await import(
        './download-path-policy'
      )
      const policy = await createPolicy({
        defaultSaveDir: root,
      })
      const info = await stat(root)
      Object.defineProperty(info, 'mtimeMs', { value: mtimeMs })
      observed.mockClear().mockResolvedValueOnce(info)
      const authorized = await policy.authorizeDirectory(root)
      expect(authorized).not.toHaveProperty('modifiedAt')
      expect(observed).toHaveBeenCalledExactlyOnceWith(await realpath(root))
    }
  )

  it('authorizes canonical aliases only within their configured resolved subtree, including restart defaults', async () => {
    const root = await tempRoot()
    const canonicalRoot = path.join(root, 'real')
    const alias = path.join(root, 'alias')
    await mkdir(canonicalRoot)
    await symlink(canonicalRoot, alias)
    const policy = await createServerDownloadPathPolicy({
      defaultSaveDir: alias,
      allowedSaveDirsValue: alias,
    })
    const prepared = await policy.prepareSaveDir(path.join(alias, 'child'))
    expect(await policy.authorizeDirectory(prepared)).toEqual({
      path: path.join(alias, 'child'),
      canonicalPath: prepared,
      rootPath: alias,
      modifiedAt: expect.any(Number),
    })
    const restarted = await createServerDownloadPathPolicy({
      defaultSaveDir: prepared,
      allowedSaveDirsValue: alias,
    })
    expect(await restarted.prepareSaveDir(undefined)).toBe(prepared)
    await expect(restarted.authorizeDirectory(root)).rejects.toMatchObject({
      directoryCode: 'outsideRoots',
    })
    const outside = path.join(root, 'outside')
    await mkdir(outside)
    await symlink(outside, path.join(canonicalRoot, 'escape'))
    await expect(
      restarted.authorizeDirectory(path.join(canonicalRoot, 'escape'))
    ).rejects.toMatchObject({ directoryCode: 'outsideRoots' })
  })

  it('does not treat whitespace-only literal requests as the default sentinel', async () => {
    const root = await tempRoot()
    const policy = await createServerDownloadPathPolicy({
      defaultSaveDir: root,
    })
    await expect(policy.prepareSaveDir(' ')).rejects.toThrow(
      'must be an absolute path'
    )
    expect(await policy.prepareSaveDir('')).toBe(await realpath(root))
    const missing = path.join(root, 'absent')
    await expect(policy.authorizeDirectory(missing)).rejects.toMatchObject({
      code: 'ENOENT',
    })
    await expect(realpath(missing)).rejects.toMatchObject({ code: 'ENOENT' })
  })
  it('creates and prepares the default and allowed roots', async () => {
    const root = await tempRoot()
    const downloads = path.join(root, 'downloads')
    const media = path.join(root, 'media')
    const policy = await createServerDownloadPathPolicy({
      defaultSaveDir: downloads,
      allowedSaveDirsValue: [downloads, media].join(path.delimiter),
    })

    expect(policy.allowedSaveDirs).toEqual([downloads, media])
    await expect(policy.prepareSaveDir('')).resolves.toBe(
      await realpath(downloads)
    )
    const nested = await policy.prepareSaveDir(path.join(media, 'nested'))
    expect(nested).toBe(await realpath(path.join(media, 'nested')))
  })

  it('rejects an allowed root that does not contain the default', async () => {
    const root = await tempRoot()
    await expect(
      createServerDownloadPathPolicy({
        defaultSaveDir: path.join(root, 'downloads'),
        allowedSaveDirsValue: path.join(root, 'media'),
      })
    ).rejects.toThrow(
      'MOTRIX_DEFAULT_SAVE_DIR must be inside MOTRIX_ALLOWED_SAVE_DIRS'
    )
  })

  it('rejects relative and out-of-root task paths before creating them', async () => {
    const root = await tempRoot()
    const downloads = path.join(root, 'downloads')
    const escaped = path.join(root, 'escaped')
    const policy = await createServerDownloadPathPolicy({
      defaultSaveDir: downloads,
      allowedSaveDirsValue: downloads,
    })

    await expect(policy.prepareSaveDir('relative')).rejects.toThrow(
      'Save directory must be an absolute path'
    )
    await expect(policy.prepareSaveDir(escaped)).rejects.toThrow(
      'Save directory is outside MOTRIX_ALLOWED_SAVE_DIRS'
    )
    await expect(realpath(escaped)).rejects.toMatchObject({ code: 'ENOENT' })
  })

  it('rejects a symlink that escapes an allowed root', async () => {
    const root = await tempRoot()
    const downloads = path.join(root, 'downloads')
    const outside = path.join(root, 'outside')
    await Promise.all([
      mkdir(downloads, { recursive: true }),
      mkdir(outside, { recursive: true }),
    ])
    await symlink(outside, path.join(downloads, 'escape'))
    const policy = await createServerDownloadPathPolicy({
      defaultSaveDir: downloads,
      allowedSaveDirsValue: downloads,
    })

    await expect(
      policy.prepareSaveDir(path.join(downloads, 'escape', 'nested'))
    ).rejects.toThrow('Save directory resolves outside the allowed root')
  })

  it('permits any absolute writable path when no allowlist is configured', async () => {
    const root = await tempRoot()
    const downloads = path.join(root, 'downloads')
    const custom = path.join(root, 'custom')
    const policy = await createServerDownloadPathPolicy({
      defaultSaveDir: downloads,
    })

    expect(policy.allowedSaveDirs).toEqual([])
    const prepared = await policy.prepareSaveDir(custom)
    expect(prepared).toBe(await realpath(custom))
  })

  it('surfaces an unwritable save directory clearly', async () => {
    const root = await tempRoot()
    const downloads = path.join(root, 'downloads')
    await mkdir(downloads, { recursive: true })
    await chmod(downloads, 0o500)

    await expect(
      createServerDownloadPathPolicy({ defaultSaveDir: downloads })
    ).rejects.toThrow('Save directory is not writable')
    await chmod(downloads, 0o700)
  })
})