Skip to content

Commit 63ce0fc

Browse files
committed
add build constraints to limit syscalls to linux
1 parent 261ef5b commit 63ce0fc

File tree

6 files changed

+99
-63
lines changed

6 files changed

+99
-63
lines changed

internal/login/login.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"net/url"
1515
"os/exec"
1616
"runtime"
17-
"strings"
18-
"syscall"
1917
"time"
2018

2119
"github.com/spf13/viper"
@@ -231,32 +229,12 @@ func generateState() (string, error) {
231229
return base64.URLEncoding.EncodeToString(b), nil
232230
}
233231

234-
// check for Windows Subsystem for Linux
235-
func isWsl(sc util.Syscall) bool {
236-
// the common factor between WSL distros is the Microsoft-specific kernel version, so we check for that
237-
// SUSE, WSLv1: 4.4.0-19041-Microsoft
238-
// Ubuntu, WSLv2: 4.19.128-microsoft-standard
239-
const wslIdentifier = "microsoft"
240-
var uname syscall.Utsname
241-
if err := sc.Uname(&uname); err == nil {
242-
var kernel []byte
243-
for _, b := range uname.Release {
244-
if b == 0 {
245-
break
246-
}
247-
kernel = append(kernel, byte(b))
248-
}
249-
return strings.Contains(strings.ToLower(string(kernel)), wslIdentifier)
250-
}
251-
return false
252-
}
253-
254232
func openBrowser(url string) error {
255233
const rundllParameters = "url.dll,FileProtocolHandler"
256234
var err error
257235
switch runtime.GOOS {
258236
case "linux":
259-
if isWsl(util.DefaultSyscall) {
237+
if util.IsWsl() {
260238
err = exec.Command("rundll32.exe", rundllParameters, url).Start()
261239
} else {
262240
err = exec.Command("xdg-open", url).Start()

internal/login/login_test.go

-40
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ package login
44

55
import (
66
"encoding/json"
7-
"errors"
87
"fmt"
98
"net/http"
109
"net/http/httptest"
11-
"syscall"
1210
"testing"
1311
"time"
1412

1513
"github.com/spf13/viper"
16-
"github.com/stretchr/testify/assert"
1714
"github.com/twitchdev/twitch-cli/internal/util"
1815
)
1916

@@ -160,40 +157,3 @@ func TestUserAuthServer(t *testing.T) {
160157
a.Equal(state, ur.State, "State mismatch")
161158
a.Equal(code, ur.Code, "Code mismatch")
162159
}
163-
164-
func TestIsWsl(t *testing.T) {
165-
a := assert.New(t)
166-
167-
var (
168-
// syscall.Utsname.Release value on various systems
169-
170-
// Ubuntu 20.04 on WSL2 on Windows 10 x64 20H2
171-
ubuntu20Wsl2 = [65]int8{52, 46, 49, 57, 46, 49, 50, 56, 45, 109, 105, 99, 114, 111, 115, 111, 102, 116, 45, 115, 116, 97, 110, 100, 97, 114, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
172-
173-
// Arch Linux on baremetal on 2021-04-02
174-
archReal = [65]int8{53, 46, 49, 49, 46, 49, 49, 45, 97, 114, 99, 104, 49, 45, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
175-
)
176-
177-
result := isWsl(util.Syscall{
178-
Uname: func(buf *syscall.Utsname) (err error) {
179-
buf.Release = ubuntu20Wsl2
180-
return nil
181-
},
182-
})
183-
a.True(result)
184-
185-
result = isWsl(util.Syscall{
186-
Uname: func(buf *syscall.Utsname) (err error) {
187-
buf.Release = archReal
188-
return nil
189-
},
190-
})
191-
a.False(result)
192-
193-
result = isWsl(util.Syscall{
194-
Uname: func(buf *syscall.Utsname) (err error) {
195-
return errors.New("mocked error")
196-
},
197-
})
198-
a.False(result)
199-
}

internal/util/syscall.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build linux
4+
15
package util
26

37
import "syscall"

internal/util/wsl.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build linux
4+
5+
package util
6+
7+
import (
8+
"strings"
9+
"syscall"
10+
)
11+
12+
func IsWsl() bool {
13+
return isWsl(DefaultSyscall)
14+
}
15+
16+
// check for Windows Subsystem for Linux
17+
func isWsl(sc Syscall) bool {
18+
// the common factor between WSL distros is the Microsoft-specific kernel version, so we check for that
19+
// SUSE, WSLv1: 4.4.0-19041-Microsoft
20+
// Ubuntu, WSLv2: 4.19.128-microsoft-standard
21+
const wslIdentifier = "microsoft"
22+
var uname syscall.Utsname
23+
if err := sc.Uname(&uname); err == nil {
24+
var kernel []byte
25+
for _, b := range uname.Release {
26+
if b == 0 {
27+
break
28+
}
29+
kernel = append(kernel, byte(b))
30+
}
31+
return strings.Contains(strings.ToLower(string(kernel)), wslIdentifier)
32+
}
33+
return false
34+
}

internal/util/wsl_default.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build !linux
4+
5+
package util
6+
7+
// non-linux platforms cannot be WSL
8+
func IsWsl() bool {
9+
return false
10+
}

internal/util/wsl_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build linux
4+
5+
package util
6+
7+
import (
8+
"errors"
9+
"syscall"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestIsWsl(t *testing.T) {
16+
a := assert.New(t)
17+
18+
var (
19+
// syscall.Utsname.Release value on various systems
20+
21+
// Ubuntu 20.04 on WSL2 on Windows 10 x64 20H2
22+
ubuntu20Wsl2 = [65]int8{52, 46, 49, 57, 46, 49, 50, 56, 45, 109, 105, 99, 114, 111, 115, 111, 102, 116, 45, 115, 116, 97, 110, 100, 97, 114, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
23+
24+
// Arch Linux on baremetal on 2021-04-02
25+
archReal = [65]int8{53, 46, 49, 49, 46, 49, 49, 45, 97, 114, 99, 104, 49, 45, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
26+
)
27+
28+
result := isWsl(Syscall{
29+
Uname: func(buf *syscall.Utsname) (err error) {
30+
buf.Release = ubuntu20Wsl2
31+
return nil
32+
},
33+
})
34+
a.True(result)
35+
36+
result = isWsl(Syscall{
37+
Uname: func(buf *syscall.Utsname) (err error) {
38+
buf.Release = archReal
39+
return nil
40+
},
41+
})
42+
a.False(result)
43+
44+
result = isWsl(Syscall{
45+
Uname: func(buf *syscall.Utsname) (err error) {
46+
return errors.New("mocked error")
47+
},
48+
})
49+
a.False(result)
50+
}

0 commit comments

Comments
 (0)