|
1 | 1 | # Secrets |
2 | 2 |
|
3 | | -It is possible to **mask out sensitive data** when passing it to steps. This is important when filling password fields, or sending secure keys to API endpoint. |
| 3 | +It is possible to **mask out sensitive data** when passing it to steps. This is important when filling password fields, or sending secure keys to API endpoint. CodeceptJS provides two approaches for masking sensitive data: |
| 4 | + |
| 5 | +## 1. Using the `secret()` Function |
4 | 6 |
|
5 | 7 | Wrap data in `secret` function to mask sensitive values in output and logs. |
6 | 8 |
|
7 | 9 | For basic string `secret` just wrap a value into a string: |
8 | 10 |
|
9 | 11 | ```js |
10 | | -I.fillField('password', secret('123456')); |
| 12 | +I.fillField('password', secret('123456')) |
11 | 13 | ``` |
12 | 14 |
|
13 | 15 | When executed it will be printed like this: |
14 | 16 |
|
15 | 17 | ``` |
16 | 18 | I fill field "password" "*****" |
17 | 19 | ``` |
| 20 | + |
18 | 21 | **Other Examples** |
| 22 | + |
19 | 23 | ```js |
20 | | -I.fillField('password', secret('123456')); |
21 | | -I.append('password', secret('123456')); |
22 | | -I.type('password', secret('123456')); |
| 24 | +I.fillField('password', secret('123456')) |
| 25 | +I.append('password', secret('123456')) |
| 26 | +I.type('password', secret('123456')) |
23 | 27 | ``` |
24 | 28 |
|
25 | 29 | For an object, which can be a payload to POST request, specify which fields should be masked: |
26 | 30 |
|
27 | 31 | ```js |
28 | | -I.sendPostRequest('/login', secret({ |
29 | | - name: 'davert', |
30 | | - password: '123456' |
31 | | -}, 'password')) |
| 32 | +I.sendPostRequest( |
| 33 | + '/login', |
| 34 | + secret( |
| 35 | + { |
| 36 | + name: 'davert', |
| 37 | + password: '123456', |
| 38 | + }, |
| 39 | + 'password', |
| 40 | + ), |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +The object created from `secret` is as Proxy to the object passed in. When printed password will be replaced with \*\*\*\*. |
| 45 | + |
| 46 | +> ⚠️ Only direct properties of the object can be masked via `secret` |
| 47 | +
|
| 48 | +## 2. Global Sensitive Data Masking |
| 49 | + |
| 50 | +CodeceptJS can automatically mask sensitive data in all output (logs, steps, debug messages, errors) using configurable patterns. This feature uses the `maskSensitiveData` configuration option. |
| 51 | + |
| 52 | +### Basic Usage (Boolean) |
| 53 | + |
| 54 | +Enable basic masking with predefined patterns: |
| 55 | + |
| 56 | +```js |
| 57 | +// codecept.conf.js |
| 58 | +exports.config = { |
| 59 | + // ... other config |
| 60 | + maskSensitiveData: true, |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +This will mask common sensitive data patterns like: |
| 65 | + |
| 66 | +- Authorization headers |
| 67 | +- API keys |
| 68 | +- Passwords |
| 69 | +- Tokens |
| 70 | +- Client secrets |
| 71 | + |
| 72 | +### Advanced Usage (Custom Patterns) |
| 73 | + |
| 74 | +Define your own masking patterns: |
| 75 | + |
| 76 | +```js |
| 77 | +// codecept.conf.js |
| 78 | +exports.config = { |
| 79 | + // ... other config |
| 80 | + maskSensitiveData: { |
| 81 | + enabled: true, |
| 82 | + patterns: [ |
| 83 | + { |
| 84 | + name: 'Email', |
| 85 | + regex: /(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b)/gi, |
| 86 | + mask: '[MASKED_EMAIL]', |
| 87 | + }, |
| 88 | + { |
| 89 | + name: 'Credit Card', |
| 90 | + regex: /\b(?:\d{4}[- ]?){3}\d{4}\b/g, |
| 91 | + mask: '[MASKED_CARD]', |
| 92 | + }, |
| 93 | + { |
| 94 | + name: 'Phone Number', |
| 95 | + regex: /(\+?1[-.\s]?)?\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})/g, |
| 96 | + mask: '[MASKED_PHONE]', |
| 97 | + }, |
| 98 | + { |
| 99 | + name: 'SSN', |
| 100 | + regex: /\b\d{3}-\d{2}-\d{4}\b/g, |
| 101 | + mask: '[MASKED_SSN]', |
| 102 | + }, |
| 103 | + ], |
| 104 | + }, |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Pattern Configuration |
| 109 | + |
| 110 | +Each custom pattern object should have: |
| 111 | + |
| 112 | +- `name`: A descriptive name for the pattern |
| 113 | +- `regex`: A JavaScript regular expression to match the sensitive data |
| 114 | +- `mask`: The replacement string to show instead of the sensitive data |
| 115 | + |
| 116 | +### Examples |
| 117 | + |
| 118 | +With the above configuration: |
| 119 | + |
| 120 | +**Input:** |
| 121 | + |
32 | 122 | ``` |
| 123 | + |
| 124 | +Credit card: 4111 1111 1111 1111 |
| 125 | +Phone: +1-555-123-4567 |
| 126 | +``` |
| 127 | + |
| 128 | +**Output:** |
| 129 | + |
| 130 | +``` |
| 131 | +User email: [MASKED_EMAIL] |
| 132 | +Credit card: [MASKED_CARD] |
| 133 | +Phone: [MASKED_PHONE] |
| 134 | +``` |
| 135 | + |
| 136 | +### Where Masking Applies |
| 137 | + |
| 138 | +Global sensitive data masking is applied to: |
| 139 | + |
| 140 | +- Step descriptions and output |
| 141 | +- Debug messages (`--debug` mode) |
| 142 | +- Log messages (`--verbose` mode) |
| 143 | +- Error messages |
| 144 | +- Success messages |
| 145 | + |
| 146 | +> ⚠️ Direct `console.log()` calls in helper functions are not masked. Use CodeceptJS output functions instead. |
33 | 147 |
|
34 | | -The object created from `secret` is as Proxy to the object passed in. When printed password will be replaced with ****. |
| 148 | +### Combining Both Approaches |
35 | 149 |
|
36 | | -> ⚠️ Only direct properties of the object can be masked via `secret` |
| 150 | +You can use both `secret()` function and global masking together. The `secret()` function is applied first, then global patterns are applied to the remaining output. |
0 commit comments