Files
arti-pack/tests/spec_spec.lua
T
unkin-agent 3c73a83602 Reject a NUL byte in every ref that reaches curl
A NUL truncates a string when vim.system marshals argv, so the URL curl
fetches diverges from the ref that was validated, locked and displayed.
The whitespace class does not cover it.

- reject a NUL in tag, branch, the "as" directory name, an explicit url
  and the registry
- cover a NUL in the middle, at the end, and alone for each
2026-09-12 00:23:08 +10:00

336 lines
12 KiB
Lua

local t = require('tests.harness')
local spec = require('arti-pack.spec')
local REGISTRY = 'https://artifacts.example.com/artifactory/github-remote'
local NUL = string.char(0)
-- A NUL truncates the value in argv, so anything that reaches curl must be
-- rejected before it can diverge from what was validated and locked.
local NULS = { 'mid' .. NUL .. 'dle', 'trailing' .. NUL, NUL }
t.describe('spec.parse', function()
t.it('parses a full table spec', function()
local s = assert(spec.parse({ 'nvim-lua/plenary.nvim', tag = 'v0.1.4', opt = true }))
t.eq('nvim-lua', s.owner)
t.eq('plenary.nvim', s.repo)
t.eq('plenary.nvim', s.name)
t.eq('v0.1.4', s.tag)
t.eq('zip', s.ext)
t.eq(true, s.opt)
end)
t.it('defaults opt to false', function()
local s = assert(spec.parse({ 'tpope/vim-fugitive', tag = 'v3.7' }))
t.eq(false, s.opt)
end)
t.it('honours an "as" override for the install directory', function()
local s = assert(spec.parse({ 'folke/tokyonight.nvim', tag = 'v4.10.0', as = 'tokyonight' }))
t.eq('tokyonight', s.name)
end)
t.it('accepts a source key instead of the positional entry', function()
local s = assert(spec.parse({ source = 'owner/repo', tag = 'v1' }))
t.eq('owner/repo', s.source)
end)
t.it('rejects a bare string with no tag', function()
local s, err = spec.parse('owner/repo')
t.falsy(s)
t.matches('missing a tag', err)
end)
t.it('parses a branch spec', function()
local s = assert(spec.parse({ 'hrsh7th/cmp-path', branch = 'main' }))
t.eq('main', s.branch)
t.eq('main', s.ref)
t.eq(nil, s.tag, 'a branch spec carries no tag')
t.eq('cmp-path', s.name)
end)
t.it('rejects a spec that sets both a tag and a branch', function()
local s, err = spec.parse({ 'owner/repo', tag = 'v1', branch = 'main' })
t.falsy(s)
t.matches('sets both a tag and a branch', err)
end)
t.it('rejects a spec with neither a tag nor a branch', function()
local s, err = spec.parse({ 'owner/repo' })
t.falsy(s)
t.matches('missing a tag or a branch', err)
end)
t.it('rejects a branch that would reshape the archive path', function()
for _, bad in ipairs({ '..', '.', 'feature/foo', '../../etc', 'a\\b', 'ma in' }) do
local s, err = spec.parse({ 'owner/repo', branch = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('invalid branch', err)
end
end)
t.it('rejects an empty branch', function()
for _, bad in ipairs({ '', ' ', 42 }) do
local s, err = spec.parse({ 'owner/repo', branch = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('missing a tag or a branch', err)
end
end)
t.it('rejects a tag that would reshape the archive path', function()
for _, bad in ipairs({ '..', '.', 'a\\b' }) do
local s, err = spec.parse({ 'owner/repo', tag = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('invalid tag', err)
end
end)
t.it('trims surrounding whitespace from a branch', function()
local s = assert(spec.parse({ 'owner/repo', branch = ' main \n' }))
t.eq('main', s.branch)
end)
t.it('rejects a source without an owner', function()
local s, err = spec.parse({ 'plenary.nvim', tag = 'v1' })
t.falsy(s)
t.matches('expected "owner/repo"', err)
end)
t.it('rejects a source with extra path segments', function()
local s, err = spec.parse({ 'a/b/c', tag = 'v1' })
t.falsy(s)
t.matches('expected "owner/repo"', err)
end)
t.it('rejects a tag containing a path separator', function()
local s, err = spec.parse({ 'owner/repo', tag = '../../etc' })
t.falsy(s)
t.matches('invalid tag', err)
end)
t.it('rejects an unsupported extension', function()
local s, err = spec.parse({ 'owner/repo', tag = 'v1', ext = 'rar' })
t.falsy(s)
t.matches('unsupported ext', err)
end)
t.it('rejects a traversing "as" name', function()
local s, err = spec.parse({ 'owner/repo', tag = 'v1', as = '..' })
t.falsy(s)
t.matches('unusable directory name', err)
end)
t.it('rejects a non-table, non-string spec', function()
local s, err = spec.parse(42)
t.falsy(s)
t.matches('must be a string or a table', err)
end)
t.it('trims surrounding whitespace from the source and tag', function()
local s = assert(spec.parse({ ' owner/repo ', tag = ' v1.0.0 ' }))
t.eq('owner/repo', s.source)
t.eq('v1.0.0', s.tag)
end)
t.it('trims surrounding whitespace from an explicit url', function()
local s = assert(spec.parse({ 'owner/repo', tag = 'v1', url = ' https://example.test/a.zip\n' }))
t.eq('https://example.test/a.zip', s.url)
end)
t.it('rejects a tag carrying a NUL byte', function()
for _, bad in ipairs(NULS) do
local s, err = spec.parse({ 'owner/repo', tag = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('NUL byte in its tag', err)
end
end)
t.it('rejects a branch carrying a NUL byte', function()
for _, bad in ipairs(NULS) do
local s, err = spec.parse({ 'owner/repo', branch = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('NUL byte in its branch', err)
end
end)
t.it('rejects an "as" name carrying a NUL byte', function()
for _, bad in ipairs(NULS) do
local s, err = spec.parse({ 'owner/repo', tag = 'v1', as = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('NUL byte in its directory name', err)
end
end)
t.it('rejects a url carrying a NUL byte', function()
local urls = { 'https://example.test/a' .. NUL .. 'b.zip', 'https://example.test/a.zip' .. NUL, NUL }
for _, bad in ipairs(urls) do
local s, err = spec.parse({ 'owner/repo', tag = 'v1', url = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('NUL byte in its url', err)
end
end)
t.it('rejects a source carrying a NUL byte', function()
for _, bad in ipairs(NULS) do
local s, err = spec.parse({ 'owner/' .. bad, tag = 'v1' })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('expected "owner/repo"', err)
end
end)
t.it('rejects a url with whitespace inside it', function()
for _, bad in ipairs({ 'https://example.test/a b.zip', 'https://example\n.test/a.zip', ' ' }) do
local s, err = spec.parse({ 'owner/repo', tag = 'v1', url = bad })
t.falsy(s, 'rejects ' .. vim.inspect(bad))
t.matches('invalid url', err)
end
end)
end)
t.describe('spec.url', function()
t.it('builds a tag archive url', function()
local s = assert(spec.parse({ 'nvim-lua/plenary.nvim', tag = 'v0.1.4' }))
t.eq(REGISTRY .. '/nvim-lua/plenary.nvim/archive/refs/tags/v0.1.4.zip', spec.url(s, REGISTRY))
end)
t.it('honours the tar.gz extension', function()
local s = assert(spec.parse({ 'nvim-lua/plenary.nvim', tag = 'v0.1.4', ext = 'tar.gz' }))
t.matches('/v0%.1%.4%.tar%.gz$', spec.url(s, REGISTRY))
end)
t.it('does not double up on a trailing registry slash', function()
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
t.eq(REGISTRY .. '/owner/repo/archive/refs/tags/v1.zip', spec.url(s, REGISTRY .. '///'))
end)
t.it('never builds a url containing whitespace', function()
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
local want = REGISTRY .. '/owner/repo/archive/refs/tags/v1.zip'
t.eq(want, spec.url(s, REGISTRY .. ' '))
t.eq(want, spec.url(s, REGISTRY .. '\n'))
t.eq(want, spec.url(s, ' ' .. REGISTRY))
for _, bad in ipairs({ 'https://arti facts.example.com/gh', 'https://artifacts\n.example.com/gh', ' ' }) do
local url, err = spec.url(s, bad)
t.falsy(url, 'rejects ' .. vim.inspect(bad))
t.matches('is not an http%(s%) URL', err)
end
end)
t.it('builds a branch archive url', function()
local s = assert(spec.parse({ 'hrsh7th/cmp-path', branch = 'main' }))
t.eq(REGISTRY .. '/hrsh7th/cmp-path/archive/refs/heads/main.zip', spec.url(s, REGISTRY))
end)
t.it('builds a branch archive url for tar.gz', function()
local s = assert(spec.parse({ 'hrsh7th/vim-vsnip', branch = 'master', ext = 'tar.gz' }))
t.eq(REGISTRY .. '/hrsh7th/vim-vsnip/archive/refs/heads/master.tar.gz', spec.url(s, REGISTRY))
end)
t.it('does not double up on a trailing registry slash for a branch', function()
local s = assert(spec.parse({ 'owner/repo', branch = 'main' }))
t.eq(REGISTRY .. '/owner/repo/archive/refs/heads/main.zip', spec.url(s, REGISTRY .. '///'))
end)
t.it('lets an explicit url bypass the registry for a branch', function()
local s = assert(spec.parse({ 'owner/repo', branch = 'main', url = 'https://example.test/main.zip' }))
t.eq('https://example.test/main.zip', spec.url(s, REGISTRY))
end)
t.it('passes an explicit url straight through', function()
local s = assert(spec.parse({ 'owner/repo', tag = 'v1', url = 'https://example.test/a.zip' }))
t.eq('https://example.test/a.zip', spec.url(s, REGISTRY))
end)
t.it('errors when no registry is configured', function()
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
local url, err = spec.url(s, nil)
t.falsy(url)
t.matches('registry is not configured', err)
t.matches("setup{ registry = 'https://", err, 'the error says how to set it')
end)
t.it('errors instead of handing a bad registry to curl', function()
local s = assert(spec.parse({ 'owner/repo', tag = 'v1' }))
for _, bad in ipairs({ '', 'https://', 'artifacts.example.com/gh', 'ftp://artifacts.example.com', 42 }) do
local url, err = spec.url(s, bad)
t.falsy(url, 'rejects ' .. vim.inspect(bad))
t.matches('is not an http%(s%) URL', err)
end
end)
end)
t.describe('spec.check_registry', function()
t.it('returns the registry with trailing slashes stripped', function()
t.eq(REGISTRY, spec.check_registry(REGISTRY))
t.eq(REGISTRY, spec.check_registry(REGISTRY .. '/'))
t.eq(REGISTRY, spec.check_registry(REGISTRY .. '///'))
end)
t.it('accepts a plain http registry', function()
t.eq('http://artifacts.example.com/gh', spec.check_registry('http://artifacts.example.com/gh'))
end)
t.it('trims leading whitespace', function()
t.eq(REGISTRY, spec.check_registry(' ' .. REGISTRY))
end)
t.it('trims trailing whitespace, including a newline', function()
t.eq(REGISTRY, spec.check_registry(REGISTRY .. ' '))
t.eq(REGISTRY, spec.check_registry(REGISTRY .. '\n'))
t.eq(REGISTRY, spec.check_registry(' \t' .. REGISTRY .. '/ \n'))
end)
t.it('rejects whitespace embedded in the registry', function()
for _, bad in ipairs({ 'https://artifacts.example.com/g h', 'https://artifacts\n.example.com/gh' }) do
local base, err = spec.check_registry(bad)
t.falsy(base, 'rejects ' .. vim.inspect(bad))
t.matches('is not an http%(s%) URL', err)
end
end)
t.it('rejects a registry carrying a NUL byte', function()
local bads = { 'https://artifacts.example.com/g' .. NUL .. 'h', REGISTRY .. NUL, NUL }
for _, bad in ipairs(bads) do
local base, err = spec.check_registry(bad)
t.falsy(base, 'rejects ' .. vim.inspect(bad))
t.matches('NUL byte', err)
end
end)
t.it('rejects a whitespace-only registry', function()
for _, bad in ipairs({ ' ', ' ', '\n', ' \t\n ' }) do
local base, err = spec.check_registry(bad)
t.falsy(base, 'rejects ' .. vim.inspect(bad))
t.matches('is not an http%(s%) URL', err)
end
end)
end)
t.describe('spec.parse_all', function()
t.it('collects specs and reports per-entry errors', function()
local specs, errors = spec.parse_all({
{ 'a/one', tag = 'v1' },
{ 'bad-source', tag = 'v1' },
{ 'b/two', tag = 'v2', opt = true },
})
t.eq(2, #specs)
t.eq(1, #errors)
t.matches('plugins%[2%]', errors[1])
end)
t.it('rejects two specs that install to the same directory', function()
local specs, errors = spec.parse_all({
{ 'a/shared', tag = 'v1' },
{ 'b/shared', tag = 'v2' },
})
t.eq(1, #specs)
t.matches('duplicate plugin name', errors[1])
end)
t.it('returns empty results for a nil plugin list', function()
local specs, errors = spec.parse_all(nil)
t.eq({}, specs)
t.eq({}, errors)
end)
end)