Skip to content

Commit 82873ac

Browse files
committed
fixed another G304 (CWE-22) in tokkenizer
1 parent c96a09c commit 82873ac

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

Diff for: helpers.go

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package taskwrappr
33

44
import (
55
"fmt"
6+
"path/filepath"
67
"unicode"
78
)
89

@@ -71,3 +72,20 @@ func isReservedVariableName(name string) (bool, LiteralType) {
7172
}
7273
return false, LiteralUndefined
7374
}
75+
76+
func sanitizeFilePath(path string) (string, error) {
77+
if path == "" {
78+
return "", fmt.Errorf("empty file path")
79+
}
80+
81+
absPath, err := filepath.Abs(path)
82+
if err != nil {
83+
return "", fmt.Errorf("could not determine absolute path: %w", err)
84+
}
85+
86+
if filepath.Ext(absPath) != ".tw" {
87+
return "", fmt.Errorf("invalid file type: %s", absPath)
88+
}
89+
90+
return path, nil
91+
}

Diff for: tokenizer.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package taskwrappr
44
import (
55
"fmt"
66
"os"
7-
"path/filepath"
87
"strconv"
98
"strings"
109
"unicode"
@@ -26,13 +25,13 @@ func (t *Tokenizer) String() string {
2625
}
2726

2827
func NewTokenizer(filePath string) *Tokenizer {
29-
absPath, err := filepath.Abs(filePath)
30-
if err != nil {
31-
fmt.Println("could not determine absolute path: %w", err)
32-
return nil
33-
}
28+
path, err := sanitizeFilePath(filePath)
29+
if err != nil {
30+
fmt.Println("error sanitizing file path:", err)
31+
return nil
32+
}
3433

35-
source, err := os.ReadFile(absPath)
34+
source, err := os.ReadFile(path)
3635
if err != nil {
3736
fmt.Println("error reading script file:", err)
3837
return nil

0 commit comments

Comments
 (0)