-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use @npmcli/redact for log redactions
Closes #7314
- Loading branch information
1 parent
5469614
commit 17d97d2
Showing
16 changed files
with
149 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 npm | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { URL } = require('url') | ||
|
||
const REPLACE = '***' | ||
const TOKEN_REGEX = /\bnpm_[a-zA-Z0-9]{36}\b/g | ||
const GUID_REGEX = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/g | ||
|
||
const redact = (value) => { | ||
if (typeof value !== 'string' || !value) { | ||
return value | ||
} | ||
|
||
let urlValue | ||
try { | ||
urlValue = new URL(value) | ||
} catch { | ||
// If it's not a URL then we can ignore all errors | ||
} | ||
|
||
if (urlValue?.password) { | ||
urlValue.password = REPLACE | ||
value = urlValue.toString() | ||
} | ||
|
||
return value | ||
.replace(TOKEN_REGEX, `npm_${REPLACE}`) | ||
.replace(GUID_REGEX, REPLACE) | ||
} | ||
|
||
// split on \s|= similar to how nopt parses options | ||
const splitAndRedact = (str) => { | ||
// stateful regex, don't move out of this scope | ||
const splitChars = /[\s=]/g | ||
|
||
let match = null | ||
let result = '' | ||
let index = 0 | ||
while (match = splitChars.exec(str)) { | ||
result += redact(str.slice(index, match.index)) + match[0] | ||
index = splitChars.lastIndex | ||
} | ||
|
||
return result + redact(str.slice(index)) | ||
} | ||
|
||
// replaces auth info in an array of arguments or in a strings | ||
const redactLog = (arg) => { | ||
if (typeof arg === 'string') { | ||
return splitAndRedact(arg) | ||
} else if (Array.isArray(arg)) { | ||
return arg.map((a) => typeof a === 'string' ? splitAndRedact(a) : a) | ||
} | ||
|
||
return arg | ||
} | ||
|
||
module.exports = { | ||
redact, | ||
redactLog, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@npmcli/redact", | ||
"version": "1.1.0", | ||
"description": "Redact sensitive npm information from output", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "tap", | ||
"lint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\"", | ||
"postlint": "template-oss-check", | ||
"template-oss-apply": "template-oss-apply --force", | ||
"lintfix": "npm run lint -- --fix", | ||
"snap": "tap", | ||
"posttest": "npm run lint" | ||
}, | ||
"keywords": [], | ||
"author": "GitHub Inc.", | ||
"license": "ISC", | ||
"files": [ | ||
"bin/", | ||
"lib/" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/npm/redact.git" | ||
}, | ||
"templateOSS": { | ||
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", | ||
"version": "4.21.3", | ||
"publish": true | ||
}, | ||
"tap": { | ||
"nyc-arg": [ | ||
"--exclude", | ||
"tap-snapshots/**" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@npmcli/eslint-config": "^4.0.2", | ||
"@npmcli/template-oss": "4.21.3", | ||
"tap": "^16.3.10" | ||
}, | ||
"engines": { | ||
"node": "^16.14.0 || >=18.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.