-
-
Notifications
You must be signed in to change notification settings - Fork 7k
enhance(tls): use go's tls defaults #38687
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
silverwind
wants to merge
2
commits into
go-gitea:main
Choose a base branch
from
silverwind:tls-go-defaults
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,110 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "math" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "gitea.dev/modules/setting" | ||
| "gitea.dev/modules/test" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestToTLSVersion(t *testing.T) { | ||
| assert.Zero(t, toTLSVersion(""), "unset must keep the crypto/tls default") | ||
| assert.Zero(t, toTLSVersion("unknown")) | ||
| assert.Equal(t, uint16(tls.VersionTLS12), toTLSVersion(" TLSV1.2 ")) | ||
| } | ||
|
|
||
| func unsetTLSSettings(t *testing.T) { | ||
| t.Helper() | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLMinimumVersion, "")) | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLMaximumVersion, "")) | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLCurvePreferences, nil)) | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLCipherSuites, nil)) | ||
| } | ||
|
|
||
| // TestApplyTLSSettingsUnsetKeepsGoDefaults guards against reintroducing hardcoded defaults, | ||
| // which is how the server ended up capped at TLS 1.2 and without post-quantum key exchange. | ||
| // It asserts the fields are left alone rather than which algorithms Go then picks, so a new | ||
| // Go release changing its preferences does not fail this test. | ||
| func TestApplyTLSSettingsUnsetKeepsGoDefaults(t *testing.T) { | ||
| unsetTLSSettings(t) | ||
|
|
||
| tlsConfig := applyTLSSettings(&tls.Config{}) | ||
| assert.Zero(t, tlsConfig.MinVersion) | ||
| assert.Zero(t, tlsConfig.MaxVersion) | ||
| assert.Nil(t, tlsConfig.CurvePreferences) | ||
| assert.Nil(t, tlsConfig.CipherSuites) | ||
| assert.Equal(t, []string{"h2", "http/1.1"}, tlsConfig.NextProtos) | ||
| } | ||
|
|
||
| func TestApplyTLSSettingsConfigured(t *testing.T) { | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLMinimumVersion, "tlsv1.2")) | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLMaximumVersion, "tlsv1.3")) | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLCurvePreferences, []string{"p384"})) | ||
| t.Cleanup(test.MockVariableValue(&setting.SSLCipherSuites, []string{"ecdhe_rsa_with_aes_256_gcm_sha384"})) | ||
|
|
||
| tlsConfig := applyTLSSettings(&tls.Config{}) | ||
| assert.Equal(t, uint16(tls.VersionTLS12), tlsConfig.MinVersion) | ||
| assert.Equal(t, uint16(tls.VersionTLS13), tlsConfig.MaxVersion) | ||
| assert.Equal(t, []tls.CurveID{tls.CurveP384}, tlsConfig.CurvePreferences) | ||
| assert.Equal(t, []uint16{tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384}, tlsConfig.CipherSuites) | ||
| } | ||
|
|
||
| // TestHTTPSServesTLS13 pins the regression that an unconfigured server was capped at TLS 1.2 | ||
| func TestHTTPSServesTLS13(t *testing.T) { | ||
| unsetTLSSettings(t) | ||
|
|
||
| server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
| server.TLS = applyTLSSettings(&tls.Config{}) | ||
| server.EnableHTTP2 = true | ||
| server.StartTLS() | ||
| defer server.Close() | ||
|
|
||
| conn, err := tls.Dial("tcp", strings.TrimPrefix(server.URL, "https://"), &tls.Config{InsecureSkipVerify: true}) | ||
| require.NoError(t, err) | ||
| defer conn.Close() | ||
| assert.Equal(t, uint16(tls.VersionTLS13), conn.ConnectionState().Version) | ||
| } | ||
|
|
||
| // TestCurveStringMapCoversGoCurves fails when Go gains a curve the map does not name, so the | ||
| // config vocabulary cannot silently go stale the way it did for the post-quantum key exchanges | ||
| func TestCurveStringMapCoversGoCurves(t *testing.T) { | ||
| for id := range math.MaxUint16 + 1 { | ||
| name := tls.CurveID(id).String() | ||
| if strings.HasPrefix(name, "CurveID(") { | ||
| continue // crypto/tls has no name for this ID, so it supports no such curve | ||
| } | ||
| assert.Contains(t, curveStringMap, strings.TrimPrefix(strings.ToLower(name), "curve"), "curve %s", name) | ||
| } | ||
| } | ||
|
|
||
| // TestCipherStringMapCoversLegacyNames pins the names accepted before the map was derived | ||
| // from crypto/tls, so no existing SSL_CIPHER_SUITES config silently stops resolving | ||
| func TestCipherStringMapCoversLegacyNames(t *testing.T) { | ||
| for _, name := range []string{ | ||
| "rsa_with_rc4_128_sha", "rsa_with_3des_ede_cbc_sha", "rsa_with_aes_128_cbc_sha", | ||
| "rsa_with_aes_256_cbc_sha", "rsa_with_aes_128_cbc_sha256", "rsa_with_aes_128_gcm_sha256", | ||
| "rsa_with_aes_256_gcm_sha384", "ecdhe_ecdsa_with_rc4_128_sha", "ecdhe_ecdsa_with_aes_128_cbc_sha", | ||
| "ecdhe_ecdsa_with_aes_256_cbc_sha", "ecdhe_rsa_with_rc4_128_sha", "ecdhe_rsa_with_3des_ede_cbc_sha", | ||
| "ecdhe_rsa_with_aes_128_cbc_sha", "ecdhe_rsa_with_aes_256_cbc_sha", "ecdhe_ecdsa_with_aes_128_cbc_sha256", | ||
| "ecdhe_rsa_with_aes_128_cbc_sha256", "ecdhe_rsa_with_aes_128_gcm_sha256", "ecdhe_ecdsa_with_aes_128_gcm_sha256", | ||
| "ecdhe_rsa_with_aes_256_gcm_sha384", "ecdhe_ecdsa_with_aes_256_gcm_sha384", | ||
| "ecdhe_rsa_with_chacha20_poly1305_sha256", "ecdhe_ecdsa_with_chacha20_poly1305_sha256", | ||
| "ecdhe_rsa_with_chacha20_poly1305", "ecdhe_ecdsa_with_chacha20_poly1305", | ||
| "aes_128_gcm_sha256", "aes_256_gcm_sha384", "chacha20_poly1305_sha256", | ||
| } { | ||
| assert.NotZero(t, cipherStringMap[name], "cipher %q", name) | ||
| } | ||
| } | ||
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
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.