Skip to content

Commit 8d900b2

Browse files
committed
Replaced binary names in copyToClipboard() w/ absolute paths for safer execution
1 parent 45dca5e commit 8d900b2

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

generate-ip/src/cli.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,15 @@ const pkgName = 'generate-ip',
205205
function printIfNotQuiet(msg) { if (!config.quietMode) console.info(msg) }
206206

207207
function copyToClipboard(data) {
208-
if (process.platform == 'darwin') // macOS
209-
execFileSync('pbcopy', [], { input: data })
210-
else if (process.platform == 'linux')
211-
execFileSync('xclip', ['-selection', 'clipboard'], { input: data })
212-
else if (process.platform == 'win32')
213-
execFileSync('powershell', ['Set-Clipboard', '-Value', data])
208+
const osConfig = {
209+
darwin: { binPath: '/usr/bin/pbcopy', args: [] },
210+
linux: { binPath: '/usr/bin/xclip', args: ['-selection', 'clipboard'] },
211+
win32: {
212+
binPath: path.join(process.env.SYSTEMROOT, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
213+
args: ['-Command', 'Set-Clipboard -Value $input']
214+
}
215+
}
216+
execFileSync(...Object.values(osConfig[process.platform]), { input: data })
214217
}
215218

216219
})()

generate-pw/src/cli.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,15 @@ const pkgName = 'generate-pw',
230230

231231
function copyToClipboard(data) {
232232
data = data.replace(/"/g, '""')
233-
if (process.platform == 'darwin') // macOS
234-
execFileSync('pbcopy', [], { input: data })
235-
else if (process.platform == 'linux')
236-
execFileSync('xclip', ['-selection', 'clipboard'], { input: data })
237-
else if (process.platform == 'win32')
238-
execFileSync('powershell', ['Set-Clipboard', '-Value', data])
233+
const osConfig = {
234+
darwin: { binPath: '/usr/bin/pbcopy', args: [] },
235+
linux: { binPath: '/usr/bin/xclip', args: ['-selection', 'clipboard'] },
236+
win32: {
237+
binPath: path.join(process.env.SYSTEMROOT, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
238+
args: ['-Command', 'Set-Clipboard -Value $input']
239+
}
240+
}
241+
execFileSync(...Object.values(osConfig[process.platform]), { input: data })
239242
}
240243

241244
})()

geolocate/src/cli.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,15 @@ const pkgName = '@adamlui/geolocate',
196196

197197
function copyToClipboard(data) {
198198
data = data.replace(/"/g, '""')
199-
if (process.platform == 'darwin') // macOS
200-
execFileSync('pbcopy', [], { input: data })
201-
else if (process.platform == 'linux')
202-
execFileSync('xclip', ['-selection', 'clipboard'], { input: data })
203-
else if (process.platform == 'win32')
204-
execFileSync('powershell', ['Set-Clipboard', '-Value', data])
199+
const osConfig = {
200+
darwin: { binPath: '/usr/bin/pbcopy', args: [] },
201+
linux: { binPath: '/usr/bin/xclip', args: ['-selection', 'clipboard'] },
202+
win32: {
203+
binPath: path.join(process.env.SYSTEMROOT, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
204+
args: ['-Command', 'Set-Clipboard -Value $input']
205+
}
206+
}
207+
execFileSync(...Object.values(osConfig[process.platform]), { input: data })
205208
}
206209

207210
})()

0 commit comments

Comments
 (0)