Skip to content

Commit 1fc6dd7

Browse files
Fix code scanning alert no. 50: Size computation for allocation may overflow (#1088)
Fixes [https://github.com/wavetermdev/waveterm/security/code-scanning/50](https://github.com/wavetermdev/waveterm/security/code-scanning/50) To fix the problem, we need to ensure that the size computation for the allocation does not overflow. This can be achieved by validating the length of `barr` before performing the arithmetic operation. We will set a maximum allowable size for `barr` to ensure that the sum of `oscPrefixLen(oscNum)` and `len(barr)` does not exceed the maximum value for an `int`. 1. Define a maximum allowable size for `barr` (e.g., 64 MB). 2. Check the length of `barr` against this maximum size before performing the allocation. 3. If `barr` exceeds the maximum size, return an error. _Suggested fixes powered by Copilot Autofix. Review carefully before merging._ --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 8dbb1f9 commit 1fc6dd7

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

pkg/blockcontroller/blockcontroller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
383383
go func() {
384384
// handles outputCh -> shellInputCh
385385
for msg := range wshProxy.ToRemoteCh {
386-
encodedMsg := wshutil.EncodeWaveOSCBytes(wshutil.WaveServerOSC, msg)
386+
encodedMsg, err := wshutil.EncodeWaveOSCBytes(wshutil.WaveServerOSC, msg)
387+
if err != nil {
388+
log.Printf("error encoding OSC message: %v\n", err)
389+
}
387390
shellInputCh <- &BlockInputUnion{InputData: encodedMsg}
388391
}
389392
}()

pkg/wshutil/wshrpcio.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ func AdaptMsgChToPty(outputCh chan []byte, oscEsc string, output io.Writer) erro
7979
panic("oscEsc must be 5 characters")
8080
}
8181
for msg := range outputCh {
82-
barr := EncodeWaveOSCBytes(oscEsc, msg)
83-
_, err := output.Write(barr)
82+
barr, err := EncodeWaveOSCBytes(oscEsc, msg)
8483
if err != nil {
85-
return fmt.Errorf("error writing to output: %w", err)
84+
return fmt.Errorf("error encoding osc message (AdaptMsgChToPty): %w", err)
85+
}
86+
if _, err := output.Write(barr); err != nil {
87+
return fmt.Errorf("error writing osc message (AdaptMsgChToPty): %w", err)
8688
}
8789
}
8890
return nil

pkg/wshutil/wshutil.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ func makeOscPrefix(oscNum string) []byte {
6767
return output
6868
}
6969

70-
func EncodeWaveOSCBytes(oscNum string, barr []byte) []byte {
70+
func EncodeWaveOSCBytes(oscNum string, barr []byte) ([]byte, error) {
7171
if len(oscNum) != 5 {
72-
panic("oscNum must be 5 characters")
72+
return nil, fmt.Errorf("oscNum must be 5 characters")
73+
}
74+
const maxSize = 64 * 1024 * 1024 // 64 MB
75+
if len(barr) > maxSize {
76+
return nil, fmt.Errorf("input data too large")
7377
}
7478
hasControlChars := false
7579
for _, b := range barr {
@@ -85,7 +89,7 @@ func EncodeWaveOSCBytes(oscNum string, barr []byte) []byte {
8589
copyOscPrefix(output, oscNum)
8690
copy(output[oscPrefixLen(oscNum):], barr)
8791
output[len(output)-1] = BEL
88-
return output
92+
return output, nil
8993
}
9094

9195
var buf bytes.Buffer
@@ -101,7 +105,7 @@ func EncodeWaveOSCBytes(oscNum string, barr []byte) []byte {
101105
}
102106
}
103107
buf.WriteByte(BEL)
104-
return buf.Bytes()
108+
return buf.Bytes(), nil
105109
}
106110

107111
func EncodeWaveOSCMessageEx(oscNum string, msg *RpcMessage) ([]byte, error) {
@@ -112,7 +116,7 @@ func EncodeWaveOSCMessageEx(oscNum string, msg *RpcMessage) ([]byte, error) {
112116
if err != nil {
113117
return nil, fmt.Errorf("error marshalling message to json: %w", err)
114118
}
115-
return EncodeWaveOSCBytes(oscNum, barr), nil
119+
return EncodeWaveOSCBytes(oscNum, barr)
116120
}
117121

118122
var termModeLock = sync.Mutex{}
@@ -194,7 +198,11 @@ func SetupTerminalRpcClient(serverImpl ServerImpl) (*WshRpc, io.Reader) {
194198
rpcClient := MakeWshRpc(messageCh, outputCh, wshrpc.RpcContext{}, serverImpl)
195199
go func() {
196200
for msg := range outputCh {
197-
barr := EncodeWaveOSCBytes(WaveOSC, msg)
201+
barr, err := EncodeWaveOSCBytes(WaveOSC, msg)
202+
if err != nil {
203+
fmt.Fprintf(os.Stderr, "Error encoding OSC message: %v\n", err)
204+
continue
205+
}
198206
os.Stdout.Write(barr)
199207
}
200208
}()

0 commit comments

Comments
 (0)