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
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 26 additions & 3 deletions v2/pkg/git/git.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package git

import (
"html/template"
"encoding/json"
"fmt"
"runtime"
"strings"

Expand Down Expand Up @@ -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 {
Expand Down
49 changes: 49 additions & 0 deletions v2/pkg/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package git

import (
"encoding/json"
"fmt"
"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)
var js json.RawMessage
jsonVal := fmt.Sprintf(`{"name": "%s"}`, got)
err = json.Unmarshal([]byte(jsonVal), &js)
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)
}
})
}
}
1 change: 1 addition & 0 deletions website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading