File size: 2,143 Bytes
2276d59
 
 
 
 
 
 
 
65fc551
 
 
2276d59
 
65fc551
 
 
 
 
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65fc551
 
 
 
2276d59
 
 
 
 
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
import { describe, it, expect, vi } from 'vitest';
import request from 'supertest';
import type { Request, Response } from 'express';
import type { Format } from '../../src/types/index.js';

// stub upstream fetch; no real network
vi.mock('../../src/utils/network/proxy.util.js', async (importActual) => {
  const actual =
    await importActual<
      typeof import('../../src/utils/network/proxy.util.js')
    >();
  return {
    ...actual,
    pipeWebStream: vi.fn((_url: string, res: Response): Promise<boolean> => {
      res.status(200);
      res.end('FAKE_MP4_BYTES');
      return Promise.resolve(true);
    }),
  };
});

import app from '../../src/app.js';
import { buildProxyUrl } from '../../src/utils/media/stream.util.js';
import { pipeWebStream } from '../../src/utils/network/proxy.util.js';

const req = {
  headers: {},
  get: () => 'localhost:5000',
  protocol: 'http',
} as unknown as Request;

// direct format yields an encoded rawUrl
const directFormat = {
  formatId: '22',
  extension: 'mp4',
  url: 'https://r1.googlevideo.com/videoplayback?id=xyz&n=abc',
} as unknown as Format;

const pathOf = (signed: string): string => {
  const parsed = new URL(signed);
  return parsed.pathname + parsed.search;
};

describe('signed /proxy end-to-end', () => {
  it('streams a validly signed proxy URL', async () => {
    vi.mocked(pipeWebStream).mockClear();
    const signed = buildProxyUrl(
      req,
      directFormat,
      'https://youtube.com/watch?v=abc'
    );
    const res = await request(app).get(pathOf(signed as string));
    expect(res.status).toBe(200);
    expect(vi.mocked(pipeWebStream)).toHaveBeenCalledTimes(1);
  });

  it('blocks a tampered signature before fetching upstream', async () => {
    vi.mocked(pipeWebStream).mockClear();
    const signed = buildProxyUrl(
      req,
      directFormat,
      'https://youtube.com/watch?v=abc'
    );
    const tampered = pathOf(signed as string).replace(
      /sig=[^&]+/u,
      'sig=forged'
    );
    const res = await request(app).get(tampered);
    expect(res.status).toBe(403);
    expect(vi.mocked(pipeWebStream)).not.toHaveBeenCalled();
  });
});