-
Notifications
You must be signed in to change notification settings - Fork 45
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
APP-6932 better local redirect validation #390
Open
DTCurrie
wants to merge
10
commits into
main
Choose a base branch
from
APP-6932-backto-path-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a1c9620
better local redirect validation
DTCurrie 957ca69
check http
DTCurrie 8f2a6ea
cleanup
DTCurrie 09c30e5
add comment
DTCurrie afc39f2
whitelist domains and restrict to https or local paths
DTCurrie c5c876d
lint
DTCurrie f90d2ee
cleanup comments
DTCurrie 78b0c9d
cleanup comments
DTCurrie 49b1ad4
only allow whitelisted fully-qualified https urls
DTCurrie b751982
rename file and function to be backto specific
DTCurrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package web | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
var hostnameWhitelist = map[string]bool{ | ||
"localhost": true, | ||
"viam.dev": true, | ||
"viam.com": true, | ||
} | ||
|
||
func isWhitelisted(hostname string) bool { | ||
return hostnameWhitelist[hostname] | ||
} | ||
|
||
// IsValidBacktoURL returns true if the passed string is a secure URL to a whitelisted | ||
// hostname. The whitelisted hostnames are: "localhost", "viam.dev", and "viam.com". | ||
// | ||
// - https://example.com -> false | ||
// - http://viam.com/path/name -> false | ||
// - https://viam.com/path/name -> true | ||
func IsValidBacktoURL(path string) bool { | ||
normalized := strings.ReplaceAll(path, "\\", "/") | ||
url, err := url.ParseRequestURI(normalized) | ||
if err != nil { | ||
// ignore invalid URLs/URL components | ||
return false | ||
} | ||
|
||
if url.Scheme != "" && url.Scheme != "https" { | ||
// ignore non-secure URLs | ||
return false | ||
} | ||
|
||
if isWhitelisted(url.Hostname()) { | ||
// ignore non-whitelisted hosts | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package web | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.viam.com/test" | ||
) | ||
|
||
func TestIsValidBacktoURL(t *testing.T) { | ||
t.Run("rejects external URLs", func(t *testing.T) { | ||
test.That(t, IsValidBacktoURL("https://example.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("http://example.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("ftp://example.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("://example.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("//example.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("example.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("www.example.com"), test.ShouldBeFalse) | ||
}) | ||
|
||
t.Run("rejects invalid production URLs", func(t *testing.T) { | ||
test.That(t, IsValidBacktoURL("http://viam.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("ftp://viam.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("://viam.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("//viam.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("//viam.com/some/path"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("viam.com"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("viam.com/some/path"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("www.viam.com"), test.ShouldBeFalse) | ||
}) | ||
|
||
t.Run("accepts valid production URLs", func(t *testing.T) { | ||
test.That(t, IsValidBacktoURL("https://viam.com"), test.ShouldBeTrue) | ||
test.That(t, IsValidBacktoURL("https://viam.com/some/path"), test.ShouldBeTrue) | ||
}) | ||
|
||
t.Run("rejects invalid staging URLs", func(t *testing.T) { | ||
test.That(t, IsValidBacktoURL("http://viam.dev"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("ftp://viam.dev"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("://viam.dev"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("//viam.dev"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("//viam.dev/some/path"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("viam.dev"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("viam.dev/some/path"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("www.viam.dev"), test.ShouldBeFalse) | ||
}) | ||
|
||
t.Run("accepts valid staging URLs", func(t *testing.T) { | ||
test.That(t, IsValidBacktoURL("https://viam.dev"), test.ShouldBeTrue) | ||
test.That(t, IsValidBacktoURL("https://viam.dev/some/path"), test.ShouldBeTrue) | ||
}) | ||
|
||
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) | ||
test.That(t, IsValidBacktoURL("//localhost/some/path"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("localhost"), test.ShouldBeFalse) | ||
test.That(t, IsValidBacktoURL("localhost/some/path"), test.ShouldBeFalse) | ||
}) | ||
|
||
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) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should these be app.viam.dev and app.viam.com?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left them as this because we have toyed with allowing users to log in from the marketing page and docs, but maybe I am being a little too forward-thinking here 😃 .
Let's start stricter; I'll update it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha! yeah i wasnt sure if it would pass the
hostnameWhitelist[hostname]
check forapp.viam.com
urls otherwise. could be misreading though