|
| 1 | +const fs = require('fs') |
| 2 | +const os = require('os') |
| 3 | +const path = require('path') |
| 4 | +/** |
| 5 | + * List of Heroku machines to remove from the .netrc file |
| 6 | + * as they are no longer used and pose a security risk. |
| 7 | + * |
| 8 | + * Add any additional Heroku machines to this list that you |
| 9 | + * want to remove from the .netrc file. |
| 10 | + */ |
| 11 | +const machinesToRemove = ['api.heroku.com', 'git.heroku.com'] |
| 12 | +/** |
| 13 | + * Removes the unencrypted Heroku entries from the .netrc file |
| 14 | + * This is a mitigation for a critical security vulnerability |
| 15 | + * where unencrypted Heroku API tokens could be leaked |
| 16 | + * if the .netrc file is compromised. This function removes |
| 17 | + * any entries related to Heroku from the .netrc file as Heroku |
| 18 | + * has discontinued it's use. |
| 19 | + * |
| 20 | + * BE ADVISED: a defect exists in the original implementation |
| 21 | + * where orphaned credentials (passwords without machine blocks) |
| 22 | + * are created when attempting to delete machine entries using the |
| 23 | + * netrc-parser library. |
| 24 | + * |
| 25 | + * This implementation corrects that issue by removing orphaned |
| 26 | + * credentials as well. |
| 27 | + * |
| 28 | + * @returns {void} |
| 29 | + */ |
| 30 | +function removeUnencryptedNetrcMachines() { |
| 31 | + try { |
| 32 | + const netrcPath = getNetrcFileLocation() |
| 33 | + |
| 34 | + if (!fs.existsSync(netrcPath)) { |
| 35 | + console.log('.netrc file not found, nothing to clean up') |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + const content = fs.readFileSync(netrcPath, 'utf8') |
| 40 | + const lines = content.split('\n') |
| 41 | + const filteredLines = [] |
| 42 | + let skipLines = false |
| 43 | + |
| 44 | + // Iterate through lines, handling machine blocks and orphaned credentials |
| 45 | + for (const line of lines) { |
| 46 | + const trimmedLine = line.trim().toLowerCase() |
| 47 | + |
| 48 | + // Check if we're starting a Heroku machine block |
| 49 | + if (trimmedLine.startsWith('machine') && |
| 50 | + (machinesToRemove.some(machine => trimmedLine.includes(machine)))) { |
| 51 | + skipLines = true |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + // Check if we're starting a new machine block (non-Heroku) |
| 56 | + if (trimmedLine.startsWith('machine') && !skipLines) { |
| 57 | + skipLines = false |
| 58 | + } |
| 59 | + |
| 60 | + // Check for orphaned Heroku passwords (HKRU-) and their associated usernames |
| 61 | + if (line.includes('HKRU-')) { |
| 62 | + // Remove the previous line if it exists (username) |
| 63 | + if (filteredLines.length > 0) { |
| 64 | + filteredLines.pop() |
| 65 | + } |
| 66 | + |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + // Only keep lines if we're not in a Heroku block |
| 71 | + if (!skipLines) { |
| 72 | + filteredLines.push(line) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Remove any trailing empty lines |
| 77 | + while (filteredLines.length > 0 && !filteredLines[filteredLines.length - 1].trim()) { |
| 78 | + filteredLines.pop() |
| 79 | + } |
| 80 | + |
| 81 | + // Add a newline at the end if we have content |
| 82 | + const outputContent = filteredLines.length > 0 ? |
| 83 | + filteredLines.join('\n') + '\n' : |
| 84 | + '' |
| 85 | + |
| 86 | + fs.writeFileSync(netrcPath, outputContent) |
| 87 | + } catch (error) { |
| 88 | + throw new Error(`Error cleaning up .netrc: ${error.message}`) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Finds the absolute path to the .netrc file |
| 94 | + * on disk based on the operating system. This |
| 95 | + * code was copied directly from `netrc-parser` |
| 96 | + * and optimized for use here. |
| 97 | + * |
| 98 | + * @see [netrc-parser](https://github.com/jdx/node-netrc-parser/blob/master/src/netrc.ts#L177) |
| 99 | + * |
| 100 | + * @returns {string} the file path of the .netrc on disk. |
| 101 | + */ |
| 102 | +function getNetrcFileLocation() { |
| 103 | + let home = '' |
| 104 | + if (os.platform() === 'win32') { |
| 105 | + home = |
| 106 | + process.env.HOME ?? |
| 107 | + (process.env.HOMEDRIVE && process.env.HOMEPATH && path.join(process.env.HOMEDRIVE, process.env.HOMEPATH)) ?? |
| 108 | + process.env.USERPROFILE |
| 109 | + } |
| 110 | + |
| 111 | + if (!home) { |
| 112 | + home = os.homedir() ?? os.tmpdir() |
| 113 | + } |
| 114 | + |
| 115 | + return path.join(home, os.platform() === 'win32' ? '_netrc' : '.netrc') |
| 116 | +} |
| 117 | + |
| 118 | +removeUnencryptedNetrcMachines() |
0 commit comments