|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import assert from 'node:assert/strict'; |
| 21 | +import { describe, test } from 'node:test'; |
| 22 | +import { |
| 23 | + redactReversibleStreamingSuffix, |
| 24 | + redactSecrets, |
| 25 | + redactStableStreamingSuffix, |
| 26 | +} from '../display-redaction.js'; |
| 27 | + |
| 28 | +const USERINFO_CASES: Array<[string, string]> = [ |
| 29 | + [ |
| 30 | + 'https://myuser:glpat-AbCdEf12345XyZ@gitlab.com/team/repo.git', |
| 31 | + 'https://<redacted>@gitlab.com/team/repo.git', |
| 32 | + ], |
| 33 | + [ |
| 34 | + 'https://alice:hunter2@internal.example.com/repo.git', |
| 35 | + 'https://<redacted>@internal.example.com/repo.git', |
| 36 | + ], |
| 37 | + [ |
| 38 | + 'https://alice:ATBBxyz123abc456@bitbucket.org/team/repo.git', |
| 39 | + 'https://<redacted>@bitbucket.org/team/repo.git', |
| 40 | + ], |
| 41 | + [ |
| 42 | + 'fatal: unable to access https://deploy:s3cretP@ss@git.corp.example/x.git/: 403', |
| 43 | + 'fatal: unable to access https://<redacted>@git.corp.example/x.git/: 403', |
| 44 | + ], |
| 45 | + [ |
| 46 | + 'https://user@host.example/team/repo.git', |
| 47 | + 'https://<redacted>@host.example/team/repo.git', |
| 48 | + ], |
| 49 | + [ |
| 50 | + 'origin https://alice:hunter2@internal.example.com/repo.git (fetch)', |
| 51 | + 'origin https://<redacted>@internal.example.com/repo.git (fetch)', |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'see https://alice:hunter2@internal.example.com/repo.git.', |
| 55 | + 'see https://<redacted>@internal.example.com/repo.git.', |
| 56 | + ], |
| 57 | + [ |
| 58 | + 'clone (https://alice:hunter2@internal.example.com/repo.git)', |
| 59 | + 'clone (https://<redacted>@internal.example.com/repo.git)', |
| 60 | + ], |
| 61 | +]; |
| 62 | + |
| 63 | +describe('display redactSecrets', () => { |
| 64 | + test('masks URL userinfo credentials without swallowing host or path', () => { |
| 65 | + for (const [input, expected] of USERINFO_CASES) { |
| 66 | + assert.equal(redactSecrets(input), expected); |
| 67 | + } |
| 68 | + assert.equal( |
| 69 | + redactSecrets('https://api.example.com/v1?token=abc123'), |
| 70 | + 'https://api.example.com/v1?token=<redacted>', |
| 71 | + ); |
| 72 | + assert.equal( |
| 73 | + redactSecrets('https://ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@github.com/o/r.git'), |
| 74 | + 'https://<redacted>@github.com/o/r.git', |
| 75 | + ); |
| 76 | + assert.equal( |
| 77 | + redactSecrets('https://alice:hunter2@api.example.com/v1?token=abc123'), |
| 78 | + 'https://<redacted>@api.example.com/v1?token=<redacted>', |
| 79 | + ); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('display streaming suffix redactors', () => { |
| 84 | + test('keeps a stable userinfo suffix compacted until the authority ends', () => { |
| 85 | + const suffix = redactStableStreamingSuffix( |
| 86 | + 'fatal: unable to access https://deploy:s3cretP@ss@', |
| 87 | + ); |
| 88 | + assert.ok(suffix); |
| 89 | + assert.equal(suffix.text, 'fatal: unable to access https://<redacted>@'); |
| 90 | + assert.equal(suffix.settledPrefixText, 'fatal: unable to access '); |
| 91 | + assert.equal(suffix.compactedSuffix, 'https://deploy:s3cretP@ss@'); |
| 92 | + assert.equal(suffix.terminator.test('/'), true); |
| 93 | + assert.equal(suffix.terminator.test('?'), true); |
| 94 | + assert.equal( |
| 95 | + redactSecrets(suffix.settledPrefixText + suffix.compactedSuffix), |
| 96 | + suffix.text, |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + test('does not treat a completed userinfo URL as a streaming suffix', () => { |
| 101 | + for (const [input] of USERINFO_CASES) { |
| 102 | + const suffix = redactStableStreamingSuffix(input); |
| 103 | + assert.equal(suffix, undefined, input); |
| 104 | + assert.equal(redactReversibleStreamingSuffix(input), undefined, input); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + test('still shortens a reversible provider token that reaches end-of-input', () => { |
| 109 | + const token = `ghp_${'A'.repeat(200)}`; |
| 110 | + const reversible = redactReversibleStreamingSuffix(token); |
| 111 | + assert.ok(reversible); |
| 112 | + assert.equal(redactSecrets(reversible.compactedInput), redactSecrets(token)); |
| 113 | + assert.equal(reversible.compactedToken.length < token.length, true); |
| 114 | + }); |
| 115 | +}); |
0 commit comments