Skip to content

Commit 57644fc

Browse files
committed
refactor(wacom): replace shell command with direct exec call
1. Replace `doAction(string)` with `doAction(args ...string)` to avoid shell injection risk 2. Refactor all Wacom set methods to pass arguments individually instead of using fmt.Sprintf to build shell commands 3. Remove `exec.Command("/bin/sh", "-c", cmd)` in favor of `exec.Command(cmdXSetWacom, args...)` for safer command execution 4. Sort imports and add strconv/errors usage for integer/string conversions Log: Replace unsafe shell command construction with direct exec.Command calls in wacom module refactor(wacom): 用直接 exec 调用替换 shell 命令拼接 1. 将 `doAction(string)` 重构为 `doAction(args ...string)` 以消除 shell 注入风险 2. 重构所有 Wacom set 方法,改为逐个传参而非使用 fmt.Sprintf 拼接 shell 命令 3. 移除 `exec.Command("/bin/sh", "-c", cmd)` 改用 `exec.Command(cmdXSetWacom, args...)` 执行命令 4. 调整 import 排序,添加 strconv/errors 用于整数/字符串转换 Log: 将 wacom 模块中不安全的 shell 命令拼接替换为直接 exec.Command 调用 Change-Id: Ia4978b3f2920dd8f005dc6f1cff45470d956bb65
1 parent c330ffe commit 57644fc

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

dxinput/wacom.go

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -88,14 +88,13 @@ func (w *Wacom) QueryType() int {
8888
// Set the tablet input area in device coordinates in the form top
8989
// left x/y and bottom right x/y.
9090
func (w *Wacom) SetArea(x1, y1, x2, y2 int) error {
91-
var cmd = fmt.Sprintf("%s set %v %s %v %v %v %v", cmdXSetWacom, w.Id,
92-
cmdKeyArea, x1, y1, x2, y2)
93-
return doAction(cmd)
91+
return doAction("set", w.getIdAsStr(), cmdKeyArea,
92+
strconv.Itoa(x1), strconv.Itoa(y1),
93+
strconv.Itoa(x2), strconv.Itoa(y2))
9494
}
9595

9696
func (w *Wacom) ResetArea() error {
97-
var cmd = fmt.Sprintf("%s set %v %s", cmdXSetWacom, w.Id, cmdKeyResetArea)
98-
return doAction(cmd)
97+
return doAction("set", w.getIdAsStr(), cmdKeyResetArea)
9998
}
10099

101100
func (w *Wacom) getIdAsStr() string {
@@ -130,9 +129,7 @@ func (w *Wacom) SetRotate(value string) error {
130129
return fmt.Errorf("Invalid value: %s", value)
131130
}
132131

133-
var cmd = fmt.Sprintf("%s set %v %s %v", cmdXSetWacom, w.Id,
134-
cmdKeyRotate, value)
135-
return doAction(cmd)
132+
return doAction("set", w.getIdAsStr(), cmdKeyRotate, value)
136133
}
137134

138135
// Button button-number [mapping]
@@ -148,9 +145,8 @@ func (w *Wacom) SetRotate(value string) error {
148145
// series of keystrokes, in this example "press a, press shift,
149146
// press and release b, release shift, release a".
150147
func (w *Wacom) SetButton(btn int, value string) error {
151-
var cmd = fmt.Sprintf("%s set %v %s %v %s", cmdXSetWacom, w.Id,
152-
cmdKeyButton, btn, value)
153-
return doAction(cmd)
148+
return doAction("set", w.getIdAsStr(), cmdKeyButton,
149+
strconv.Itoa(btn), value)
154150
}
155151

156152
// Mode Absolute|Relative
@@ -166,9 +162,7 @@ func (w *Wacom) SetMode(mode string) error {
166162
default:
167163
return fmt.Errorf("Invalid value: %s", mode)
168164
}
169-
var cmd = fmt.Sprintf("%s set %v %s %s", cmdXSetWacom, w.Id,
170-
cmdKeyMode, mode)
171-
return doAction(cmd)
165+
return doAction("set", w.getIdAsStr(), cmdKeyMode, mode)
172166
}
173167

174168
// PressureCurve x1 y1 x2 y2
@@ -187,9 +181,9 @@ func (w *Wacom) SetPressureCurve(x1, y1, x2, y2 int) error {
187181
return fmt.Errorf("Invalid value: %v %v %v %v", x1, y1, x2, y2)
188182
}
189183

190-
var cmd = fmt.Sprintf("%s set %v %s %v %v %v %v", cmdXSetWacom, w.Id,
191-
cmdKeyPressureCurve, x1, y1, x2, y2)
192-
return doAction(cmd)
184+
return doAction("set", w.getIdAsStr(), cmdKeyPressureCurve,
185+
strconv.Itoa(x1), strconv.Itoa(y1),
186+
strconv.Itoa(x2), strconv.Itoa(y2))
193187
}
194188

195189
// Suppress level
@@ -202,9 +196,8 @@ func (w *Wacom) SetSuppress(value int) error {
202196
return fmt.Errorf("Invalid value: %v", value)
203197
}
204198

205-
var cmd = fmt.Sprintf("%s set %v %s %v", cmdXSetWacom, w.Id,
206-
cmdKeySuppress, value)
207-
return doAction(cmd)
199+
return doAction("set", w.getIdAsStr(), cmdKeySuppress,
200+
strconv.Itoa(value))
208201
}
209202

210203
// Threshold level
@@ -219,9 +212,8 @@ func (w *Wacom) SetThreshold(thres int) error {
219212
return fmt.Errorf("Invalid value: %v", thres)
220213
}
221214

222-
var cmd = fmt.Sprintf("%s set %v %s %v", cmdXSetWacom, w.Id,
223-
cmdKeyThreshold, thres)
224-
return doAction(cmd)
215+
return doAction("set", w.getIdAsStr(), cmdKeyThreshold,
216+
strconv.Itoa(thres))
225217
}
226218

227219
// The the window size for incoming input tool raw data points
@@ -231,9 +223,8 @@ func (w *Wacom) SetRawSample(sample uint32) error {
231223
return fmt.Errorf("Invalid raw sample: %v", sample)
232224
}
233225

234-
var cmd = fmt.Sprintf("%s set %v %s %v", cmdXSetWacom, w.Id,
235-
cmdKeyRawSample, sample)
236-
return doAction(cmd)
226+
return doAction("set", w.getIdAsStr(), cmdKeyRawSample,
227+
strconv.FormatUint(uint64(sample), 10))
237228
}
238229

239230
// Mapping PC screen to tablet, such as "VGA1"
@@ -242,14 +233,11 @@ func (w *Wacom) MapToOutput(output string) error {
242233
return nil
243234
}
244235

245-
var cmd = fmt.Sprintf("%s set %v %s %s", cmdXSetWacom, w.Id,
246-
cmdKeyMapToOutput, output)
247-
return doAction(cmd)
236+
return doAction("set", w.getIdAsStr(), cmdKeyMapToOutput, output)
248237
}
249238

250-
func doAction(cmd string) error {
251-
// #nosec G204
252-
out, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
239+
func doAction(args ...string) error {
240+
out, err := exec.Command(cmdXSetWacom, args...).CombinedOutput()
253241
if err != nil {
254242
return errors.New(string(out))
255243
}

0 commit comments

Comments
 (0)