File size: 1,569 Bytes
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c48b9be
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
import { describe, it, expect, afterEach } from 'vitest';
import tls from 'node:tls';
import {
  randomizeCiphers,
  startCipherRotation,
} from '../../src/utils/network/cipher.util.js';

describe('TLS Cipher Randomization', () => {
  const originalCiphers = tls.DEFAULT_CIPHERS;

  afterEach(() => {
    tls.DEFAULT_CIPHERS = originalCiphers;
  });

  it('changes cipher order on each call', () => {
    randomizeCiphers();
    const first = tls.DEFAULT_CIPHERS;
    randomizeCiphers();
    const second = tls.DEFAULT_CIPHERS;

    expect(first).not.toBe(originalCiphers);
    expect(second).not.toBe(first);
  });

  it('preserves total cipher count', () => {
    const originalCount = originalCiphers.split(':').length;
    randomizeCiphers();
    expect(tls.DEFAULT_CIPHERS.split(':')).toHaveLength(originalCount);
  });

  it('only shuffles top 8 ciphers', () => {
    randomizeCiphers();
    const afterShuffle = tls.DEFAULT_CIPHERS.split(':');
    const originalList = originalCiphers.split(':');

    // ciphers after position 8 should be unchanged
    expect(afterShuffle.slice(8)).toEqual(originalList.slice(8));
  });

  it('top 8 contains same ciphers in different order', () => {
    randomizeCiphers();
    const shuffledTop = tls.DEFAULT_CIPHERS.split(':').slice(0, 8).sort();
    const originalTop = originalCiphers.split(':').slice(0, 8).sort();

    expect(shuffledTop).toEqual(originalTop);
  });

  it('startCipherRotation applies immediately', () => {
    startCipherRotation();
    expect(tls.DEFAULT_CIPHERS).not.toBe(originalCiphers);
  });
});