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

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

wants to merge 3 commits into from

Conversation

cushonz
Copy link
Contributor

@cushonz cushonz commented Jan 24, 2023

Please check if the PR fulfills these requirements

See how to contribute

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • The PR follows
    our contributing guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • UPGRADING.md has been updated with a migration guide (for breaking changes)

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

Users are allowed inputs like 'sketch new $%^[].cpp'

What is the new behavior?

When 'sketch new' is called the input is checked against a regular expression to verify there are only alphabetic characters.

Does this PR introduce a breaking change, and is titled accordingly?

This code does not introduce any breaking changes.

Other information

Fixes issue #2043.

@cushonz
Copy link
Contributor Author

cushonz commented Jan 24, 2023

I see some test's failed but I'm unsure of why or how to fix, let me know what steps to take and I'll give it my best shot

@per1234 per1234 added topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project labels Jan 24, 2023
@per1234 per1234 linked an issue Jan 24, 2023 that may be closed by this pull request
3 tasks
@@ -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("^[a-zA-Z].")
Copy link
Contributor

Choose a reason for hiding this comment

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

This regular expression is incorrect. See the requirements in the Arduino Sketch Specification:

https://arduino.github.io/arduino-cli/dev/sketch-specification/#sketch-folders-and-files

You can see a correct regular expression (JavaScript-based) here:

https://github.com/arduino/arduino-ide/blob/2cb9d64f307c6b4f8a0d0bd652c6cac2058f090f/arduino-ide-extension/src/common/protocol/sketches-service.ts#L165

Run: func(cmd *cobra.Command, args []string) {
re := regexp.MustCompile("^[a-zA-Z].")
if !re.MatchString(args[0]) {
fmt.Println("Error: Value can only contain alphabetic characters")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fmt.Println("Error: Value can only contain alphabetic characters")
fmt.Println("Error: Value can only contain alphabetic characters")

The error message is incorrect. You can see an appropriate error message here:

https://github.com/arduino/arduino-ide/blob/2cb9d64f307c6b4f8a0d0bd652c6cac2058f090f/arduino-ide-extension/src/common/protocol/sketches-service.ts#L152

Run: func(cmd *cobra.Command, args []string) { runNewCommand(args, overwrite) },
Run: func(cmd *cobra.Command, args []string) {
re := regexp.MustCompile("^[a-zA-Z].")
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.

Copy link
Contributor

@Bikappa Bikappa left a comment

Choose a reason for hiding this comment

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

Please adopt the same validation in the direct gRPC flow, so that cli and gRPC users see the same behavior.

Also mark the pr as a breaking change since some currently working calls will start to fail.

PS: I realized too late that I was working on the same fix, my bad. However, I feel like you could take some inspiration, see #2051

@cushonz cushonz closed this Jan 25, 2023
@cushonz cushonz deleted the fix-invalid-filenames branch January 25, 2023 21:22
@per1234 per1234 added the conclusion: invalid Issue/PR not valid label Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The sketch new command ignores the sketch folder name specification
3 participants