From 5b091db26d9255fe587fb0f3c4254b1c584de05e Mon Sep 17 00:00:00 2001 From: taisei <46064673+taisei-86@users.noreply.github.com> Date: Mon, 29 Jul 2024 20:34:19 +0900 Subject: [PATCH] Fixed an error that occurred when an author name contains a string that is not suitable for JSON (#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 --- v2/pkg/git/git.go | 29 +++++++++++++++++++--- v2/pkg/git/git_test.go | 44 +++++++++++++++++++++++++++++++++ website/src/pages/changelog.mdx | 1 + 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 v2/pkg/git/git_test.go diff --git a/v2/pkg/git/git.go b/v2/pkg/git/git.go index 319c5672b75..a0ac68ca95e 100644 --- a/v2/pkg/git/git.go +++ b/v2/pkg/git/git.go @@ -1,7 +1,8 @@ package git import ( - "html/template" + "encoding/json" + "fmt" "runtime" "strings" @@ -30,9 +31,31 @@ func Email() (string, error) { // Name tries to retrieve the func Name() (string, error) { + errMsg := "failed to retrieve git user name: %w" stdout, _, err := shell.RunCommand(".", gitcommand(), "config", "user.name") - name := template.JSEscapeString(strings.TrimSpace(stdout)) - return name, err + if err != nil { + return "", fmt.Errorf(errMsg, err) + } + name := strings.TrimSpace(stdout) + return EscapeName(name) +} + +func EscapeName(str string) (string, error) { + b, err := json.Marshal(str) + if err != nil { + return "", err + } + // Remove the surrounding quotes + escaped := string(b[1 : len(b)-1]) + + // Check if username is JSON compliant + var js json.RawMessage + jsonVal := fmt.Sprintf(`{"name": "%s"}`, escaped) + err = json.Unmarshal([]byte(jsonVal), &js) + if err != nil { + return "", fmt.Errorf("failed to retrieve git user name: %w", err) + } + return escaped, nil } func InitRepo(projectDir string) error { diff --git a/v2/pkg/git/git_test.go b/v2/pkg/git/git_test.go new file mode 100644 index 00000000000..238008ec3cc --- /dev/null +++ b/v2/pkg/git/git_test.go @@ -0,0 +1,44 @@ +package git + +import ( + "testing" +) + +func TestEscapeName1(t *testing.T) { + type args struct { + str string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "Escape Apostrophe", + args: args{ + str: `John O'Keefe`, + }, + want: `John O'Keefe`, + }, + { + name: "Escape backslash", + args: args{ + str: `MYDOMAIN\USER`, + }, + want: `MYDOMAIN\\USER`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := EscapeName(tt.args.str) + if (err != nil) != tt.wantErr { + t.Errorf("EscapeName() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("EscapeName() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index e0474c1f1cf..db40b108d1b 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed +- Fixed an error that occurred when an author name contains a string that is not suitable for JSON. Fixed by @taiseiotsuka in [PR](https://github.com/wailsapp/wails/pull/3638) - Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200) - Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi + Fixed file drop events on Windows in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi