Skip to content

Commit 9cd832c

Browse files
committed
Eliminated cmd injection risk from copyToClipboard()
1 parent d1d70c0 commit 9cd832c

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

generate-ip/src/cli.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const pkgName = 'generate-ip',
1111
// Import LIBS
1212
const { ipv4, ipv6, mac } = require(__dirname.match(/src/) ? './generate-ip' : './generate-ip.min'),
1313
fs = require('fs'), path = require('path'),
14-
{ execSync } = require('child_process') // for --version cmd + cross-platform copying
14+
{ execSync, execFileSync } = require('child_process') // for --version cmd + cross-platform copying
1515

1616
// Init UI COLORS
1717
const nc = '\x1b[0m', // no color
@@ -206,11 +206,11 @@ const pkgName = 'generate-ip',
206206

207207
function copyToClipboard(data) {
208208
if (process.platform == 'darwin') // macOS
209-
execSync(`printf "${data}" | pbcopy`)
209+
execFileSync('pbcopy', [], { input: data })
210210
else if (process.platform == 'linux')
211-
execSync(`printf "${data}" | xclip -selection clipboard`)
211+
execFileSync('xclip', ['-selection', 'clipboard'], { input: data })
212212
else if (process.platform == 'win32')
213-
execSync(`Set-Clipboard -Value "${data}"`, { shell: 'powershell' })
213+
execFileSync('powershell', ['Set-Clipboard', '-Value', data])
214214
}
215215

216216
})()

generate-pw/src/cli.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const pkgName = 'generate-pw',
1111
// Import LIBS
1212
const { generatePassword } = require(__dirname.match(/src/) ? './generate-pw' : './generate-pw.min'),
1313
fs = require('fs'), path = require('path'),
14-
{ execSync } = require('child_process') // for --version cmd + cross-platform copying
14+
{ execSync, execFileSync } = require('child_process') // for --version cmd + cross-platform copying
1515

1616
// Init UI COLORS
1717
const nc = '\x1b[0m', // no color
@@ -231,11 +231,11 @@ const pkgName = 'generate-pw',
231231
function copyToClipboard(data) {
232232
data = data.replace(/"/g, '""')
233233
if (process.platform == 'darwin') // macOS
234-
execSync(`printf "${data}" | pbcopy`)
234+
execFileSync('pbcopy', [], { input: data })
235235
else if (process.platform == 'linux')
236-
execSync(`printf "${data}" | xclip -selection clipboard`)
236+
execFileSync('xclip', ['-selection', 'clipboard'], { input: data })
237237
else if (process.platform == 'win32')
238-
execSync(`Set-Clipboard -Value "${data}"`, { shell: 'powershell' })
238+
execFileSync('powershell', ['Set-Clipboard', '-Value', data])
239239
}
240240

241241
})()

geolocate/src/cli.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const pkgName = '@adamlui/geolocate',
1111
// Import LIBS
1212
const geo = require(__dirname.match(/src/) ? './geolocate' : './geolocate.min'),
1313
fs = require('fs'), path = require('path'),
14-
{ execSync } = require('child_process') // for --version cmd + cross-platform copying
14+
{ execSync, execFileSync } = require('child_process') // for --version cmd + cross-platform copying
1515

1616
// Init UI COLORS
1717
const nc = '\x1b[0m', // no color
@@ -197,11 +197,11 @@ const pkgName = '@adamlui/geolocate',
197197
function copyToClipboard(data) {
198198
data = data.replace(/"/g, '""')
199199
if (process.platform == 'darwin') // macOS
200-
execSync(`printf "${data}" | pbcopy`)
200+
execFileSync('pbcopy', [], { input: data })
201201
else if (process.platform == 'linux')
202-
execSync(`printf "${data}" | xclip -selection clipboard`)
202+
execFileSync('xclip', ['-selection', 'clipboard'], { input: data })
203203
else if (process.platform == 'win32')
204-
execSync(`Set-Clipboard -Value "${data}"`, { shell: 'powershell' })
204+
execFileSync('powershell', ['Set-Clipboard', '-Value', data])
205205
}
206206

207207
})()

0 commit comments

Comments
 (0)