| 1 | import { createHash } from 'node:crypto'; |
| 2 | import { readFileSync, writeFileSync } from 'node:fs'; |
| 3 | import { dirname, join, resolve } from 'node:path'; |
| 4 | import { fileURLToPath } from 'node:url'; |
| 5 | |
| 6 | const digest = value => createHash('sha256').update(value).digest('hex'); |
| 7 | const upstreamFixes = [ |
| 8 | 'https://github.com/isaacs/minimatch/commit/77a0f5c4b80bdc641c3c930a1d9edf5ad2db57f8', |
| 9 | 'https://github.com/isaacs/node-glob/commit/4682e8c9babee1b385202ede36389634e12076d7' |
| 10 | ]; |
| 11 | const reviewed = [ |
| 12 | { |
| 13 | "package": "next", |
| 14 | "version": "16.4.6", |
| 15 | "file": "dist/compiled/@vercel/nft/index.js", |
| 16 | "original": "e436d398b2263fb87a2d8ee3d9dab8db2544c684ba9bc1c0d4ab4e67acbab207", |
| 17 | "patched": "3acafac86a359647efceec23f1a838103911dadd5ab63ee3a5b8afa661d20a37", |
| 18 | "edits": [ |
| 19 | { |
| 20 | "before": "firstPhasePreProcess(e){let t=false;", |
| 21 | "after": "firstPhasePreProcess(e){const{braceExpandMax=100_000}=this.options;let t=false;", |
| 22 | "count": 1 |
| 23 | }, |
| 24 | { |
| 25 | "before": "if(!c||c===\".\"||c===\"..\"||!u||u===\".\"||u===\"..\"){continue}", |
| 26 | "after": "if(!c||c===\".\"||c===\"..\"||!u||u===\".\"||u===\"..\"||e.length>=braceExpandMax){continue}", |
| 27 | "count": 1 |
| 28 | }, |
| 29 | { |
| 30 | "before": "secondPhasePreProcess(e){for(let t=0;", |
| 31 | "after": "secondPhasePreProcess(e){if(e.length>1000)return e;for(let t=0;", |
| 32 | "count": 1 |
| 33 | }, |
| 34 | { |
| 35 | "before": "walkCB(e,t,s){if(this.signal?.aborted)s();", |
| 36 | "after": "walkCB(e,t,s){if(this.signal?.aborted)return s();", |
| 37 | "count": 1 |
| 38 | }, |
| 39 | { |
| 40 | "before": "walkCB2(e,t,s,r){if(this.#p(e))return r();if(this.signal?.aborted)r();", |
| 41 | "after": "walkCB2(e,t,s,r){if(this.#p(e))return r();if(this.signal?.aborted)return r();", |
| 42 | "count": 1 |
| 43 | }, |
| 44 | { |
| 45 | "before": "walkCBSync(e,t,s){if(this.signal?.aborted)s();", |
| 46 | "after": "walkCBSync(e,t,s){if(this.signal?.aborted)return s();", |
| 47 | "count": 1 |
| 48 | }, |
| 49 | { |
| 50 | "before": "walkCB2Sync(e,t,s,r){if(this.#p(e))return r();if(this.signal?.aborted)r();", |
| 51 | "after": "walkCB2Sync(e,t,s,r){if(this.#p(e))return r();if(this.signal?.aborted)return r();", |
| 52 | "count": 1 |
| 53 | }, |
| 54 | { |
| 55 | "before": "onResume(e){if(this.signal?.aborted)return;if(!this.paused)", |
| 56 | "after": "onResume(e){if(!this.paused)", |
| 57 | "count": 1 |
| 58 | } |
| 59 | ] |
| 60 | }, |
| 61 | { |
| 62 | "package": "cf", |
| 63 | "version": "1.0.0-beta.12", |
| 64 | "file": "dist/autoconfig-PfnbLuEO.mjs", |
| 65 | "original": "d0d55fbdc213153837647f7bf4bd03709d36b68b2f9ad8399800fafc0ff02eef", |
| 66 | "patched": "daf62fbe10ebbd56171a9a51905b35f2f402312a27bc164699cbb6582eb57cc9", |
| 67 | "edits": [ |
| 68 | { |
| 69 | "before": "firstPhasePreProcess(e){let t=!1;", |
| 70 | "after": "firstPhasePreProcess(e){const{braceExpandMax=100_000}=this.options;let t=!1;", |
| 71 | "count": 1 |
| 72 | }, |
| 73 | { |
| 74 | "before": "if(a!==`..`||!o||o===`.`||o===`..`||!s||s===`.`||s===`..`)continue;", |
| 75 | "after": "if(a!==`..`||!o||o===`.`||o===`..`||!s||s===`.`||s===`..`||e.length>=braceExpandMax)continue;", |
| 76 | "count": 1 |
| 77 | }, |
| 78 | { |
| 79 | "before": "secondPhasePreProcess(e){for(let t=0;t<e.length-1;t++)", |
| 80 | "after": "secondPhasePreProcess(e){if(e.length>1000)return e;for(let t=0;t<e.length-1;t++)", |
| 81 | "count": 1 |
| 82 | } |
| 83 | ] |
| 84 | }, |
| 85 | { |
| 86 | "package": "cf", |
| 87 | "version": "1.0.0-beta.12", |
| 88 | "file": "dist/d1-MvAQMvvo.mjs", |
| 89 | "original": "0342fd94228a09739c49fe12234c37f9b69ef9b2e0181c7f2831fc9b675eb366", |
| 90 | "patched": "c73b1da5735ab719be0dc90b7998559815797c9f4b1a6c1c77efc7723088f6eb", |
| 91 | "edits": [ |
| 92 | { |
| 93 | "before": "firstPhasePreProcess(e){let t=!1;", |
| 94 | "after": "firstPhasePreProcess(e){const{braceExpandMax=100_000}=this.options;let t=!1;", |
| 95 | "count": 1 |
| 96 | }, |
| 97 | { |
| 98 | "before": "if(a!==`..`||!o||o===`.`||o===`..`||!s||s===`.`||s===`..`)continue;", |
| 99 | "after": "if(a!==`..`||!o||o===`.`||o===`..`||!s||s===`.`||s===`..`||e.length>=braceExpandMax)continue;", |
| 100 | "count": 1 |
| 101 | }, |
| 102 | { |
| 103 | "before": "secondPhasePreProcess(e){for(let t=0;t<e.length-1;t++)", |
| 104 | "after": "secondPhasePreProcess(e){if(e.length>1000)return e;for(let t=0;t<e.length-1;t++)", |
| 105 | "count": 1 |
| 106 | } |
| 107 | ] |
| 108 | }, |
| 109 | { |
| 110 | "package": "wrangler", |
| 111 | "version": "4.161.0", |
| 112 | "file": "wrangler-dist/cli.js", |
| 113 | "original": "26835ab3b89181c37f97c99330ed17bbc7ec4d3ad3014175f6ffb2118c998f86", |
| 114 | "patched": "9a9fea2dcbdfba59d67006d55e413071ca53a22467782a67256eabdf2deb341b", |
| 115 | "edits": [ |
| 116 | { |
| 117 | "before": "firstPhasePreProcess(globParts) {\n let didSomething = false;", |
| 118 | "after": "firstPhasePreProcess(globParts) {\n const { braceExpandMax = 100_000 } = this.options;\n let didSomething = false;", |
| 119 | "count": 1 |
| 120 | }, |
| 121 | { |
| 122 | "before": "if (!p8 || p8 === \".\" || p8 === \"..\" || !p22 || p22 === \".\" || p22 === \"..\") {", |
| 123 | "after": "if (!p8 || p8 === \".\" || p8 === \"..\" || !p22 || p22 === \".\" || p22 === \"..\" || globParts.length >= braceExpandMax) {", |
| 124 | "count": 1 |
| 125 | }, |
| 126 | { |
| 127 | "before": "secondPhasePreProcess(globParts) {\n for (let i8 = 0; i8 < globParts.length - 1; i8++) {", |
| 128 | "after": "secondPhasePreProcess(globParts) {\n if (globParts.length > 1000) return globParts;\n for (let i8 = 0; i8 < globParts.length - 1; i8++) {", |
| 129 | "count": 1 |
| 130 | } |
| 131 | ] |
| 132 | } |
| 133 | ]; |
| 134 | |
| 135 | export function patchBundledGlobs({ root = resolve(dirname(fileURLToPath(import.meta.url)), '..'), checkOnly = false } = {}) { |
| 136 | const lock = JSON.parse(readFileSync(join(root, 'package-lock.json'), 'utf8')); |
| 137 | const pending = []; |
| 138 | const copies = reviewed.map(entry => { |
| 139 | const path = 'node_modules/' + entry.package; |
| 140 | const pkg = JSON.parse(readFileSync(join(root, path, 'package.json'), 'utf8')); |
| 141 | if (pkg.name !== entry.package || pkg.version !== entry.version || lock.packages?.[path]?.version !== pkg.version) throw new Error('Unreviewed bundled glob version or lock mismatch: ' + path); |
| 142 | const target = join(root, path, entry.file); |
| 143 | const source = readFileSync(target, 'utf8'); |
| 144 | const hash = digest(source); |
| 145 | if (hash !== entry.patched) { |
| 146 | if (checkOnly || hash !== entry.original) throw new Error('Unreviewed or unpatched glob bundle: ' + target); |
| 147 | let patched = source; |
| 148 | for (const edit of entry.edits) { |
| 149 | if (patched.split(edit.before).length - 1 !== edit.count) throw new Error('Bundled glob patch anchor changed: ' + target); |
| 150 | patched = patched.split(edit.before).join(edit.after); |
| 151 | } |
| 152 | if (digest(patched) !== entry.patched) throw new Error('Bundled glob patch digest mismatch: ' + target); |
| 153 | pending.push({ path: target, source: patched }); |
| 154 | } |
| 155 | return { package: entry.package, version: pkg.version, file: path + '/' + entry.file, sha256: entry.patched }; |
| 156 | }); |
| 157 | if (!checkOnly) for (const file of pending) writeFileSync(file.path, file.source); |
| 158 | return { upstreamFixes, copies }; |
| 159 | } |
| 160 | if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 161 | console.log(JSON.stringify({ mitigation: 'bounded bundled preprocessing and cancelled traversal', ...patchBundledGlobs() })); |
| 162 | } |
| 163 |