Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added regex based input validation when creating a new sketch #2049

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/cli/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
"context"
"os"
"strings"

"regexp"
"fmt"
"github.com/arduino/arduino-cli/arduino/globals"
sk "github.com/arduino/arduino-cli/commands/sketch"
"github.com/arduino/arduino-cli/internal/cli/feedback"
Expand All @@ -38,7 +39,14 @@ func initNewCommand() *cobra.Command {
Long: tr("Create a new Sketch"),
Example: " " + os.Args[0] + " sketch new MultiBlinker",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { runNewCommand(args, overwrite) },
Run: func(cmd *cobra.Command, args []string) {
re := regexp.MustCompile("^/[0-9a-zA-Z][0-9a-zA-Z_.-]{0,62}$")
if !re.MatchString(args[0]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are testing the entire path here. The specification must be applied only to the sketch folder name.

This is the reason the unit tests are failing. Even though they have a valid sketch folder name, the path starts with /, which is not allowed by your regex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick reply, I'm working on those changes now.

fmt.Println("Sketch names must start with a letter or number, followed by letters, numbers, dashes, dots and underscores. Maximum length is 63 characters.")
return
}
runNewCommand(args, overwrite)
},
}

newCommand.Flags().BoolVarP(&overwrite, "overwrite", "f", false, tr("Overwrites an existing .ino sketch."))
Expand Down