Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

fix(download): reject exhausted redirects during link probing (#125)

Rudy Mizrahi Celekli7795d031d2f8
diff --git a/e2e/probeRedirects.spec.ts b/e2e/probeRedirects.spec.ts
new file mode 100644
index 0000000..4016a5d
--- /dev/null
+++ b/e2e/probeRedirects.spec.ts
@@ -0,0 +1,34 @@
+import { test, expect, BLOCK } from './fixtures'
+
+test.describe('probe redirect limits @smoke', () => {
+ for (const count of [0, 5]) {
+ test(`${count} redirects reach the real file metadata`, async ({ plexo, serve }) => {
+ const origin = await serve({ size: 3 * BLOCK })
+ origin.setRule(({ path }) => {
+ const remaining = Number(/^\/hop\/(\d+)$/.exec(path)?.[1] ?? 0)
+ return remaining > 0 ? { redirect: `/hop/${remaining - 1}` } : 'ok'
+ })
+ const result = await plexo.api.probeUrl(origin.url(`/hop/${count}`))
+ expect(result.kind).toBe('http')
+ if (result.kind !== 'http') throw new Error('Expected HTTP probe')
+ expect(result.finalUrl).toBe(origin.url('/hop/0'))
+ expect(result.totalBytes).toBe(3 * BLOCK)
+ expect(result.supportsRanges).toBe(true)
+ })
+ }
+
+ for (const path of ['/hop/6', '/loop']) {
+ test(`${path} rejects redirect exhaustion before offering a download`, async ({
+ plexo,
+ serve
+ }) => {
+ const origin = await serve({ size: 3 * BLOCK })
+ origin.setRule(({ path: requestPath }) => {
+ if (requestPath === '/loop') return { redirect: '/loop' }
+ const remaining = Number(/^\/hop\/(\d+)$/.exec(requestPath)?.[1] ?? 0)
+ return remaining > 0 ? { redirect: `/hop/${remaining - 1}` } : 'ok'
+ })
+ await expect(plexo.api.probeUrl(origin.url(path))).rejects.toThrow(/Too many redirects/)
+ })
+ }
+})
diff --git a/src/main/download/probe.ts b/src/main/download/probe.ts
index de17b58..6d578f4 100644
--- a/src/main/download/probe.ts
+++ b/src/main/download/probe.ts
@@ -109,6 +109,9 @@ async function requestFollowingRedirects(
if (response.statusCode >= 300 && response.statusCode < 400) {
const location = headerValue(response.headers, 'location')
if (!location) break
+ if (redirect === MAX_REDIRECTS) {
+ throw new Error('Too many redirects while checking the link')
+ }
current = new URL(location, current)
continue
}