-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from camphor-/refactor-reliable-origins
- Loading branch information
Showing
6 changed files
with
111 additions
and
62 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package config | ||
|
||
import "regexp" | ||
|
||
// IsReliableOrigin は受け取ったURLが信頼できるURLか確認します。 | ||
func IsReliableOrigin(url string) bool { | ||
if IsDev() { | ||
return isReliableOriginDev(url) | ||
} | ||
// ローカルは何でも良い | ||
if IsLocal() { | ||
return true | ||
} | ||
|
||
return isReliableOrigin(url) | ||
} | ||
|
||
func isReliableOrigin(url string) bool { | ||
return FrontendURL() == url | ||
} | ||
|
||
func isReliableOriginDev(url string) bool { | ||
re := regexp.MustCompile(`^https://[a-zA-Z0-9-_]+\.relaym\.pages\.dev$`) | ||
return re.MatchString(url) | ||
} |
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,74 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func Test_origin_IsReliableOrigin(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
origin string | ||
env string | ||
want bool | ||
}{ | ||
{ | ||
name: "Cloudflare Pages: 正しいデプロイプレビューのURLならtrue", | ||
origin: "https://5c927597.relaym.pages.dev", | ||
env: "dev", | ||
want: true, | ||
}, | ||
{ | ||
name: "Cloudflare Pages: 別のURLならfalse", | ||
origin: "https://5c927597.relaym2.pages.dev", | ||
env: "dev", | ||
want: false, | ||
}, | ||
{ | ||
name: "Cloudflare Pages: 前後に変な文字が入ってもfalse", | ||
origin: "https://evil.example.com/https://5c927597.relaym2.pages.dev/hoge", | ||
env: "dev", | ||
want: false, | ||
}, | ||
{ | ||
name: "Cloudflare Pages: クエリパラメータで回避しようとしてもfalse", | ||
origin: "https://evil.com?hoge=5c927597.relaym.pages.dev", | ||
env: "dev", | ||
want: false, | ||
}, | ||
{ | ||
name: "ローカル環境は何でも良い", | ||
origin: "http://relaym.local:3000", | ||
env: "local", | ||
want: true, | ||
}, | ||
{ | ||
name: "本番環境では、FRONTEND_URLと同じ値のときはtrueになる", | ||
origin: "http://relaym.local:3000", // test時にセットされるFRONTEND_URLの環境変数がこれになっている。本番環境では本番のURLが環境変数で渡される。 | ||
env: "prod", | ||
want: true, | ||
}, | ||
{ | ||
name: "本番環境では、FRONTEND_URLと違う値のときはfalseになる", | ||
origin: "http://relaym.local:3001", | ||
env: "prod", | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
originalEnv := os.Getenv("ENV") | ||
os.Setenv("ENV", tt.env) | ||
defer func() { | ||
os.Setenv("ENV", originalEnv) | ||
}() | ||
if got := IsReliableOrigin(tt.origin); got != tt.want { | ||
t.Errorf("IsReliableOrigin() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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