fix: Fix username extraction in the TLS handler

This commit is contained in:
PapaTutuWawa 2024-01-11 22:10:32 +01:00
parent 352f8bb4ce
commit 9b971daf28
4 changed files with 22 additions and 25 deletions

View File

@ -24,26 +24,14 @@ import (
"github.com/urfave/cli/v2"
)
// Extract the username from the domain name @domain that we're processing
// at the moment.
func extractUsername(pagesDomain, domain string) string {
suffixlessDomain := strings.TrimSuffix(domain, "."+pagesDomain)
usernameParts := strings.Split(suffixlessDomain, ".")
if len(usernameParts) == 1 {
return usernameParts[0]
}
return strings.Join(usernameParts, ".")
}
func handleSubdomain(pagesDomain, domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, w http.ResponseWriter) {
username := ""
if cname != "" {
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
username = extractUsername(pagesDomain, cname)
username = dns.ExtractUsername(pagesDomain, cname)
} else {
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
username = extractUsername(pagesDomain, domain)
username = dns.ExtractUsername(pagesDomain, domain)
}
// Strip the leading /

15
internal/dns/username.go Normal file
View File

@ -0,0 +1,15 @@
package dns
import "strings"
// Extract the username from the domain name @domain that we're processing
// at the moment.
func ExtractUsername(pagesDomain, domain string) string {
suffixlessDomain := strings.TrimSuffix(domain, "."+pagesDomain)
usernameParts := strings.Split(suffixlessDomain, ".")
if len(usernameParts) == 1 {
return usernameParts[0]
}
return strings.Join(usernameParts, ".")
}

View File

@ -1,9 +1,9 @@
package main
package dns
import "testing"
func TestExtractUsernameSimple(t *testing.T) {
username := extractUsername(
username := ExtractUsername(
"pages.local",
"papatutuwawa.pages.local",
)
@ -13,7 +13,7 @@ func TestExtractUsernameSimple(t *testing.T) {
}
func TestExtractUsernameDot(t *testing.T) {
username := extractUsername(
username := ExtractUsername(
"pages.local",
"polynom.me.pages.local",
)

View File

@ -44,7 +44,6 @@ func unlockDomain(domain string) {
}
func buildDomainList(domain, pagesDomain string) []string {
// TODO: For wildcards, we MUST use DNS01
if domain == pagesDomain || strings.HasSuffix(domain, pagesDomain) {
return []string{
pagesDomain,
@ -56,7 +55,6 @@ func buildDomainList(domain, pagesDomain string) []string {
}
func getDomainKey(domain, pagesDomain string) string {
// TODO: For wildcards, we MUST use DNS01
if domain == pagesDomain || strings.HasSuffix(domain, pagesDomain) {
return "*." + pagesDomain
}
@ -64,10 +62,6 @@ func getDomainKey(domain, pagesDomain string) string {
return domain
}
func usernameFromDomain(domain string) string {
return strings.Split(domain, ".")[0]
}
func getUsername(sni, pagesDomain string) (string, error) {
if !strings.HasSuffix(sni, pagesDomain) {
log.Debugf("'%s' is not a subdomain of '%s'", sni, pagesDomain)
@ -81,10 +75,10 @@ func getUsername(sni, pagesDomain string) (string, error) {
return "", errors.New("CNAME does not resolve to subdomain of pages domain")
}
return usernameFromDomain(query), nil
return dns.ExtractUsername(pagesDomain, query), nil
}
return usernameFromDomain(sni), nil
return dns.ExtractUsername(pagesDomain, sni), nil
}
func MakeTlsConfig(pagesDomain, cachePath string, cache *certificates.CertificatesCache, acmeClient *lego.Client, giteaClient *repo.GiteaClient) *tls.Config {