Skip to content

Commit

Permalink
Handle temp PR envs
Browse files Browse the repository at this point in the history
  • Loading branch information
DTCurrie committed Nov 19, 2024
1 parent d2f5cb9 commit 15a4230
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions web/backto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package web

import (
"fmt"
"net/url"
"regexp"
"strings"
)

Expand All @@ -12,6 +14,18 @@ var hostnameWhitelist = map[string]bool{
}

func isWhitelisted(hostname string) bool {
fmt.Printf("hostname: %v\n", hostname)

Check failure on line 17 in web/backto.go

View workflow job for this annotation

GitHub Actions / Build and Test

fmt.(Fp|P)rintf* usage found "Printf"

regex, err := regexp.Compile("pr-(\\d+)-appmain-bplesliplq-uc.a.run.app")
if err != nil {
fmt.Errorf("Error compiling regex for whitelisted hostnames: %+v", err)
return false
}

if regex.MatchString(hostname) {
return true
}

return hostnameWhitelist[hostname]
}

Expand Down
15 changes: 15 additions & 0 deletions web/backto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,19 @@ func TestIsValidBacktoURL(t *testing.T) {
test.That(t, IsValidBacktoURL("http://localhost"), test.ShouldBeTrue)
test.That(t, IsValidBacktoURL("http://localhost/some/path"), test.ShouldBeTrue)
})

t.Run("rejects invalid temp PR env URLs", func(t *testing.T) {
test.That(t, IsValidBacktoURL("http://pr-1-appmain-bplesliplq-uc.a.run.app"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("ftp://pr-12-appmain-bplesliplq-uc.a.run.app"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("://pr-123-appmain-bplesliplq-uc.a.run.app"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("//pr-1234-appmain-bplesliplq-uc.a.run.app"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("//pr-12345-appmain-bplesliplq-uc.a.run.app/some/path"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("pr-1234-appmain-bplesliplq-uc.a.run.app"), test.ShouldBeFalse)
test.That(t, IsValidBacktoURL("pr-123-appmain-bplesliplq-uc.a.run.app/some/path"), test.ShouldBeFalse)
})

t.Run("accepts valid temp PR env URLs", func(t *testing.T) {
test.That(t, IsValidBacktoURL("https://pr-12345-appmain-bplesliplq-uc.a.run.app"), test.ShouldBeTrue)
test.That(t, IsValidBacktoURL("https://pr-6789-appmain-bplesliplq-uc.a.run.app/some/path"), test.ShouldBeTrue)
})
}

0 comments on commit 15a4230

Please sign in to comment.