-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathremoteSource.spec.ts
More file actions
246 lines (209 loc) · 8.03 KB
/
remoteSource.spec.ts
File metadata and controls
246 lines (209 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { expect } from "chai";
import * as sinon from "sinon";
import * as fs from "fs";
import * as path from "path";
import * as mockfs from "mock-fs";
import * as archiver from "archiver";
import { Writable } from "stream";
import { getRemoteSource, requireFunctionsYaml } from "./remoteSource";
import { FirebaseError } from "../../error";
import * as downloadUtils from "../../downloadUtils";
describe("remoteSource", () => {
describe("requireFunctionsYaml", () => {
afterEach(() => {
mockfs.restore();
});
it("should not throw if functions.yaml exists", () => {
mockfs({
"/app/functions.yaml": "runtime: nodejs16",
});
expect(() => requireFunctionsYaml("/app")).to.not.throw();
});
it("should throw FirebaseError if functions.yaml is missing", () => {
mockfs({
"/app/index.js": "console.log('hello')",
});
expect(() => requireFunctionsYaml("/app")).to.throw(
FirebaseError,
/The remote repository is missing a required deployment manifest/,
);
});
});
describe("getRemoteSource", () => {
let downloadToTmpStub: sinon.SinonStub;
beforeEach(() => {
downloadToTmpStub = sinon.stub(downloadUtils, "downloadToTmp");
});
afterEach(() => {
sinon.restore();
mockfs.restore();
});
async function createZipBuffer(
files: { [path: string]: string },
topLevelDir?: string,
): Promise<Buffer> {
const archive = archiver("zip", { zlib: { level: 9 } });
const chunks: Buffer[] = [];
const output = new Writable({
write(chunk, _encoding, callback) {
chunks.push(chunk instanceof Buffer ? chunk : Buffer.from(chunk));
callback();
},
});
return new Promise((resolve, reject) => {
output.on("finish", () => resolve(Buffer.concat(chunks as unknown as Uint8Array[])));
archive.on("error", (err) => reject(err));
archive.pipe(output);
for (const [filePath, content] of Object.entries(files)) {
const entryPath = topLevelDir ? path.join(topLevelDir, filePath) : filePath;
archive.append(content, { name: entryPath });
}
archive.finalize();
});
}
it("should use GitHub Archive API for GitHub URLs", async () => {
const zipBuffer = await createZipBuffer(
{ "functions.yaml": "runtime: nodejs16" },
"repo-main",
);
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
const sourceDir = await getRemoteSource("https://github.com/org/repo", "main", "/dest");
expect(downloadToTmpStub.calledOnce).to.be.true;
expect(downloadToTmpStub.firstCall.args[0]).to.equal(
"https://github.com/org/repo/archive/main.zip",
);
expect(sourceDir).to.match(/repo-main$/);
expect(sourceDir).to.contain("/dest");
expect(fs.statSync(path.join(sourceDir, "functions.yaml")).isFile()).to.be.true;
});
it("should support org/repo shorthand", async () => {
const zipBuffer = await createZipBuffer(
{ "functions.yaml": "runtime: nodejs16" },
"repo-main",
);
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
const sourceDir = await getRemoteSource("org/repo", "main", "/dest");
expect(downloadToTmpStub.calledOnce).to.be.true;
expect(downloadToTmpStub.firstCall.args[0]).to.equal(
"https://github.com/org/repo/archive/main.zip",
);
expect(sourceDir).to.match(/repo-main$/);
});
it("should strip top-level directory from GitHub archive", async () => {
const zipBuffer = await createZipBuffer(
{ "functions.yaml": "runtime: nodejs16" },
"repo-main",
);
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
const sourceDir = await getRemoteSource("https://github.com/org/repo", "main", "/dest");
expect(sourceDir).to.match(/repo-main$/);
expect(fs.statSync(path.join(sourceDir, "functions.yaml")).isFile()).to.be.true;
});
it("should NOT strip top-level directory if multiple files exist at root", async () => {
const zipBuffer = await createZipBuffer({
"file1.txt": "content",
"functions.yaml": "runtime: nodejs16",
"repo-main/index.js": "console.log('hello')",
});
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
const sourceDir = await getRemoteSource("https://github.com/org/repo", "main", "/dest");
expect(sourceDir).to.not.match(/repo-main$/);
expect(sourceDir).to.equal("/dest");
expect(fs.statSync(path.join(sourceDir, "file1.txt")).isFile()).to.be.true;
expect(fs.statSync(path.join(sourceDir, "functions.yaml")).isFile()).to.be.true;
});
it("should throw error if GitHub Archive download fails", async () => {
mockfs({ "/dest": {} });
downloadToTmpStub.rejects(new Error("404 Not Found"));
await expect(
getRemoteSource("https://github.com/org/repo", "main", "/dest"),
).to.be.rejectedWith(FirebaseError, /Failed to download GitHub archive/);
});
it("should throw error for non-GitHub URLs", async () => {
mockfs({ "/dest": {} });
await expect(
getRemoteSource("https://gitlab.com/org/repo", "main", "/dest"),
).to.be.rejectedWith(FirebaseError, /Only GitHub repositories are supported/);
});
it("should validate subdirectory exists after clone", async () => {
const zipBuffer = await createZipBuffer(
{ "functions.yaml": "runtime: nodejs16" },
"repo-main",
);
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
await expect(
getRemoteSource("https://github.com/org/repo", "main", "/dest", "nonexistent"),
).to.be.rejectedWith(FirebaseError, /Directory 'nonexistent' not found/);
});
it("should return source even if functions.yaml is missing", async () => {
const zipBuffer = await createZipBuffer({ "index.js": "console.log('hello')" }, "repo-main");
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
const sourceDir = await getRemoteSource("https://github.com/org/repo", "main", "/dest");
expect(sourceDir).to.match(/repo-main$/);
expect(fs.statSync(path.join(sourceDir, "index.js")).isFile()).to.be.true;
expect(() => fs.statSync(path.join(sourceDir, "functions.yaml"))).to.throw();
});
it("should prevent path traversal in subdirectory", async () => {
const zipBuffer = await createZipBuffer(
{ "functions.yaml": "runtime: nodejs16" },
"repo-main",
);
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
await expect(
getRemoteSource("https://github.com/org/repo", "main", "/dest", "../outside"),
).to.be.rejectedWith(FirebaseError, /must not escape/);
});
it("should return subdirectory if specified", async () => {
const zipBuffer = await createZipBuffer(
{
"functions.yaml": "runtime: nodejs16",
"app/index.js": "console.log('hello')",
"app/functions.yaml": "runtime: nodejs16",
},
"repo-main",
);
mockfs({
"/tmp/source.zip": zipBuffer,
"/dest": {},
});
downloadToTmpStub.resolves("/tmp/source.zip");
const sourceDir = await getRemoteSource(
"https://github.com/org/repo",
"main",
"/dest",
"app",
);
expect(sourceDir).to.match(/repo-main\/app$/);
expect(fs.statSync(path.join(sourceDir, "index.js")).isFile()).to.be.true;
expect(fs.statSync(path.join(sourceDir, "functions.yaml")).isFile()).to.be.true;
});
});
});