|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2025 STMicroelectronics GmbH. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | + |
| 17 | +import { expect } from 'chai'; |
| 18 | +import { DefaultLoggerSanitizer } from './logger-sanitizer'; |
| 19 | + |
| 20 | +describe('DefaultLoggerSanitizer', () => { |
| 21 | + let sanitizer: DefaultLoggerSanitizer; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + sanitizer = new DefaultLoggerSanitizer(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('sanitize', () => { |
| 28 | + it('should mask credentials in http URL', () => { |
| 29 | + const message = 'http://username:[email protected]:8080'; |
| 30 | + const sanitized = sanitizer.sanitize(message); |
| 31 | + expect(sanitized).to.equal('http://****:****@proxy.example.com:8080'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should mask credentials in https URL', () => { |
| 35 | + const message = 'https://user:[email protected]:443/path'; |
| 36 | + const sanitized = sanitizer.sanitize(message); |
| 37 | + expect(sanitized).to.equal('https://****:****@secure-proxy.com:443/path'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return URL unchanged if no credentials present', () => { |
| 41 | + const message = 'http://proxy.example.com:8080'; |
| 42 | + const sanitized = sanitizer.sanitize(message); |
| 43 | + expect(sanitized).to.equal('http://proxy.example.com:8080'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return empty string for empty string input', () => { |
| 47 | + const sanitized = sanitizer.sanitize(''); |
| 48 | + expect(sanitized).to.equal(''); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle complex passwords with special characters', () => { |
| 52 | + const message = 'http://user:p%40ss%[email protected]:8080'; |
| 53 | + const sanitized = sanitizer.sanitize(message); |
| 54 | + expect(sanitized).to.equal('http://****:****@proxy.com:8080'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle URL with path and query parameters', () => { |
| 58 | + const message = 'http://user:[email protected]:8080/path?query=value'; |
| 59 | + const sanitized = sanitizer.sanitize(message); |
| 60 | + expect(sanitized).to.equal('http://****:****@proxy.com:8080/path?query=value'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should mask credentials in ftp URL', () => { |
| 64 | + const message = 'ftp://user:[email protected]'; |
| 65 | + const sanitized = sanitizer.sanitize(message); |
| 66 | + expect(sanitized).to.equal('ftp://****:****@ftp.example.com'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should mask credentials in sftp URL', () => { |
| 70 | + const message = 'sftp://user:[email protected]:22/path'; |
| 71 | + const sanitized = sanitizer.sanitize(message); |
| 72 | + expect(sanitized).to.equal('sftp://****:****@sftp.example.com:22/path'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should mask credentials in ssh URL', () => { |
| 76 | + const message = 'ssh://git:[email protected]/repo'; |
| 77 | + const sanitized = sanitizer.sanitize(message); |
| 78 | + expect(sanitized).to.equal('ssh://****:****@github.com/repo'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should mask credentials in ws URL', () => { |
| 82 | + const message = 'ws://user:[email protected]'; |
| 83 | + const sanitized = sanitizer.sanitize(message); |
| 84 | + expect(sanitized).to.equal('ws://****:****@websocket.example.com'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should mask credentials in wss URL', () => { |
| 88 | + const message = 'wss://user:[email protected]'; |
| 89 | + const sanitized = sanitizer.sanitize(message); |
| 90 | + expect(sanitized).to.equal('wss://****:****@secure-websocket.example.com'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should mask credentials in socks proxy URL', () => { |
| 94 | + const message = 'socks://user:[email protected]:1080'; |
| 95 | + const sanitized = sanitizer.sanitize(message); |
| 96 | + expect(sanitized).to.equal('socks://****:****@socks-proxy.com:1080'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should mask credentials in socks4 proxy URL', () => { |
| 100 | + const message = 'socks4://user:[email protected]:1080'; |
| 101 | + const sanitized = sanitizer.sanitize(message); |
| 102 | + expect(sanitized).to.equal('socks4://****:****@socks4-proxy.com:1080'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should mask credentials in socks5 proxy URL', () => { |
| 106 | + const message = 'socks5://user:[email protected]:1080'; |
| 107 | + const sanitized = sanitizer.sanitize(message); |
| 108 | + expect(sanitized).to.equal('socks5://****:****@socks5-proxy.com:1080'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should mask credentials in git URL', () => { |
| 112 | + const message = 'git://user:[email protected]/org/repo.git'; |
| 113 | + const sanitized = sanitizer.sanitize(message); |
| 114 | + expect(sanitized).to.equal('git://****:****@github.com/org/repo.git'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should not mask mailto links (no credentials format)', () => { |
| 118 | + const message = 'mailto:[email protected]'; |
| 119 | + const sanitized = sanitizer.sanitize(message); |
| 120 | + expect(sanitized).to.equal('mailto:[email protected]'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should mask credentials in any protocol with standard URL format', () => { |
| 124 | + const message = 'customprotocol://user:[email protected]'; |
| 125 | + const sanitized = sanitizer.sanitize(message); |
| 126 | + expect(sanitized).to.equal('customprotocol://****:****@custom.server.com'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should be case-insensitive for protocol', () => { |
| 130 | + const message = 'HTTP://user:[email protected]:8080'; |
| 131 | + const sanitized = sanitizer.sanitize(message); |
| 132 | + expect(sanitized).to.equal('HTTP://****:****@proxy.com:8080'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should mask multiple URLs in a single string', () => { |
| 136 | + const message = 'Connecting to http://user1:[email protected] and http://user2:[email protected]'; |
| 137 | + const sanitized = sanitizer.sanitize(message); |
| 138 | + expect(sanitized).to.equal('Connecting to http://****:****@proxy1.com and http://****:****@proxy2.com'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should mask multiple URLs with different protocols', () => { |
| 142 | + const message = 'HTTP: http://u:[email protected], SOCKS: socks5://u:[email protected], Git: git://u:[email protected]'; |
| 143 | + const sanitized = sanitizer.sanitize(message); |
| 144 | + expect(sanitized).to.equal('HTTP: http://****:****@h1.com, SOCKS: socks5://****:****@h2.com, Git: git://****:****@h3.com'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should mask credentials in log messages containing URLs', () => { |
| 148 | + const message = 'Failed to connect to http://admin:[email protected]:8080'; |
| 149 | + const sanitized = sanitizer.sanitize(message); |
| 150 | + expect(sanitized).to.equal('Failed to connect to http://****:****@internal-proxy.com:8080'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should handle error stack traces with URLs', () => { |
| 154 | + const stack = `Error: Connection failed |
| 155 | + at Request.http://user:[email protected]:8080/api |
| 156 | + at processRequest (index.js:10:5)`; |
| 157 | + const sanitized = sanitizer.sanitize(stack); |
| 158 | + expect(sanitized).to.contain('http://****:****@proxy.com:8080'); |
| 159 | + expect(sanitized).not.to.contain('user:pass'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should return message unchanged if no sensitive data', () => { |
| 163 | + const message = 'Normal log message without sensitive data'; |
| 164 | + const sanitized = sanitizer.sanitize(message); |
| 165 | + expect(sanitized).to.equal(message); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments