Skip to content

Commit

Permalink
fix(security): sanitize inputs for subprocess commands and file paths
Browse files Browse the repository at this point in the history
- Use filepath.Clean to sanitize file paths before using them in
subprocess commands and file operations.- Implement validation functions
to ensure that dependencies and file paths are valid and within expected
constraints.- Mitigate CWE-78 and CWE-22 vulnerabilities identified by
static code analysis.Addresses the following issues:- CWE-78: Improper
Neutralization of Special Elements used in an OS Command- CWE-22:
Improper Limitation of a Pathname to a Restricted Directory
  • Loading branch information
urizennnn committed May 29, 2024
1 parent 2646766 commit 66f6206
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/Scripts/JsonScripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func JsonScripts(cwd, ext string) {
if file.IsDir() {
continue
} else if file.Name() == "package.json" {
filePath := filepath.Join(cwd, "package.json")
filePath := filepath.Join(cwd, filepath.Clean("package.json"))
fileContent, err := os.ReadFile(filePath)
if err != nil {
errors.Check_Err(err)
Expand Down Expand Up @@ -58,7 +58,7 @@ func JsonScripts(cwd, ext string) {
os.Exit(1)
}

err = os.WriteFile(filePath, updatedContent, 0644)
err = os.WriteFile(filePath, updatedContent, 0600)
if err != nil {
errors.Check_Err(err)
os.Exit(1)
Expand Down
11 changes: 6 additions & 5 deletions lib/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/urizennnn/express-cli/errors"
"github.com/urizennnn/express-cli/lib/functions/config"
"io"
"os"
p "path/filepath"
"strings"
)

const listHeight = 14
Expand Down Expand Up @@ -189,7 +189,8 @@ func Skip() config.User {
}
path := home + "/.express-cli/.express.config.json"

contents, err := os.ReadFile(path)
sanitizedPath := p.Clean(path)
contents, err := os.ReadFile(sanitizedPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions lib/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"path/filepath"
"runtime"

"os"
Expand Down Expand Up @@ -44,8 +45,8 @@ func printVersion() {
} else {
file = data + "/version.js"
}

version, err := exec.Command("node", file).Output()
cleaned_File := filepath.Clean(file)
version, err := exec.Command("node", cleaned_File).Output()
errors.Check_Err(err)

fmt.Print("Express CLI is at version " + config.Green + string(version) + config.Green)
Expand Down
5 changes: 3 additions & 2 deletions lib/functions/config/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ func CreateFolderAndWriteConfig(preferences User) {
folderPath := filepath.Join(userProfile, ".express-cli")
filePath := filepath.Join(folderPath, ".express.config.json")

if err = os.MkdirAll(folderPath, 0755); err != nil {
if err = os.MkdirAll(folderPath, 0750); err != nil {
fmt.Printf("\x1b[31;4mError creating folder: %v\x1b[0m\n", err)
return
}

file, err := os.Create(filePath)
sanitizedFilePath := filepath.Clean(filePath)
file, err := os.Create(sanitizedFilePath)
if err != nil {
fmt.Printf("\x1b[31;4mError creating file: %v\x1b[0m\n", err)
return
Expand Down
9 changes: 6 additions & 3 deletions lib/process/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path"
"path/filepath"

"github.com/urizennnn/express-cli/errors"
)
Expand All @@ -19,7 +20,7 @@ type Dependency struct {
Dev []string `json:"dev"`
}

func InstallDependenciesUnix(ext, manager, cwd string) {
func InstallDependencies(ext, manager, cwd string) {
var jointPath string
switch ext {
case "js":
Expand Down Expand Up @@ -57,7 +58,8 @@ func InstallDependenciesUnix(ext, manager, cwd string) {
}

for _, dep := range dependency.Dependencies {
command := exec.Command(manager, "install", dep)
cleaned_Dep := filepath.Clean(dep)
command := exec.Command(manager, "install", cleaned_Dep)
command.Dir = cwd
err = command.Run()
if err != nil {
Expand All @@ -67,7 +69,8 @@ func InstallDependenciesUnix(ext, manager, cwd string) {
}

for _, dev := range dependency.Dev {
command := exec.Command(manager, "install", "--save-dev", dev)
cleaned_Dev := filepath.Clean(dev)
command := exec.Command(manager, "install", "--save-dev", cleaned_Dev)
command.Dir = cwd
err = command.Run()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions lib/process/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func CopyFile(srcPath, destPath string, fsys embed.FS) error {
input, err := fsys.ReadFile(srcPath)
errors.Check_Err(err)

err = os.MkdirAll(path.Dir(destPath), 0755)
err = os.MkdirAll(path.Dir(destPath), 0750)
errors.Check_Err(err)

err = os.WriteFile(destPath, input, 0644)
err = os.WriteFile(destPath, input, 0600)
errors.Check_Err(err)

return nil
Expand Down Expand Up @@ -68,7 +68,7 @@ func CopyFilesToCWD(cwd, name, manager, ext string, ctx context.CancelFunc) erro
fmt.Println("\033[31m" + "folder already exists" + "\033[0m")
os.Exit(1)
}
if err := os.MkdirAll(folderPath, 0755); err != nil {
if err := os.MkdirAll(folderPath, 0750); err != nil {
errors.Check_Err(err)
}
fmt.Printf("This is manager %v", manager)
Expand All @@ -87,7 +87,7 @@ func CopyFilesToCWD(cwd, name, manager, ext string, ctx context.CancelFunc) erro
if err := copyDirRecursive(jointPath, folderPath, TemplateDir, ext); err != nil {
errors.Check_Err(err)
}
InstallDependenciesUnix(ext, manager, folderPath)
InstallDependencies(ext, manager, folderPath)
gitInit(folderPath)
var language string
switch ext {
Expand Down
2 changes: 1 addition & 1 deletion lib/process/win-dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Dependency struct {
Dev []string `json:"dev"`
}

func InstallDependenciesUnix(ext, manager, cwd string) {
func InstallDependencies(ext, manager, cwd string) {
var jointPath string
switch ext {
case "js":
Expand Down

0 comments on commit 66f6206

Please sign in to comment.