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

Fixed an error that occurred when an author name contains a string that is not suitable for JSON #3638

Merged
merged 6 commits into from
Jul 29, 2024

Conversation

taisei-86
Copy link
Contributor

@taisei-86 taisei-86 commented Jul 26, 2024

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

When running wails dev, an error occurs in json.Unmarshal in project.go.

Reason: If git user.name contains a ', it will be escaped as "\'", making it unsuitable for a json string.

Fixes #3128

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

I ran wails init & go build & wails(binary) dev and it was successful.

  • Windows
  • macOS
  • Linux

Test Configuration

Please paste the output of wails doctor. If you are unable to run this command, please describe your environment in as much detail as possible.



          Wails Doctor



# Wails
Version | v2.9.1

# System
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
| OS           | MacOS                                                                                                                  |
| Version      | 14.5                                                                                                                   |
| ID           | 23F79                                                                                                                  |
| Go Version   | go1.22.0                                                                                                               |
| Platform     | darwin                                                                                                                 |
| Architecture | arm64                                                                                                                  |
| CPU          | Apple M1                                                                                                               |
| GPU          | Chipset Model: Apple M1 Type: GPU Bus: Built-In Total Number of Cores: 8 Vendor: Apple (0x106b) Metal Support: Metal 3 |
| Memory       | 16GB                                                                                                                   |
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌────────────────────────────────────────────────────────────────┐
| Dependency                | Package Name | Status    | Version |
| Xcode command line tools  | N/A          | Installed | 2408    |
| Nodejs                    | N/A          | Installed | 20.11.1 |
| npm                       | N/A          | Installed | 10.2.4  |
| *Xcode                    | N/A          | Available |         |
| *upx                      | N/A          | Available |         |
| *nsis                     | N/A          | Available |         |
└─────────────────── * - Optional Dependency ────────────────────┘

# Diagnosis
Optional package(s) installation details:
  - Xcode: Available at https://apps.apple.com/us/app/xcode/id497799835
  - upx : Available at https://upx.github.io/
  - nsis : More info at https://wails.io/docs/guides/windows-installer/

 SUCCESS  Your system is ready for Wails development!

 ♥   If Wails is useful to you or your company, please consider sponsoring the project:
https://github.com/sponsors/leaanthony

Checklist:

  • I have updated website/src/pages/changelog.mdx with details of this PR
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Improved error handling for retrieving the user's name in the git package.
    • Added validation for username format to ensure compliance with JSON standards.
  • Bug Fixes

    • Enhanced reliability of the function by adding error checks after command execution.
    • Fixed issues related to author names containing unsuitable strings for JSON formatting, improving app robustness.
  • Documentation

    • Updated changelog to clarify recent bug fixes and enhance entry clarity.

…author name contained non-JSON legal characters.
Copy link
Contributor

coderabbitai bot commented Jul 26, 2024

Walkthrough

The recent updates to the Name function in the git package significantly improve error handling and ensure usernames conform with JSON formatting. By implementing immediate error checks and adding validation for usernames, these changes enhance reliability and prevent crashes when dealing with author names that include special characters.

Changes

File Change Summary
v2/pkg/git/git.go Enhanced error handling in the Name function and added validation for JSON-compliant usernames.
v2/pkg/git/git_test.go Introduced tests for the EscapeName function, validating handling of apostrophes and backslashes.
website/src/pages/changelog.mdx Added a changelog entry for a bug fix on author name formatting and consolidated existing entries.

Assessment against linked issues

Objective Addressed Explanation
Handle apostrophes in author names to prevent crashes (#3128)
Ensure proper escaping of special characters in names (#3128)

Poem

🐇 In the code where bunnies play,
A function grew in a clever way.
Error checks and JSON dreams,
Valid names in bright sunbeams.
Hopping forth with data bright,
Code’s a dance, a joyful sight! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@taisei-86 taisei-86 changed the title fix(pkg): Fixed an issue where wails new would throw an error if the author name contained non-JSON legal characters. fix(pkg): Fixed an issue where wails dev would throw an error if the author name contained non-JSON legal characters. Jul 26, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7a40cc5 and ce38bea.

Files selected for processing (1)
  • v2/pkg/git/git.go (2 hunks)
Additional comments not posted (1)
v2/pkg/git/git.go (1)

39-41: Ensure proper escaping of the username.

The template.JSEscapeString function is used to escape the username. This is a good practice to prevent injection attacks.

Comment on lines 36 to 38
if err != nil {
return "", err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Enhance error handling message.

The error handling can be improved by providing a more descriptive error message.

-  return "", err
+  return "", fmt.Errorf("failed to retrieve git user name: %w", err)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if err != nil {
return "", err
}
if err != nil {
return "", fmt.Errorf("failed to retrieve git user name: %w", err)
}

Comment on lines 42 to 46
// Check if username is JSON compliant
var js json.RawMessage
jsonVal := fmt.Sprintf(`{"name": "%s"}`, name)
err = json.Unmarshal([]byte(jsonVal), &js)

Copy link
Contributor

Choose a reason for hiding this comment

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

Improve JSON validation and error handling.

The JSON validation is a good addition to ensure the username is JSON compliant. However, the error handling can be improved by providing a more descriptive error message.

-  return name, err
+  if err != nil {
+    return "", fmt.Errorf("username is not JSON compliant: %w", err)
+  }
+  return name, nil
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Check if username is JSON compliant
var js json.RawMessage
jsonVal := fmt.Sprintf(`{"name": "%s"}`, name)
err = json.Unmarshal([]byte(jsonVal), &js)
// Check if username is JSON compliant
var js json.RawMessage
jsonVal := fmt.Sprintf(`{"name": "%s"}`, name)
err = json.Unmarshal([]byte(jsonVal), &js)
if err != nil {
return "", fmt.Errorf("username is not JSON compliant: %w", err)
}
return name, nil

@taisei-86 taisei-86 changed the title fix(pkg): Fixed an issue where wails dev would throw an error if the author name contained non-JSON legal characters. Fixed an error that occurred when an author name contains a string that is not suitable for JSON. Jul 26, 2024
@taisei-86 taisei-86 changed the title Fixed an error that occurred when an author name contains a string that is not suitable for JSON. Fixed an error that occurred when an author name contains a string that is not suitable for JSON Jul 26, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ce38bea and 80808e6.

Files selected for processing (2)
  • v2/pkg/git/git.go (2 hunks)
  • website/src/pages/changelog.mdx (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • v2/pkg/git/git.go
Additional comments not posted (2)
website/src/pages/changelog.mdx (2)

18-18: LGTM!

The entry clearly describes the bug fix related to author names containing unsuitable strings for JSON formatting.


19-19: LGTM!

The consolidation of the entries regarding file drop events on Windows is clear and follows the changelog format.

Tools
LanguageTool

[grammar] ~19-~19: The operating system from Apple is written “macOS”.
Context: ...b.com//pull/3638) - Fixed MacOS build to use outputfilename from wail...

(MAC_OS)

@taisei-86
Copy link
Contributor Author

[imo]
Personally, I don't think there is any way to attack it, so I don't think the escaping process itself is necessary.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 80808e6 and 6ca8c4f.

Files selected for processing (2)
  • v2/pkg/git/git.go (2 hunks)
  • v2/pkg/git/git_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • v2/pkg/git/git.go
Additional comments not posted (4)
v2/pkg/git/git_test.go (4)

1-7: LGTM!

The package declaration and imports are appropriate and necessary for the test function.


9-18: LGTM!

The test function TestEscapeName1 is declared correctly and uses a table-driven approach for the test cases, which is a good practice.


19-33: Verify the expected output for the apostrophe case.

The test case for escaping an apostrophe expects the output to be John O'Keefe. Ensure this is the correct expected output based on the EscapeName function's implementation.


34-49: LGTM!

The test execution loop correctly runs each test case and validates both the function output and the JSON parsing, ensuring comprehensive testing.

Copy link
Member

@leaanthony leaanthony left a comment

Choose a reason for hiding this comment

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

Nice one @taisei-86!
I added a couple of changes and a test. Glad to finally put this one to bed.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6ca8c4f and c943bed.

Files selected for processing (1)
  • v2/pkg/git/git_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • v2/pkg/git/git_test.go

@leaanthony leaanthony merged commit 5b091db into wailsapp:master Jul 29, 2024
4 of 5 checks passed
Copy link

therealsamf pushed a commit to therealsamf/wails that referenced this pull request Aug 29, 2024
…at is not suitable for JSON (wailsapp#3638)

* fix(pkg): Fixed an issue where wails new would throw an error if the author name contained non-JSON legal characters.

* refactor(pkg): Incorporating coderabbit's suggestions

* docs: write changelog.mdx

* Escape using json package. Add tests.

* Update test.

---------

Co-authored-by: Lea O'Anthony <lea.anthony@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Apostrophe in Autogenerated Name Causes Application Crash
2 participants