Skip to content

Commit

Permalink
allow http for localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
DTCurrie committed Nov 19, 2024
1 parent c4a86f8 commit d2f5cb9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 13 additions & 1 deletion web/backto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ func isWhitelisted(hostname string) bool {
return hostnameWhitelist[hostname]
}

func isAllowedURLScheme(url *url.URL) bool {
if url.Scheme == "https" {
return true
}

if url.Hostname() == "localhost" && url.Scheme == "http" {
return true
}

return false
}

// IsValidBacktoURL returns true if the passed string is a secure URL to a whitelisted
// hostname. The whitelisted hostnames are: "localhost", "app.viam.dev", and "app.viam.com".
//
Expand All @@ -29,7 +41,7 @@ func IsValidBacktoURL(path string) bool {
return false
}

if url.Scheme != "" && url.Scheme != "https" {
if !isAllowedURLScheme(url) {
// ignore non-secure URLs
return false
}
Expand Down
3 changes: 2 additions & 1 deletion web/backto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func TestIsValidBacktoURL(t *testing.T) {
})

t.Run("rejects invalid local URLs", func(t *testing.T) {
test.That(t, IsValidBacktoURL("http://localhost"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("ftp://localhost"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("://localhost"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("//localhost"), test.ShouldBeFalse)
Expand All @@ -62,5 +61,7 @@ func TestIsValidBacktoURL(t *testing.T) {
t.Run("accepts valid local URLs", func(t *testing.T) {
test.That(t, IsValidBacktoURL("https://localhost"), test.ShouldBeTrue)
test.That(t, IsValidBacktoURL("https://localhost/some/path"), test.ShouldBeTrue)
test.That(t, IsValidBacktoURL("http://localhost"), test.ShouldBeTrue)
test.That(t, IsValidBacktoURL("http://localhost/some/path"), test.ShouldBeTrue)
})
}

0 comments on commit d2f5cb9

Please sign in to comment.