[PRIVACY] Add a DNS method to fetch new updates

- Use TXT records in order to determine the latest available version.
- This addresses a valid privacy issue, as with HTTP requests the server
can keep track(estimated) of how many instances are using Forgejo, with
DNS that's basically not possible as the server will never receive any
data, as the only ones receiving data are DNS resolvers.
This commit is contained in:
Gusted 2023-01-27 01:07:33 +01:00 committed by Loïc Dachary
parent e06f36f5ea
commit 14e1122af7
No known key found for this signature in database
GPG key ID: 992D23B392F9E4F2
5 changed files with 76 additions and 10 deletions

View file

@ -2112,6 +2112,7 @@ ROUTER = console
;ENABLE_SUCCESS_NOTICE = false ;ENABLE_SUCCESS_NOTICE = false
;SCHEDULE = @every 168h ;SCHEDULE = @every 168h
;HTTP_ENDPOINT = https://dl.gitea.io/gitea/version.json ;HTTP_ENDPOINT = https://dl.gitea.io/gitea/version.json
;DOMAIN_ENDPOINT = release.forgejo.org
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View file

@ -969,6 +969,7 @@ Default templates for project boards:
- `ENABLE_SUCCESS_NOTICE`: **true**: Set to false to switch off success notices. - `ENABLE_SUCCESS_NOTICE`: **true**: Set to false to switch off success notices.
- `SCHEDULE`: **@every 168h**: Cron syntax for scheduling a work, e.g. `@every 168h`. - `SCHEDULE`: **@every 168h**: Cron syntax for scheduling a work, e.g. `@every 168h`.
- `HTTP_ENDPOINT`: **https://dl.gitea.io/gitea/version.json**: the endpoint that Gitea will check for newer versions - `HTTP_ENDPOINT`: **https://dl.gitea.io/gitea/version.json**: the endpoint that Gitea will check for newer versions
- `DOMAIN_ENDPOINT`: **release.forgejo.org**: the domain that, if specified, Gitea will check for newer versions. This is preferred over `HTTP_ENDPOINT`.
#### Cron - Delete all old system notices from database ('cron.delete_old_system_notices') #### Cron - Delete all old system notices from database ('cron.delete_old_system_notices')

View file

@ -5,8 +5,11 @@
package updatechecker package updatechecker
import ( import (
"errors"
"io" "io"
"net"
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/proxy" "code.gitea.io/gitea/modules/proxy"
@ -27,7 +30,51 @@ func (r *CheckerState) Name() string {
} }
// GiteaUpdateChecker returns error when new version of Gitea is available // GiteaUpdateChecker returns error when new version of Gitea is available
func GiteaUpdateChecker(httpEndpoint string) error { func GiteaUpdateChecker(httpEndpoint, domainEndpoint string) error {
var version string
var err error
if domainEndpoint != "" {
version, err = getVersionDNS(domainEndpoint)
} else {
version, err = getVersionHTTP(httpEndpoint)
}
if err != nil {
return err
}
return UpdateRemoteVersion(version)
}
// getVersionDNS will request the TXT records for the domain. If a record starts
// with "forgejo_versions=" everything after that will be used as the latest
// version available.
func getVersionDNS(domainEndpoint string) (version string, err error) {
records, err := net.LookupTXT(domainEndpoint)
if err != nil {
return "", err
}
if len(records) == 0 {
return "", errors.New("no TXT records were found")
}
for _, record := range records {
if strings.HasPrefix(record, "forgejo_versions=") {
// Get all supported versions, separated by a comma.
supportedVersions := strings.Split(strings.TrimPrefix(record, "forgejo_versions="), ",")
// For now always return the latest supported version.
return supportedVersions[len(supportedVersions)-1], nil
}
}
return "", errors.New("there is no TXT record with a valid value")
}
// getVersionHTTP will make an HTTP request to the endpoint, and the returned
// content is JSON. The "latest.version" path's value will be used as the latest
// version available.
func getVersionHTTP(httpEndpoint string) (version string, err error) {
httpClient := &http.Client{ httpClient := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
Proxy: proxy.Proxy(), Proxy: proxy.Proxy(),
@ -36,16 +83,16 @@ func GiteaUpdateChecker(httpEndpoint string) error {
req, err := http.NewRequest("GET", httpEndpoint, nil) req, err := http.NewRequest("GET", httpEndpoint, nil)
if err != nil { if err != nil {
return err return "", err
} }
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err != nil { if err != nil {
return err return "", err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return "", err
} }
type respType struct { type respType struct {
@ -56,10 +103,9 @@ func GiteaUpdateChecker(httpEndpoint string) error {
respData := respType{} respData := respType{}
err = json.Unmarshal(body, &respData) err = json.Unmarshal(body, &respData)
if err != nil { if err != nil {
return err return "", err
} }
return respData.Latest.Version, nil
return UpdateRemoteVersion(respData.Latest.Version)
} }
// UpdateRemoteVersion updates the latest available version of Gitea // UpdateRemoteVersion updates the latest available version of Gitea

View file

@ -0,0 +1,16 @@
// Copyright 2023 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package updatechecker
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDNSUpdate(t *testing.T) {
version, err := getVersionDNS("release.forgejo.org")
assert.NoError(t, err)
assert.NotEmpty(t, version)
}

View file

@ -148,6 +148,7 @@ func registerUpdateGiteaChecker() {
type UpdateCheckerConfig struct { type UpdateCheckerConfig struct {
BaseConfig BaseConfig
HTTPEndpoint string HTTPEndpoint string
DomainEndpoint string
} }
RegisterTaskFatal("update_checker", &UpdateCheckerConfig{ RegisterTaskFatal("update_checker", &UpdateCheckerConfig{
BaseConfig: BaseConfig{ BaseConfig: BaseConfig{
@ -156,9 +157,10 @@ func registerUpdateGiteaChecker() {
Schedule: "@every 168h", Schedule: "@every 168h",
}, },
HTTPEndpoint: "https://dl.gitea.io/gitea/version.json", HTTPEndpoint: "https://dl.gitea.io/gitea/version.json",
DomainEndpoint: "release.forgejo.org",
}, func(ctx context.Context, _ *user_model.User, config Config) error { }, func(ctx context.Context, _ *user_model.User, config Config) error {
updateCheckerConfig := config.(*UpdateCheckerConfig) updateCheckerConfig := config.(*UpdateCheckerConfig)
return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint) return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint, updateCheckerConfig.DomainEndpoint)
}) })
} }