-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Use hostmatcher for openid allow/block list. Disable OpenID by default and added default black list for OpenID #36729
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
Open
lunny
wants to merge
7
commits into
go-gitea:main
Choose a base branch
from
lunny:lunny/disable_openid_by_default
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
7 commits
Select commit
Hold shift + click to select a range
ae63568
Disable OpenID by default. Added default black list for OpenID
lunny 6996249
Use hostmatcher to detect open id allow/block list
lunny b288038
Merge branch 'main' into lunny/disable_openid_by_default
lunny 6d7518a
adjustment
lunny 2b6df87
Merge branch 'main' into lunny/disable_openid_by_default
lunny 4c5a036
Fix admin config page
lunny 3ae00fb
Merge branch 'main' into lunny/disable_openid_by_default
lunny 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 hidden or 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 hidden or 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,63 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package setting | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "code.gitea.io/gitea/modules/hostmatcher" | ||
| ) | ||
|
|
||
| // OpenID settings | ||
| var OpenID = struct { | ||
| EnableSignIn bool | ||
| EnableSignUp bool | ||
| Allowlist *hostmatcher.HostMatchList | ||
| Blocklist *hostmatcher.HostMatchList | ||
| }{ | ||
| EnableSignIn: false, | ||
| EnableSignUp: false, | ||
| Allowlist: nil, | ||
| Blocklist: nil, | ||
| } | ||
|
|
||
| var defaultOpenIDBlocklist = []string{ | ||
| "localhost", | ||
| hostmatcher.MatchBuiltinLoopback, | ||
| hostmatcher.MatchBuiltinPrivate, | ||
| "169.254.0.0/16", | ||
| "fe80::/10", | ||
| } | ||
|
|
||
| func splitOpenIDHostList(raw string) []string { | ||
| return strings.FieldsFunc(raw, func(r rune) bool { | ||
| switch r { | ||
| case ',', ' ', '\t', '\n', '\r': | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // loadOpenIDSetting loads OpenID settings from rootCfg, depends on service settings, | ||
| // so should be called after loadServiceSettings. | ||
| func loadOpenIDSetting(rootCfg ConfigProvider) { | ||
| sec := rootCfg.Section("openid") | ||
| OpenID.EnableSignIn = sec.Key("ENABLE_OPENID_SIGNIN").MustBool(false) | ||
| OpenID.EnableSignUp = sec.Key("ENABLE_OPENID_SIGNUP").MustBool(!Service.DisableRegistration && OpenID.EnableSignIn) | ||
| OpenID.Allowlist = nil | ||
| OpenID.Blocklist = nil | ||
| allowlist := splitOpenIDHostList(sec.Key("WHITELISTED_URIS").String()) | ||
| if len(allowlist) != 0 { | ||
| OpenID.Allowlist = hostmatcher.ParseHostMatchList("openid.WHITELISTED_URIS", strings.Join(allowlist, ",")) | ||
| } | ||
| blocklist := splitOpenIDHostList(sec.Key("BLACKLISTED_URIS").String()) | ||
| if len(blocklist) == 0 { | ||
| blocklist = defaultOpenIDBlocklist | ||
| } | ||
| if len(blocklist) != 0 { | ||
| OpenID.Blocklist = hostmatcher.ParseHostMatchList("openid.BLACKLISTED_URIS", strings.Join(blocklist, ",")) | ||
| } | ||
| } | ||
This file contains hidden or 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,73 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package setting | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "code.gitea.io/gitea/modules/test" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestLoadOpenIDSettings(t *testing.T) { | ||
| defer test.MockVariableValue(&Service)() | ||
|
|
||
| t.Run("DefaultBlacklist", func(t *testing.T) { | ||
| cfg, err := NewConfigProviderFromData(` | ||
| [openid] | ||
| `) | ||
| assert.NoError(t, err) | ||
| loadServiceFrom(cfg) | ||
|
|
||
| assert.False(t, OpenID.EnableSignIn) | ||
| assert.False(t, OpenID.EnableSignUp) | ||
| assert.Nil(t, OpenID.Allowlist) | ||
| if assert.NotNil(t, OpenID.Blocklist) { | ||
| assert.True(t, OpenID.Blocklist.MatchHostName("localhost")) | ||
| assert.True(t, OpenID.Blocklist.MatchHostName("127.0.0.1")) | ||
| assert.True(t, OpenID.Blocklist.MatchHostName("192.168.0.1")) | ||
| assert.True(t, OpenID.Blocklist.MatchHostName("fe80::1")) | ||
| assert.False(t, OpenID.Blocklist.MatchHostName("example.com")) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("AllowlistParsing", func(t *testing.T) { | ||
| cfg, err := NewConfigProviderFromData(` | ||
| [openid] | ||
| ENABLE_OPENID_SIGNIN = true | ||
| WHITELISTED_URIS = example.com, *.trusted.example | ||
| `) | ||
| assert.NoError(t, err) | ||
| loadServiceFrom(cfg) | ||
|
|
||
| assert.True(t, OpenID.EnableSignIn) | ||
| assert.True(t, OpenID.EnableSignUp) | ||
| if assert.NotNil(t, OpenID.Allowlist) { | ||
| assert.True(t, OpenID.Allowlist.MatchHostName("example.com")) | ||
| assert.True(t, OpenID.Allowlist.MatchHostName("foo.trusted.example")) | ||
| assert.False(t, OpenID.Allowlist.MatchHostName("example.org")) | ||
| } | ||
| if assert.NotNil(t, OpenID.Blocklist) { | ||
| assert.True(t, OpenID.Blocklist.MatchHostName("localhost")) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("BlocklistParsing", func(t *testing.T) { | ||
| cfg, err := NewConfigProviderFromData(` | ||
| [openid] | ||
| BLACKLISTED_URIS = bad.example.com, 10.0.0.0/8 | ||
| `) | ||
| assert.NoError(t, err) | ||
| loadServiceFrom(cfg) | ||
|
|
||
| assert.Nil(t, OpenID.Allowlist) | ||
| if assert.NotNil(t, OpenID.Blocklist) { | ||
| assert.True(t, OpenID.Blocklist.MatchHostName("bad.example.com")) | ||
| assert.True(t, OpenID.Blocklist.MatchHostName("10.1.1.1")) | ||
| assert.False(t, OpenID.Blocklist.MatchHostName("localhost")) | ||
| assert.False(t, OpenID.Blocklist.MatchHostName("good.example.com")) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
| // Copyright 2024 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package auth | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "code.gitea.io/gitea/modules/hostmatcher" | ||
| "code.gitea.io/gitea/modules/setting" | ||
| "code.gitea.io/gitea/modules/test" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAllowedOpenIDURI(t *testing.T) { | ||
| defer test.MockVariableValue(&setting.Service)() | ||
|
|
||
| t.Run("Whitelist", func(t *testing.T) { | ||
| setting.OpenID.Allowlist = hostmatcher.ParseHostMatchList("openid.WHITELISTED_URIS", "trusted.example.com,*.trusted.net") | ||
| setting.OpenID.Blocklist = hostmatcher.ParseHostMatchList("openid.BLACKLISTED_URIS", "trusted.example.com,bad.example.com") | ||
|
|
||
| assert.NoError(t, allowedOpenIDURI("https://trusted.example.com/openid")) | ||
| assert.NoError(t, allowedOpenIDURI("https://sub.trusted.net")) | ||
| assert.Error(t, allowedOpenIDURI("https://trusted.example.com.evil.org")) | ||
| assert.Error(t, allowedOpenIDURI("https://bad.example.com")) | ||
| }) | ||
|
|
||
| t.Run("Blacklist", func(t *testing.T) { | ||
| setting.OpenID.Allowlist = nil | ||
| setting.OpenID.Blocklist = hostmatcher.ParseHostMatchList("openid.BLACKLISTED_URIS", "bad.example.com,10.0.0.0/8") | ||
|
|
||
| assert.Error(t, allowedOpenIDURI("https://bad.example.com")) | ||
| assert.Error(t, allowedOpenIDURI("https://10.1.1.1")) | ||
| assert.NoError(t, allowedOpenIDURI("https://good.example.com")) | ||
| }) | ||
|
|
||
| t.Run("InvalidURI", func(t *testing.T) { | ||
| setting.OpenID.Allowlist = nil | ||
| setting.OpenID.Blocklist = nil | ||
|
|
||
| assert.Error(t, allowedOpenIDURI("://bad")) | ||
| }) | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.