Skip to content

Commit 8bf0ca4

Browse files
author
Wails Documentation Agent
committed
fix(darwin): sanitize CFBundleIdentifier to comply with Apple requirements
CFBundleIdentifier was generated as com.wails.{{.Name}} which could contain spaces and special characters, violating Apple's requirement that bundle identifiers only contain [A-Za-z0-9.-]. Add a safeBundleID template function that lowercases and replaces non-alphanumeric runs with hyphens, then strips leading/trailing dashes. Applied to both Info.plist and Info.dev.plist templates. Fixes #4866
1 parent 81018ce commit 8bf0ca4

4 files changed

Lines changed: 50 additions & 3 deletions

File tree

v2/pkg/buildassets/build/darwin/Info.dev.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<key>CFBundleExecutable</key>
99
<string>{{.OutputFilename}}</string>
1010
<key>CFBundleIdentifier</key>
11-
<string>com.wails.{{.Name}}</string>
11+
<string>com.wails.{{safeBundleID .Name}}</string>
1212
<key>CFBundleVersion</key>
1313
<string>{{.Info.ProductVersion}}</string>
1414
<key>CFBundleGetInfoString</key>

v2/pkg/buildassets/build/darwin/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<key>CFBundleExecutable</key>
99
<string>{{.OutputFilename}}</string>
1010
<key>CFBundleIdentifier</key>
11-
<string>com.wails.{{.Name}}</string>
11+
<string>com.wails.{{safeBundleID .Name}}</string>
1212
<key>CFBundleVersion</key>
1313
<string>{{.Info.ProductVersion}}</string>
1414
<key>CFBundleGetInfoString</key>

v2/pkg/buildassets/buildassets.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
iofs "io/fs"
99
"os"
1010
"path/filepath"
11+
"regexp"
12+
"strings"
1113
"text/template"
1214

1315
"github.com/leaanthony/gosod"
@@ -107,8 +109,26 @@ type assetData struct {
107109
OutputFilename string
108110
}
109111

112+
func xmlEscape(s string) string {
113+
var buf bytes.Buffer
114+
template.HTMLEscape(&buf, []byte(s))
115+
return buf.String()
116+
}
117+
118+
var nonAlphaNum = regexp.MustCompile(`[^A-Za-z0-9]+`)
119+
120+
func safeBundleID(s string) string {
121+
result := strings.ToLower(nonAlphaNum.ReplaceAllString(s, "-"))
122+
result = strings.Trim(result, "-")
123+
return result
124+
}
125+
110126
func resolveProjectData(content []byte, projectData *project.Project) ([]byte, error) {
111-
tmpl, err := template.New("").Parse(string(content))
127+
funcMap := template.FuncMap{
128+
"xml": xmlEscape,
129+
"safeBundleID": safeBundleID,
130+
}
131+
tmpl, err := template.New("").Funcs(funcMap).Parse(string(content))
112132
if err != nil {
113133
return nil, err
114134
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package buildassets
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSafeBundleID(t *testing.T) {
8+
tests := []struct {
9+
input string
10+
expected string
11+
}{
12+
{"My Fab Application", "my-fab-application"},
13+
{"SimpleApp", "simpleapp"},
14+
{"App-With-Dashes", "app-with-dashes"},
15+
{"App_With_Underscores", "app-with-underscores"},
16+
{"App@#$%^&*()", "app"},
17+
{"123App", "123app"},
18+
{" Spaces ", "spaces"},
19+
{"already-lowercase", "already-lowercase"},
20+
}
21+
for _, tt := range tests {
22+
got := safeBundleID(tt.input)
23+
if got != tt.expected {
24+
t.Errorf("safeBundleID(%q) = %q, want %q", tt.input, got, tt.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)