@contember/create 2.2.0-alpha.17 → 2.2.0-alpha.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/src/commands/WorkspaceCreateCommand.cjs +13 -14
- package/dist/development/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
- package/dist/development/src/commands/WorkspaceCreateCommand.js +14 -15
- package/dist/development/src/commands/WorkspaceCreateCommand.js.map +1 -1
- package/dist/development/src/lib/FileSystem.cjs +10 -4
- package/dist/development/src/lib/FileSystem.cjs.map +1 -1
- package/dist/development/src/lib/FileSystem.js +8 -2
- package/dist/development/src/lib/FileSystem.js.map +1 -1
- package/dist/development/src/lib/TemplateInstaller.cjs +13 -14
- package/dist/development/src/lib/TemplateInstaller.cjs.map +1 -1
- package/dist/development/src/lib/TemplateInstaller.js +7 -8
- package/dist/development/src/lib/TemplateInstaller.js.map +1 -1
- package/dist/development/src/lib/fsErrors.cjs +7 -0
- package/dist/development/src/lib/fsErrors.cjs.map +1 -0
- package/dist/development/src/lib/fsErrors.js +7 -0
- package/dist/development/src/lib/fsErrors.js.map +1 -0
- package/dist/development/src/lib/projectPath.cjs +66 -0
- package/dist/development/src/lib/projectPath.cjs.map +1 -0
- package/dist/development/src/lib/projectPath.js +66 -0
- package/dist/development/src/lib/projectPath.js.map +1 -0
- package/dist/development/src/lib/remoteTemplate.cjs +108 -0
- package/dist/development/src/lib/remoteTemplate.cjs.map +1 -0
- package/dist/development/src/lib/remoteTemplate.js +108 -0
- package/dist/development/src/lib/remoteTemplate.js.map +1 -0
- package/dist/development/src/lib/tar.cjs +243 -0
- package/dist/development/src/lib/tar.cjs.map +1 -0
- package/dist/development/src/lib/tar.js +243 -0
- package/dist/development/src/lib/tar.js.map +1 -0
- package/dist/production/src/commands/WorkspaceCreateCommand.cjs +13 -14
- package/dist/production/src/commands/WorkspaceCreateCommand.cjs.map +1 -1
- package/dist/production/src/commands/WorkspaceCreateCommand.js +14 -15
- package/dist/production/src/commands/WorkspaceCreateCommand.js.map +1 -1
- package/dist/production/src/lib/FileSystem.cjs +10 -4
- package/dist/production/src/lib/FileSystem.cjs.map +1 -1
- package/dist/production/src/lib/FileSystem.js +8 -2
- package/dist/production/src/lib/FileSystem.js.map +1 -1
- package/dist/production/src/lib/TemplateInstaller.cjs +13 -14
- package/dist/production/src/lib/TemplateInstaller.cjs.map +1 -1
- package/dist/production/src/lib/TemplateInstaller.js +7 -8
- package/dist/production/src/lib/TemplateInstaller.js.map +1 -1
- package/dist/production/src/lib/fsErrors.cjs +7 -0
- package/dist/production/src/lib/fsErrors.cjs.map +1 -0
- package/dist/production/src/lib/fsErrors.js +7 -0
- package/dist/production/src/lib/fsErrors.js.map +1 -0
- package/dist/production/src/lib/projectPath.cjs +66 -0
- package/dist/production/src/lib/projectPath.cjs.map +1 -0
- package/dist/production/src/lib/projectPath.js +66 -0
- package/dist/production/src/lib/projectPath.js.map +1 -0
- package/dist/production/src/lib/remoteTemplate.cjs +108 -0
- package/dist/production/src/lib/remoteTemplate.cjs.map +1 -0
- package/dist/production/src/lib/remoteTemplate.js +108 -0
- package/dist/production/src/lib/remoteTemplate.js.map +1 -0
- package/dist/production/src/lib/tar.cjs +243 -0
- package/dist/production/src/lib/tar.cjs.map +1 -0
- package/dist/production/src/lib/tar.js +243 -0
- package/dist/production/src/lib/tar.js.map +1 -0
- package/dist/types/commands/WorkspaceCreateCommand.d.ts.map +1 -1
- package/dist/types/lib/FileSystem.d.ts +2 -0
- package/dist/types/lib/FileSystem.d.ts.map +1 -1
- package/dist/types/lib/TemplateInstaller.d.ts.map +1 -1
- package/dist/types/lib/fsErrors.d.ts +3 -0
- package/dist/types/lib/fsErrors.d.ts.map +1 -0
- package/dist/types/lib/projectPath.d.ts +12 -0
- package/dist/types/lib/projectPath.d.ts.map +1 -0
- package/dist/types/lib/remoteTemplate.d.ts +15 -0
- package/dist/types/lib/remoteTemplate.d.ts.map +1 -0
- package/dist/types/lib/tar.d.ts +28 -0
- package/dist/types/lib/tar.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -7
- package/resources/templates/default/admin/lib/editor/sortable-block.tsx +5 -4
- package/resources/templates/default/admin/lib/tenant/dict.ts +3 -0
- package/resources/templates/default/admin/lib/ui/label.tsx +25 -2
- package/resources/templates/default/admin/lib/ui/menu.tsx +1 -1
- package/resources/templates/default/admin/lib/ui/progress.tsx +75 -11
- package/resources/templates/default/admin/lib/ui/separator.tsx +27 -3
- package/resources/templates/default/admin/lib/ui/sidebar.tsx +1 -1
- package/resources/templates/default/admin/lib/ui/toggle.tsx +41 -2
- package/resources/templates/default/admin/lib/utils/classNames.ts +107 -0
- package/resources/templates/default/admin/lib/utils/cn.ts +3 -3
- package/resources/templates/default/admin/lib/utils/index.ts +1 -0
- package/resources/templates/default/admin/lib/utils/uic.tsx +2 -3
- package/resources/templates/default/admin/package.json +0 -7
- package/src/commands/WorkspaceCreateCommand.ts +14 -15
- package/src/lib/FileSystem.ts +7 -2
- package/src/lib/TemplateInstaller.ts +8 -8
- package/src/lib/fsErrors.ts +2 -0
- package/src/lib/projectPath.ts +79 -0
- package/src/lib/remoteTemplate.ts +130 -0
- package/src/lib/tar.ts +283 -0
- package/tests/cases/unit/fileSystem.test.ts +14 -0
- package/tests/cases/unit/fixtures/template.tar.gz +0 -0
- package/tests/cases/unit/remoteTemplate.test.ts +277 -0
- package/tests/cases/unit/template-installer-containment.test.ts +133 -0
- package/tests/cases/unit/template-installer-symlinks.test.ts +37 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { readFileSync } from 'node:fs'
|
|
3
|
+
import { lstat, mkdir, mkdtemp, readdir, readFile, readlink, rm, symlink } from 'node:fs/promises'
|
|
4
|
+
import { tmpdir } from 'node:os'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { downloadTemplate, type Fetch, parseTemplateSource, templateArchiveUrl } from '../../../src/lib/remoteTemplate.js'
|
|
7
|
+
import { extractTar, parseTar, type TarEntry } from '../../../src/lib/tar.js'
|
|
8
|
+
|
|
9
|
+
const fixture = readFileSync(new URL('./fixtures/template.tar.gz', import.meta.url))
|
|
10
|
+
|
|
11
|
+
const tempDirs: string[] = []
|
|
12
|
+
const createTempDir = async (): Promise<string> => {
|
|
13
|
+
const dir = await mkdtemp(join(tmpdir(), 'contember-remote-template-test-'))
|
|
14
|
+
tempDirs.push(dir)
|
|
15
|
+
return dir
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
afterEach(async () => {
|
|
19
|
+
await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })))
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const listTree = async (root: string, dir = root): Promise<string[]> => {
|
|
23
|
+
const result: string[] = []
|
|
24
|
+
for (const name of (await readdir(dir)).sort()) {
|
|
25
|
+
const path = join(dir, name)
|
|
26
|
+
const stat = await lstat(path)
|
|
27
|
+
const relative = path.slice(root.length + 1)
|
|
28
|
+
if (stat.isSymbolicLink()) {
|
|
29
|
+
result.push(`${relative} -> ${await readlink(path)}`)
|
|
30
|
+
} else if (stat.isDirectory()) {
|
|
31
|
+
result.push(`${relative}/`, ...(await listTree(root, path)))
|
|
32
|
+
} else {
|
|
33
|
+
result.push(relative)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Builds a ustar archive; enough to express the hostile entries a real tar tool would refuse to create. */
|
|
40
|
+
const tar = (entries: readonly { path: string; type?: string; data?: string; link?: string }[]): Uint8Array => {
|
|
41
|
+
const blocks: Uint8Array[] = []
|
|
42
|
+
for (const { path, type = '0', data = '', link = '' } of entries) {
|
|
43
|
+
const header = new Uint8Array(512)
|
|
44
|
+
const write = (offset: number, value: string) => header.set(new TextEncoder().encode(value), offset)
|
|
45
|
+
const body = new TextEncoder().encode(data)
|
|
46
|
+
write(0, path)
|
|
47
|
+
write(100, '0000644\0')
|
|
48
|
+
write(124, body.length.toString(8).padStart(11, '0') + '\0')
|
|
49
|
+
write(156, type)
|
|
50
|
+
write(157, link)
|
|
51
|
+
write(257, 'ustar\u000000')
|
|
52
|
+
header.fill(0x20, 148, 156)
|
|
53
|
+
const checksum = header.reduce((sum, byte) => sum + byte, 0)
|
|
54
|
+
write(148, checksum.toString(8).padStart(6, '0') + '\0 ')
|
|
55
|
+
blocks.push(header, body, new Uint8Array((512 - (body.length % 512)) % 512))
|
|
56
|
+
}
|
|
57
|
+
blocks.push(new Uint8Array(1024))
|
|
58
|
+
return Buffer.concat(blocks)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const respond = (body: Uint8Array, status = 200): Fetch => async () => new Response(body, { status })
|
|
62
|
+
|
|
63
|
+
const redirectTo = (location: string, then: Fetch): Fetch => {
|
|
64
|
+
let redirected = false
|
|
65
|
+
return async (url, init) => {
|
|
66
|
+
if (redirected) {
|
|
67
|
+
return then(url, init)
|
|
68
|
+
}
|
|
69
|
+
redirected = true
|
|
70
|
+
return new Response(null, { status: 302, headers: { location } })
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describe('parseTemplateSource', () => {
|
|
75
|
+
test('reads the degit source forms', () => {
|
|
76
|
+
expect(parseTemplateSource('owner/repo')).toStrictEqual({ host: 'github', user: 'owner', name: 'repo', subdir: [], ref: 'HEAD' })
|
|
77
|
+
expect(parseTemplateSource('owner/repo/templates/basic#v1.2')).toStrictEqual({
|
|
78
|
+
host: 'github',
|
|
79
|
+
user: 'owner',
|
|
80
|
+
name: 'repo',
|
|
81
|
+
subdir: ['templates', 'basic'],
|
|
82
|
+
ref: 'v1.2',
|
|
83
|
+
})
|
|
84
|
+
expect(parseTemplateSource('https://github.com/owner/repo.git').name).toBe('repo')
|
|
85
|
+
expect(parseTemplateSource('git@github.com:owner/repo#feature/x').ref).toBe('feature/x')
|
|
86
|
+
expect(parseTemplateSource('gitlab:owner/repo').host).toBe('gitlab')
|
|
87
|
+
expect(parseTemplateSource('bitbucket.org/owner/repo').host).toBe('bitbucket')
|
|
88
|
+
expect(parseTemplateSource('git.sr.ht/~owner/repo').host).toBe('git.sr.ht')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('rejects unknown hosts, unparsable sources and dot segments', () => {
|
|
92
|
+
expect(() => parseTemplateSource('example.com/owner/repo')).toThrow('supported from GitHub')
|
|
93
|
+
expect(() => parseTemplateSource('repo')).toThrow('Could not parse')
|
|
94
|
+
expect(() => parseTemplateSource('owner/repo/../x')).toThrow('Could not parse')
|
|
95
|
+
expect(() => parseTemplateSource('owner/repo#../main')).toThrow('Could not parse')
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('templateArchiveUrl', () => {
|
|
100
|
+
test('builds each host archive URL', () => {
|
|
101
|
+
const source = (host: 'github' | 'gitlab' | 'bitbucket' | 'git.sr.ht') => ({ host, user: 'o', name: 'r', subdir: [], ref: 'feature/x' })
|
|
102
|
+
expect(templateArchiveUrl(source('github'))).toBe('https://github.com/o/r/archive/feature/x.tar.gz')
|
|
103
|
+
expect(templateArchiveUrl(source('gitlab'))).toBe('https://gitlab.com/o/r/-/archive/feature/x/r-feature-x.tar.gz')
|
|
104
|
+
expect(templateArchiveUrl(source('bitbucket'))).toBe('https://bitbucket.org/o/r/get/feature/x.tar.gz')
|
|
105
|
+
expect(templateArchiveUrl(source('git.sr.ht'))).toBe('https://git.sr.ht/o/r/archive/feature/x.tar.gz')
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
describe('downloadTemplate', () => {
|
|
110
|
+
test('extracts a subdirectory of a git archive with its symlink, executable bit and long path', async () => {
|
|
111
|
+
const target = await createTempDir()
|
|
112
|
+
const requests: string[] = []
|
|
113
|
+
await downloadTemplate('owner/repo/templates/basic#main', target, async (url, init) => {
|
|
114
|
+
requests.push(`${url} ${init.redirect}`)
|
|
115
|
+
return requests.length === 1
|
|
116
|
+
? new Response(null, { status: 302, headers: { location: 'https://codeload.example.com/archive.tar.gz' } })
|
|
117
|
+
: respond(fixture)(url, init)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
expect(requests).toStrictEqual([
|
|
121
|
+
'https://github.com/owner/repo/archive/main.tar.gz manual',
|
|
122
|
+
'https://codeload.example.com/archive.tar.gz manual',
|
|
123
|
+
])
|
|
124
|
+
const deep = 'deep/'.repeat(12)
|
|
125
|
+
expect(await listTree(target)).toStrictEqual([
|
|
126
|
+
'bin/',
|
|
127
|
+
'bin/run.sh',
|
|
128
|
+
'contember.template.yaml',
|
|
129
|
+
...Array.from({ length: 12 }, (_, index) => `${'deep/'.repeat(index + 1)}`),
|
|
130
|
+
`${deep}file-with-a-long-name.txt`,
|
|
131
|
+
'link.yaml -> contember.template.yaml',
|
|
132
|
+
])
|
|
133
|
+
expect((await lstat(join(target, 'bin/run.sh'))).mode & 0o100).toBe(0o100)
|
|
134
|
+
expect(await readFile(join(target, deep, 'file-with-a-long-name.txt'), 'utf8')).toBe('long\n')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('extracts the whole repository without its top-level directory', async () => {
|
|
138
|
+
const target = await createTempDir()
|
|
139
|
+
await downloadTemplate('owner/repo', target, respond(fixture))
|
|
140
|
+
expect((await readdir(target)).sort()).toStrictEqual(['README.md', 'templates'])
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('reports an HTTP error with the URL', async () => {
|
|
144
|
+
const target = await createTempDir()
|
|
145
|
+
await expect(downloadTemplate('owner/repo#nope', target, respond(new Uint8Array(), 404))).rejects.toThrow(
|
|
146
|
+
'Could not download https://github.com/owner/repo/archive/nope.tar.gz: HTTP 404',
|
|
147
|
+
)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('refuses a redirect hop to plain HTTP', async () => {
|
|
151
|
+
const target = await createTempDir()
|
|
152
|
+
const fetchArchive = redirectTo('http://example.com/a.tar.gz', respond(fixture))
|
|
153
|
+
await expect(downloadTemplate('owner/repo', target, fetchArchive)).rejects.toThrow('non-HTTPS')
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
test('stops after too many redirects', async () => {
|
|
157
|
+
const target = await createTempDir()
|
|
158
|
+
const loop: Fetch = async () => new Response(null, { status: 302, headers: { location: 'https://example.com/again' } })
|
|
159
|
+
await expect(downloadTemplate('owner/repo', target, loop)).rejects.toThrow('too many or invalid redirects')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('refuses an archive that is not gzip', async () => {
|
|
163
|
+
const target = await createTempDir()
|
|
164
|
+
await expect(downloadTemplate('owner/repo', target, respond(new TextEncoder().encode('<html>')))).rejects.toThrow('not a readable gzip')
|
|
165
|
+
})
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
describe('tar extraction', () => {
|
|
169
|
+
const extract = async (archive: Uint8Array) => {
|
|
170
|
+
const target = await createTempDir()
|
|
171
|
+
await extractTar(parseTar(archive), join(target, 'out'), { strip: 1 })
|
|
172
|
+
return target
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
test.each([
|
|
176
|
+
['a parent-directory path', [{ path: 'top/../../evil.txt', data: 'x' }]],
|
|
177
|
+
['an absolute path', [{ path: '/tmp/evil.txt', data: 'x' }]],
|
|
178
|
+
['a symlink leaving the template', [{ path: 'top/link', type: '2', link: '../../outside' }]],
|
|
179
|
+
['an absolute symlink', [{ path: 'top/link', type: '2', link: '/etc/passwd' }]],
|
|
180
|
+
['a Windows drive symlink', [{ path: 'top/link', type: '2', link: 'C:/Windows' }]],
|
|
181
|
+
['a backslash symlink', [{ path: 'top/link', type: '2', link: '..\\..\\outside' }]],
|
|
182
|
+
['a file below a symlink', [{ path: 'top/dir', type: '2', link: '.' }, { path: 'top/dir/file.txt', data: 'x' }]],
|
|
183
|
+
['a symlink chain leaving the template', [
|
|
184
|
+
{ path: 'top/d/l', type: '2', link: '..' },
|
|
185
|
+
{ path: 'top/esc', type: '2', link: 'd/l/..' },
|
|
186
|
+
{ path: 'top/esc/planted', type: '2', link: 'x' },
|
|
187
|
+
]],
|
|
188
|
+
])('refuses %s before writing anything', async (_, entries) => {
|
|
189
|
+
const target = await createTempDir()
|
|
190
|
+
// nested, so that a `..` escaping the output directory would still land inside `target`
|
|
191
|
+
await expect(extractTar(parseTar(tar(entries)), join(target, 'a/b/out'), { strip: 1 })).rejects.toThrow('Refusing')
|
|
192
|
+
expect(await readdir(target)).toStrictEqual([])
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
// A link the name checks cannot see stands in for an alias on a case-insensitive filesystem (`D/L` is `d/l`).
|
|
196
|
+
test.each([
|
|
197
|
+
['a file written through it', [{ path: 'top/alias/x.txt', data: 'x' }]],
|
|
198
|
+
['a symlink created through it', [{ path: 'top/alias/x', type: '2', link: 'y' }]],
|
|
199
|
+
['a symlink resolving through it', [{ path: 'top/esc', type: '2', link: 'alias/..' }]],
|
|
200
|
+
])('checks the filesystem for links the names do not show: %s', async (_, entries) => {
|
|
201
|
+
const target = await createTempDir()
|
|
202
|
+
const out = join(target, 'a/b/out')
|
|
203
|
+
await mkdir(out, { recursive: true })
|
|
204
|
+
await symlink('..', join(out, 'alias'))
|
|
205
|
+
await expect(extractTar(parseTar(tar(entries)), out, { strip: 1 })).rejects.toThrow('Refusing')
|
|
206
|
+
expect(await readdir(join(target, 'a/b'))).toStrictEqual(['out'])
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
test('refuses a too deeply nested path', async () => {
|
|
210
|
+
const entries: TarEntry[] = [{ type: 'file', path: `top/${'a/'.repeat(300)}x`, mode: 0o644, data: new Uint8Array() }]
|
|
211
|
+
await expect(extractTar(entries, join(await createTempDir(), 'out'), { strip: 1 })).rejects.toThrow('too deeply nested path')
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
test('refuses a symlink resolving through a file', async () => {
|
|
215
|
+
const archive = tar([{ path: 'top/f.txt', data: 'x' }, { path: 'top/link', type: '2', link: 'f.txt/x/../g' }])
|
|
216
|
+
await expect(extract(archive)).rejects.toThrow('symlink resolving through a file')
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
test('refuses an overlong path', async () => {
|
|
220
|
+
const entries: TarEntry[] = [{ type: 'file', path: `top/${'a/'.repeat(5000)}x`, mode: 0o644, data: new Uint8Array() }]
|
|
221
|
+
await expect(extractTar(entries, join(await createTempDir(), 'out'), { strip: 1 })).rejects.toThrow('overlong path')
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('allows a symlink to a symlink inside the template', async () => {
|
|
225
|
+
const target = await extract(tar([
|
|
226
|
+
{ path: 'top/c.txt', data: 'c' },
|
|
227
|
+
{ path: 'top/b', type: '2', link: 'c.txt' },
|
|
228
|
+
{ path: 'top/a', type: '2', link: 'b' },
|
|
229
|
+
]))
|
|
230
|
+
expect(await readFile(join(target, 'out/a'), 'utf8')).toBe('c')
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
test('ignores unusual names outside the requested subdirectory', async () => {
|
|
234
|
+
const target = await createTempDir()
|
|
235
|
+
const entries = parseTar(tar([{ path: 'top/other/back\\slash.txt', data: 'x' }, { path: 'top/tpl/a.txt', data: 'a' }]))
|
|
236
|
+
await extractTar(entries, join(target, 'out'), { strip: 1, subdir: ['tpl'] })
|
|
237
|
+
expect(await readdir(join(target, 'out'))).toStrictEqual(['a.txt'])
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
test('reads GNU long names and long link targets', async () => {
|
|
241
|
+
const longName = `top/${'n'.repeat(120)}.txt`
|
|
242
|
+
const longLink = `${'n'.repeat(120)}.txt`
|
|
243
|
+
const target = await extract(tar([
|
|
244
|
+
{ path: '././@LongLink', type: 'L', data: `${longName}\0` },
|
|
245
|
+
{ path: 'top/truncated', data: 'long' },
|
|
246
|
+
{ path: '././@LongLink', type: 'K', data: `${longLink}\0` },
|
|
247
|
+
{ path: 'top/link', type: '2', link: 'truncated' },
|
|
248
|
+
]))
|
|
249
|
+
expect(await readFile(join(target, 'out', `${'n'.repeat(120)}.txt`), 'utf8')).toBe('long')
|
|
250
|
+
expect(await readlink(join(target, 'out/link'))).toBe(longLink)
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('rejects hard links and other entry types', () => {
|
|
254
|
+
expect(() => parseTar(tar([{ path: 'top/a', type: '1', link: 'top/b' }]))).toThrow('Unsupported tar entry')
|
|
255
|
+
expect(() => parseTar(tar([{ path: 'top/fifo', type: '6' }]))).toThrow('Unsupported tar entry')
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
test('rejects a PAX size that is not a plain number', () => {
|
|
259
|
+
const record = '14 size=-1600\n'
|
|
260
|
+
expect(() => parseTar(tar([{ path: 'pax', type: 'x', data: record }, { path: 'top/a', data: '' }]))).toThrow('unreadable PAX size')
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
test('rejects a corrupted header and a truncated archive', () => {
|
|
264
|
+
const archive = tar([{ path: 'top/a.txt', data: 'hello' }])
|
|
265
|
+
const corrupted = new Uint8Array(archive)
|
|
266
|
+
corrupted[0] = 'X'.charCodeAt(0)
|
|
267
|
+
expect(() => parseTar(corrupted)).toThrow('checksum')
|
|
268
|
+
expect(() => parseTar(archive.subarray(0, 512))).toThrow('exceeds the archive')
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
test('keeps paths from PAX headers', async () => {
|
|
272
|
+
const pax = `path=top/${'p'.repeat(150)}.txt\n`
|
|
273
|
+
const record = `${pax.length + 4} ${pax}`
|
|
274
|
+
const target = await extract(tar([{ path: 'pax', type: 'x', data: record }, { path: 'ignored', data: 'pax' }]))
|
|
275
|
+
expect(await readFile(join(target, 'out', `${'p'.repeat(150)}.txt`), 'utf8')).toBe('pax')
|
|
276
|
+
})
|
|
277
|
+
})
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdir, mkdtemp, readdir, readFile, rm, symlink, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { FileSystem } from '../../../src/lib/FileSystem.js'
|
|
6
|
+
import { TemplateInstaller } from '../../../src/lib/TemplateInstaller.js'
|
|
7
|
+
|
|
8
|
+
const temporaryDirectories: string[] = []
|
|
9
|
+
|
|
10
|
+
afterEach(async () => {
|
|
11
|
+
await Promise.all(temporaryDirectories.splice(0).map(path => rm(path, { recursive: true, force: true })))
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
interface Fixture {
|
|
15
|
+
installer: TemplateInstaller
|
|
16
|
+
templateDir: string
|
|
17
|
+
/** The project the installer creates; it does not exist before the install. */
|
|
18
|
+
target: string
|
|
19
|
+
/** A sibling of the project holding `victim.txt`; directives must never touch it. */
|
|
20
|
+
outside: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Layout: `<root>/resources/templates/default`, `<root>/work/project` (target) and `<root>/work/outside`. */
|
|
24
|
+
const createFixture = async (config: string): Promise<Fixture> => {
|
|
25
|
+
const root = await mkdtemp(join(tmpdir(), 'contember-template-containment-'))
|
|
26
|
+
temporaryDirectories.push(root)
|
|
27
|
+
const templateDir = join(root, 'resources/templates/default')
|
|
28
|
+
await mkdir(join(templateDir, 'sub'), { recursive: true })
|
|
29
|
+
await writeFile(join(templateDir, 'contember.template.yaml'), config)
|
|
30
|
+
await writeFile(join(templateDir, 'README.md'), 'readme {projectName}')
|
|
31
|
+
const outside = join(root, 'work/outside')
|
|
32
|
+
await mkdir(outside, { recursive: true })
|
|
33
|
+
await writeFile(join(outside, 'victim.txt'), 'victim {projectName}')
|
|
34
|
+
const installer = new TemplateInstaller(join(root, 'resources'), new FileSystem(), { warn: () => {} })
|
|
35
|
+
return { installer, templateDir, target: join(root, 'work/project'), outside }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const expectOutsideUntouched = async (outside: string) => {
|
|
39
|
+
expect((await readdir(outside)).sort()).toEqual(['victim.txt'])
|
|
40
|
+
expect(await readFile(join(outside, 'victim.txt'), 'utf8')).toBe('victim {projectName}')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const install = (fixture: Fixture) => fixture.installer.installTemplate('default', fixture.target, { projectName: 'acme' })
|
|
44
|
+
|
|
45
|
+
describe('template directives stay inside the project', () => {
|
|
46
|
+
const traversal: [string, string][] = [
|
|
47
|
+
['rename source with ..', 'rename:\n ../outside/victim.txt: stolen.txt\n'],
|
|
48
|
+
['rename target with ..', 'rename:\n README.md: ../outside/planted.txt\n'],
|
|
49
|
+
['copy source with ..', 'copy:\n ../outside: stolen\n'],
|
|
50
|
+
['copy target with ..', 'copy:\n README.md: ../outside/planted.txt\n'],
|
|
51
|
+
['copy target with .. in the middle', 'copy:\n README.md: sub/../../outside/planted.txt\n'],
|
|
52
|
+
['replaceVariables with ..', 'replaceVariables:\n - ../outside/victim.txt\n'],
|
|
53
|
+
]
|
|
54
|
+
for (const [name, config] of traversal) {
|
|
55
|
+
test(`refuses a ${name}`, async () => {
|
|
56
|
+
const fixture = await createFixture(config)
|
|
57
|
+
await expect(install(fixture)).rejects.toThrow('points outside the project')
|
|
58
|
+
await expectOutsideUntouched(fixture.outside)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
test('refuses absolute paths', async () => {
|
|
63
|
+
for (const directive of ['rename', 'copy']) {
|
|
64
|
+
const probe = await createFixture('{}')
|
|
65
|
+
const fixture = await createFixture(`${directive}:\n README.md: ${JSON.stringify(join(probe.outside, 'planted.txt'))}\n`)
|
|
66
|
+
await expect(install(fixture)).rejects.toThrow('points outside the project')
|
|
67
|
+
await expectOutsideUntouched(probe.outside)
|
|
68
|
+
}
|
|
69
|
+
const fixture = await createFixture('{}')
|
|
70
|
+
const absolute = await createFixture(`replaceVariables:\n - ${JSON.stringify(join(fixture.outside, 'victim.txt'))}\n`)
|
|
71
|
+
await expect(install(absolute)).rejects.toThrow('points outside the project')
|
|
72
|
+
await expectOutsideUntouched(fixture.outside)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('refuses the project directory itself', async () => {
|
|
76
|
+
const fixture = await createFixture('rename:\n sub/..: renamed\n')
|
|
77
|
+
await expect(install(fixture)).rejects.toThrow('points outside the project')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const throughSymlink: [string, string][] = [
|
|
81
|
+
['rename target', 'rename:\n README.md: link/planted.txt\n'],
|
|
82
|
+
['rename source', 'rename:\n link/victim.txt: stolen.txt\n'],
|
|
83
|
+
['copy target', 'copy:\n README.md: link/planted.txt\n'],
|
|
84
|
+
['copy target in a new directory', 'copy:\n README.md: link/new/planted.txt\n'],
|
|
85
|
+
['copy source', 'copy:\n link: stolen\n'],
|
|
86
|
+
['replaceVariables', 'replaceVariables:\n - link/victim.txt\n'],
|
|
87
|
+
]
|
|
88
|
+
for (const [name, config] of throughSymlink) {
|
|
89
|
+
test(`refuses a ${name} through a symlink that leaves the project`, async () => {
|
|
90
|
+
const fixture = await createFixture(config)
|
|
91
|
+
await symlink(fixture.outside, join(fixture.templateDir, 'link'))
|
|
92
|
+
await expect(install(fixture)).rejects.toThrow('points outside the project')
|
|
93
|
+
await expectOutsideUntouched(fixture.outside)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
test('refuses a copy onto a dangling symlink that leaves the project', async () => {
|
|
98
|
+
const fixture = await createFixture('copy:\n README.md: dangling\n')
|
|
99
|
+
await symlink(join(fixture.outside, 'planted.txt'), join(fixture.templateDir, 'dangling'))
|
|
100
|
+
await expect(install(fixture)).rejects.toThrow('passes through a dangling symlink')
|
|
101
|
+
await expectOutsideUntouched(fixture.outside)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('refuses a copy into an existing directory, whose entries could be symlinks that leave the project', async () => {
|
|
105
|
+
const fixture = await createFixture('copy:\n src: dst\n')
|
|
106
|
+
await mkdir(join(fixture.templateDir, 'src/vdir'), { recursive: true })
|
|
107
|
+
await writeFile(join(fixture.templateDir, 'src/vdir/planted.txt'), 'planted')
|
|
108
|
+
await writeFile(join(fixture.templateDir, 'src/victim.txt'), 'overwritten')
|
|
109
|
+
await mkdir(join(fixture.templateDir, 'dst'))
|
|
110
|
+
await symlink(fixture.outside, join(fixture.templateDir, 'dst/vdir'))
|
|
111
|
+
await symlink(join(fixture.outside, 'victim.txt'), join(fixture.templateDir, 'dst/victim.txt'))
|
|
112
|
+
await expect(install(fixture)).rejects.toThrow('Template path "dst" already exists')
|
|
113
|
+
await expectOutsideUntouched(fixture.outside)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('refuses a copy onto an existing file', async () => {
|
|
117
|
+
const fixture = await createFixture('copy:\n README.md: sub/README.md\n')
|
|
118
|
+
await writeFile(join(fixture.templateDir, 'sub/README.md'), 'kept')
|
|
119
|
+
await expect(install(fixture)).rejects.toThrow('Template path "sub/README.md" already exists')
|
|
120
|
+
expect(await readFile(join(fixture.target, 'sub/README.md'), 'utf8')).toBe('kept')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('still applies directives inside the project, also through a symlink inside it', async () => {
|
|
124
|
+
const fixture = await createFixture(
|
|
125
|
+
'rename:\n README.md: inner/README.md\ncopy:\n sub: new/nested/copy\nreplaceVariables:\n - sub/README.md\n - missing.txt\n',
|
|
126
|
+
)
|
|
127
|
+
await symlink(join(fixture.target, 'sub'), join(fixture.templateDir, 'inner'))
|
|
128
|
+
await install(fixture)
|
|
129
|
+
expect(await readFile(join(fixture.target, 'sub/README.md'), 'utf8')).toBe('readme acme')
|
|
130
|
+
expect(await readFile(join(fixture.target, 'new/nested/copy/README.md'), 'utf8')).toBe('readme {projectName}')
|
|
131
|
+
await expectOutsideUntouched(fixture.outside)
|
|
132
|
+
})
|
|
133
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { afterEach, expect, test } from 'bun:test'
|
|
2
|
+
import { mkdir, mkdtemp, readFile, readlink, rm, symlink, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { FileSystem } from '../../../src/lib/FileSystem.js'
|
|
6
|
+
import { TemplateInstaller } from '../../../src/lib/TemplateInstaller.js'
|
|
7
|
+
|
|
8
|
+
const temporaryDirectories: string[] = []
|
|
9
|
+
|
|
10
|
+
afterEach(async () => {
|
|
11
|
+
await Promise.all(temporaryDirectories.splice(0).map(path => rm(path, { recursive: true, force: true })))
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const createTempDir = async (prefix: string): Promise<string> => {
|
|
15
|
+
const dir = await mkdtemp(join(tmpdir(), prefix))
|
|
16
|
+
temporaryDirectories.push(dir)
|
|
17
|
+
return dir
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
test('keeps relative symlinks of a remote template relative', async () => {
|
|
21
|
+
const installer = new TemplateInstaller('/unused', new FileSystem(), { warn: () => {} }, async (_source, directory) => {
|
|
22
|
+
await writeFile(join(directory, 'contember.template.yaml'), 'copy:\n docs: docs-copy\n')
|
|
23
|
+
await writeFile(join(directory, 'README.md'), 'readme')
|
|
24
|
+
await mkdir(join(directory, 'docs'))
|
|
25
|
+
await symlink('../README.md', join(directory, 'docs/README.md'))
|
|
26
|
+
await symlink('README.md', join(directory, 'LINK.md'))
|
|
27
|
+
})
|
|
28
|
+
const target = join(await createTempDir('contember-template-target-'), 'project')
|
|
29
|
+
|
|
30
|
+
await installer.installTemplate('owner/repository', target)
|
|
31
|
+
|
|
32
|
+
expect(await readlink(join(target, 'LINK.md'))).toBe('README.md')
|
|
33
|
+
expect(await readlink(join(target, 'docs/README.md'))).toBe('../README.md')
|
|
34
|
+
expect(await readlink(join(target, 'docs-copy/README.md'))).toBe('../README.md')
|
|
35
|
+
expect(await readFile(join(target, 'docs/README.md'), 'utf8')).toBe('readme')
|
|
36
|
+
expect(await readFile(join(target, 'docs-copy/README.md'), 'utf8')).toBe('readme')
|
|
37
|
+
})
|