Welcome to mirror list, hosted at ThFree Co, Russian Federation.

secret_detection_spec.js « utils « lib « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b97827208d64b25b0c96a25d18c706c16a4d5e96 (plain)
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
import { containsSensitiveToken, confirmSensitiveAction, i18n } from '~/lib/utils/secret_detection';
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';

jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal');

const mockConfirmAction = ({ confirmed }) => {
  confirmAction.mockResolvedValueOnce(confirmed);
};

describe('containsSensitiveToken', () => {
  describe('when message does not contain sensitive tokens', () => {
    const nonSensitiveMessages = [
      'This is a normal message',
      '1234567890',
      '!@#$%^&*()_+',
      'https://example.com',
      'Some tokens are prefixed with glpat- or glcbt-, for example.',
      'glpat-FAKE',
    ];

    it.each(nonSensitiveMessages)('returns false for message: %s', (message) => {
      expect(containsSensitiveToken(message)).toBe(false);
    });
  });

  describe('when message contains sensitive tokens', () => {
    const sensitiveMessages = [
      'token: glpat-cgyKc1k_AsnEpmP-5fRL',
      'token: GlPat-abcdefghijklmnopqrstuvwxyz',
      'token: feed_token=ABCDEFGHIJKLMNOPQRSTUVWXYZ',
      'token: feed_token=glft-ABCDEFGHIJKLMNOPQRSTUVWXYZ',
      'token: feed_token=glft-a8cc74ccb0de004d09a968705ba49099229b288b3de43f26c473a9d8d7fb7693-1234',
      'token: gloas-a8cc74ccb0de004d09a968705ba49099229b288b3de43f26c473a9d8d7fb7693',
      'https://example.com/feed?feed_token=123456789_abcdefghij',
      'glpat-1234567890 and feed_token=ABCDEFGHIJKLMNOPQRSTUVWXYZ',
      'token: gldt-cgyKc1k_AsnEpmP-5fRL',
      'curl "https://gitlab.example.com/api/v4/groups/33/scim/identities" --header "PRIVATE-TOKEN: glsoat-cgyKc1k_AsnEpmP-5fRL',
      'CI_JOB_TOKEN=glcbt-FFFF_cgyKc1k_AsnEpmP-5fRL',
      'Use this secret job token: glcbt-1_cgyKc1k_AsnEpmP-5fRL',
    ];

    it.each(sensitiveMessages)('returns true for message: %s', (message) => {
      expect(containsSensitiveToken(message)).toBe(true);
    });
  });

  describe('when custom pat prefix is set', () => {
    beforeEach(() => {
      gon.pat_prefix = 'specpat-';
    });

    const sensitiveMessages = [
      'token: specpat-mGYFaXBmNLvLmrEb7xdf',
      'token: feed_token=ABCDEFGHIJKLMNOPQRSTUVWXYZ',
      'https://example.com/feed?feed_token=123456789_abcdefghij',
      'glpat-1234567890 and feed_token=ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    ];

    it.each(sensitiveMessages)('returns true for message: %s', (message) => {
      expect(containsSensitiveToken(message)).toBe(true);
    });
  });
});

describe('confirmSensitiveAction', () => {
  afterEach(() => {
    confirmAction.mockReset();
  });

  it('should call confirmAction with correct parameters', async () => {
    const prompt = 'Are you sure you want to delete this item?';
    const expectedParams = {
      primaryBtnVariant: 'danger',
      primaryBtnText: i18n.primaryBtnText,
    };
    await confirmSensitiveAction(prompt);

    expect(confirmAction).toHaveBeenCalledWith(prompt, expectedParams);
  });

  it('should return true when confirmed is true', async () => {
    mockConfirmAction({ confirmed: true });

    const result = await confirmSensitiveAction();
    expect(result).toBe(true);
  });

  it('should return false when confirmed is false', async () => {
    mockConfirmAction({ confirmed: false });

    const result = await confirmSensitiveAction();
    expect(result).toBe(false);
  });
});