Skip to content

Commit 2e702bf

Browse files
add redirect handling with limit in downloadFile function
1 parent 782f1de commit 2e702bf

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

cli/src/utils/downloadProverBinary.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { pipeline } from "stream/promises";
66

77
const PROVER_VERSION = "1.0.2";
88
const GITHUB_RELEASES_BASE_URL = `https://github.com/Lightprotocol/light-protocol/releases/download/light-prover-v${PROVER_VERSION}`;
9+
const MAX_REDIRECTS = 10;
910

1011
interface DownloadOptions {
1112
maxRetries?: number;
@@ -59,7 +60,11 @@ export async function downloadProverBinary(
5960
);
6061
}
6162

62-
async function downloadFile(url: string, outputPath: string): Promise<void> {
63+
async function downloadFile(
64+
url: string,
65+
outputPath: string,
66+
redirectDepth: number = 0,
67+
): Promise<void> {
6368
return new Promise((resolve, reject) => {
6469
const protocol = url.startsWith("https") ? https : http;
6570

@@ -74,7 +79,17 @@ async function downloadFile(url: string, outputPath: string): Promise<void> {
7479
if (!redirectUrl) {
7580
return reject(new Error("Redirect without location header"));
7681
}
77-
return downloadFile(redirectUrl, outputPath).then(resolve, reject);
82+
if (redirectDepth >= MAX_REDIRECTS) {
83+
return reject(
84+
new Error(
85+
`Too many redirects: exceeded maximum of ${MAX_REDIRECTS} redirects`,
86+
),
87+
);
88+
}
89+
return downloadFile(redirectUrl, outputPath, redirectDepth + 1).then(
90+
resolve,
91+
reject,
92+
);
7893
}
7994

8095
if (response.statusCode !== 200) {

0 commit comments

Comments
 (0)