Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Studio: fetch GitHub files whose names start with two dots (#267)

DarkChunk179fb68f62a3
diff --git a/.changeset/github-dot-prefixed-files.md b/.changeset/github-dot-prefixed-files.md
new file mode 100644
index 0000000..8d0d253
--- /dev/null
+++ b/.changeset/github-dot-prefixed-files.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Studio: opening a film from GitHub keeps files and folders whose names start with two dots, such as `..assets`. These names are no longer mistaken for paths outside the project.
diff --git a/packages/openfilm/studio/server/fetch-github.mjs b/packages/openfilm/studio/server/fetch-github.mjs
index 3282f41..dc542a0 100644
--- a/packages/openfilm/studio/server/fetch-github.mjs
+++ b/packages/openfilm/studio/server/fetch-github.mjs
@@ -9,7 +9,7 @@
*/
import { createWriteStream } from 'node:fs';
import { mkdir } from 'node:fs/promises';
-import { dirname, join, relative, isAbsolute } from 'node:path';
+import { dirname, join, relative, isAbsolute, sep } from 'node:path';
import { Readable, Transform } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { FetchError } from './fetch-error.mjs';
@@ -151,7 +151,7 @@ export async function downloadFiles(at, commit, folder, files, { dir, signal, ho
const one = async (/** @type {RemoteFile} */ file) => {
const to = join(dir, ...file.path.split('/'));
const inside = relative(dir, to);
- if (!inside || inside.startsWith('..') || isAbsolute(inside)) throw new FetchError(`“${file.path}” is not a path in the folder.`, 422, 'failed');
+ if (!inside || inside.split(sep)[0] === '..' || isAbsolute(inside)) throw new FetchError(`“${file.path}” is not a path in the folder.`, 422, 'failed');
await mkdir(dirname(to), { recursive: true });
/* a connection that drops is tried again, a little later each time, from the start of the file */
const told = { counted: 0 };
diff --git a/packages/openfilm/studio/server/fetch-repo.test.mjs b/packages/openfilm/studio/server/fetch-repo.test.mjs
index 3574434..a4a40cd 100644
--- a/packages/openfilm/studio/server/fetch-repo.test.mjs
+++ b/packages/openfilm/studio/server/fetch-repo.test.mjs
@@ -8,6 +8,7 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { FetchError, cloneInto, createFetches, fetchFilm, parseRepoUrl, splitTree } from './fetch-repo.mjs';
+import { downloadFiles } from './fetch-github.mjs';
const refused = (input, code) => assert.throws(() => parseRepoUrl(input), (e) => e instanceof FetchError && e.code === code, String(input));
@@ -74,11 +75,15 @@ before(() => {
const work = join(scratch, 'work');
mkdirSync(join(work, 'films', 'one', 'assets'), { recursive: true });
mkdirSync(join(work, 'films', 'two'), { recursive: true });
+ mkdirSync(join(work, 'films', 'dots', '..assets'), { recursive: true });
mkdirSync(join(work, 'docs'), { recursive: true });
writeFileSync(join(work, 'README.md'), '# films\n');
writeFileSync(join(work, 'films', 'one', 'film.html'), '<!doctype html><title>one</title>');
writeFileSync(join(work, 'films', 'one', 'assets', 'a.txt'), 'a');
writeFileSync(join(work, 'films', 'two', 'film.html'), '<!doctype html><title>two</title>');
+ writeFileSync(join(work, 'films', 'dots', 'film.html'), '<!doctype html><img src="..assets/frame.svg">');
+ writeFileSync(join(work, 'films', 'dots', '..notes.md'), 'notes');
+ writeFileSync(join(work, 'films', 'dots', '..assets', 'frame.svg'), '<svg/>');
writeFileSync(join(work, 'docs', 'notes.md'), 'no film here');
writeFileSync(join(work, 'big.bin'), randomBytes(512 * 1024));
writeFileSync(join(work, 'film.html'), '<!doctype html><title>root</title>');
@@ -247,6 +252,28 @@ test('over https: a branch with a slash in its name; the whole repository; a fil
assert.equal(await fetchFilm(overHttp(['main', 'films', 'two', 'film.html'], true), { library: lib, hosts: github.hosts }), join(lib, 'two'));
});
+test('over https: files and folders whose names start with two dots are fetched, as through git', async () => {
+ const lib = library();
+ const tree = ['main', 'films', 'dots'];
+ const fromGit = await fetchFilm(source(tree), { library: lib });
+ const fromHttp = await fetchFilm(overHttp(tree), { library: lib, hosts: github.hosts });
+ for (const path of ['film.html', '..notes.md', '..assets/frame.svg']) {
+ assert.equal(readFileSync(join(fromHttp, path), 'utf8'), readFileSync(join(fromGit, path), 'utf8'), path);
+ }
+ assert.deepEqual(readdirSync(lib).sort(), ['dots', 'dots 2']);
+});
+
+test('over https: a download path outside the folder is refused before any file is requested', async () => {
+ const dir = library();
+ asked.length = 0;
+ for (const path of ['', '..', '../outside.txt', 'assets/../../outside.txt']) {
+ await assert.rejects(downloadFiles({ owner: 'o', repo: 'r' }, COMMIT, [], [{ path, size: 1 }], { dir, hosts: github.hosts }),
+ (e) => e instanceof FetchError && e.status === 422);
+ }
+ assert.deepEqual(asked, []);
+ assert.deepEqual(readdirSync(dir), []);
+});
+
test('over https: no film, no folder, no branch, too big: said before anything downloads', async () => {
const lib = library();
const fails = async (src, code, options = {}) => {