Skip to content

Commit

Permalink
feat(schema): allow usage of custom schemas (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-z authored Mar 6, 2024
1 parent 27b723a commit eac6c48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Config struct {
// Allows usage of popular browser extensions schemas
AllowBrowserExtensions bool

// Allows to add custom schema like tauri://
CustomSchemas []string

// Allows usage of WebSocket protocol
AllowWebSockets bool

Expand Down Expand Up @@ -87,6 +90,9 @@ func (c Config) getAllowedSchemas() []string {
if c.AllowFiles {
allowedSchemas = append(allowedSchemas, FileSchemas...)
}
if c.CustomSchemas != nil {
allowedSchemas = append(allowedSchemas, c.CustomSchemas...)
}
return allowedSchemas
}

Expand Down
16 changes: 16 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ func TestValidateOrigin(t *testing.T) {
assert.True(t, cors.validateOrigin("chrome-extension://random-extension-id"))
}

func TestValidateTauri(t *testing.T) {
c := Config{
AllowOrigins: []string{"tauri://localhost:1234"},
AllowBrowserExtensions: true,
}
err := c.Validate()
assert.Equal(t, err.Error(), "bad origin: origins must contain '*' or include http://,https://,chrome-extension://,safari-extension://,moz-extension://,ms-browser-extension://")

c = Config{
AllowOrigins: []string{"tauri://localhost:1234"},
AllowBrowserExtensions: true,
CustomSchemas: []string{"tauri"},
}
assert.Nil(t, c.Validate())
}

func TestPassesAllowOrigins(t *testing.T) {
router := newTestRouter(Config{
AllowOrigins: []string{"http://google.com"},
Expand Down

0 comments on commit eac6c48

Please sign in to comment.