From 6fc17ad37bb4eb07f595d975937cccd20aa8edcc Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 27 Jan 2023 01:07:33 +0100 Subject: [PATCH] [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. (cherry picked from commit 0baefb546ab96bc3c06d90feffdb14873c2c2a3a) (cherry picked from commit e8ee41880b775532e6a68bd2052ed96d369dee78) (cherry picked from commit 7eca4f3bf1faa3f063c9668d1bb354b842361007) (cherry picked from commit 6dde3992dc504f105f2285c34fa6445fa24da0b1) (cherry picked from commit fb3a37fbfc73d35e3ba8d793b6051aeddcbb0634) (cherry picked from commit 8304af1e9d94b89ee700b0d454d74d0dc5cfac64) (cherry picked from commit 0543a7d12a4bac012db8b9e683e879e6f265a9dc) (cherry picked from commit c3a22933b7ca8201c96267d360281848fb2a1c7e) (cherry picked from commit e24370769413aebe96ba6cfb230da3c2c33e39f4) (cherry picked from commit 7eb6d1bcf7a0bdae0cbbf06136854d0b29c54899) (cherry picked from commit 1d7b9535cd73bffda5defd143c25e514cc55dee8) (cherry picked from commit 05920dce67808ae346c4e9cd9a41c9942e857bc6) (cherry picked from commit f173f27d7c3bf36c54b7e715a61b2308d1b0d277) (cherry picked from commit 90e1c9340e72f08e67293647cf5f19b3e3571909) (cherry picked from commit de68610ea70b81146f6412e42d72b97aad0428ae) (cherry picked from commit 8d5757ea0459cffeb885462b7a9d1e1c630e1f16) (cherry picked from commit c7a7fff316c1a88ba9b0984114a25cecfac56bce) (cherry picked from commit 39ac8b8fc164090eacd5870bf78ea863d2add5fa) (cherry picked from commit 98892033012ccc4f4bef30e10932fa22bf12ebcd) [PRIVACY]: Adjust update checker description - Resolves #323 - Adjust the description of the update check function on the installation page to describe the privacy method instead of the HTTP method by checking gitea.io (cherry picked from commit 61eae5b105e374169fe23fbb150ce080f8f340ec) (cherry picked from commit 091def20a1180af718209f2bd520cb249b1943ae) (cherry picked from commit d5d11bf45a77cad6be2194620c4d865231cfbe67) (cherry picked from commit 71863d470739c743335931f1563e5b63cf3194eb) (cherry picked from commit 11ece4aab1ba7a154e985a6732c6839d8a688315) (cherry picked from commit afdd7e714f9e735811dc909a854cfac964e474e7) (cherry picked from commit 39170e2f1d64d79bb05940b72d7db602bcff1bce) (cherry picked from commit 4b3a52aab82e8b9f59f247983a4aa7de65c6c747) (cherry picked from commit 9d763c5fc8cd0ff7898855e1ef088edb264388d5) (cherry picked from commit 638db15482f6907fa21e27aaccf6795226ae60c0) (cherry picked from commit a52bfdd8e76466aa293855a35c6ac9e18a228e90) (cherry picked from commit dc93d00e85dfdd9c090294f5504b8d885a602c60) (cherry picked from commit 0bc4b3508c8d2e49b57d47b4c5b36a9365580a1b) (cherry picked from commit 3f760d85a45520d3340bf27efe667fc689b6b8ce) (cherry picked from commit ecc2716785036b5538c8022d1c4dd6156796acef) (cherry picked from commit 6334d5677eb3e1848ab16630a22ae59ab716a732) Conflicts: modules/updatechecker/update_checker.go UpdateRemoteVersion now has a context argument. However, in the updated code from Gitea the context comes from the HTTP request and does not actually provide any useful context. Replace that with context.Background() (cherry picked from commit ca2200767edb9c30b655b942824957577b1f6fe7) (cherry picked from commit f46feca224d388e92875a76c8d2428c2681ad0c5) (cherry picked from commit a800a66ca89c03a8e08f26f633bea8634cffe629) (cherry picked from commit 21f017454e90db7c1ce47627785e4781396300b9) (cherry picked from commit 762d18e09a5cc04f7c2ff374e1202324244999df) (cherry picked from commit 6d28e120c131b2547a87691a6195205cedfac052) (cherry picked from commit 4fcd7e7cbf55d7b69568a931fc86b3aef8346adc) (cherry picked from commit 95162851128ef1d03435bd5e011748dc3d0f26f8) (cherry picked from commit 0dd752a2624693fb1c9c4dd0efa8e343ae1ad3c7) (cherry picked from commit b1f1af7fe03878c333966b2f4c6581ab0e6c2593) (cherry picked from commit 71937deaa575015fb5e9073a014b4fba74ed92a8) (cherry picked from commit 93e41a782334b7b53198da89e1c8a92bb9e6b6be) (cherry picked from commit f497ed30cfaafb2c801ad6bfccc7788a128c4435) (cherry picked from commit bff3346f7df2cf28b1f1fdd637a87bbe6940326c) (cherry picked from commit 906ae19c0f61d808decf4fab0abc2086abf9d4e2) --- custom/conf/app.example.ini | 1 + modules/updatechecker/update_checker.go | 60 +++++++++++++++++--- modules/updatechecker/update_checker_test.go | 16 ++++++ options/locale/locale_en-US.ini | 1 + services/cron/tasks_extended.go | 8 ++- templates/install.tmpl | 2 +- 6 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 modules/updatechecker/update_checker_test.go diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index e6840cd2e..a62526484 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2202,6 +2202,7 @@ LEVEL = Info ;ENABLE_SUCCESS_NOTICE = false ;SCHEDULE = @every 168h ;HTTP_ENDPOINT = https://dl.gitea.com/gitea/version.json +;DOMAIN_ENDPOINT = release.forgejo.org ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/modules/updatechecker/update_checker.go b/modules/updatechecker/update_checker.go index 3c1e05d06..0c93f08d2 100644 --- a/modules/updatechecker/update_checker.go +++ b/modules/updatechecker/update_checker.go @@ -5,8 +5,11 @@ package updatechecker import ( "context" + "errors" "io" + "net" "net/http" + "strings" "code.gitea.io/gitea/modules/json" "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 -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(context.Background(), 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{ Transport: &http.Transport{ Proxy: proxy.Proxy(), @@ -36,16 +83,16 @@ func GiteaUpdateChecker(httpEndpoint string) error { req, err := http.NewRequest("GET", httpEndpoint, nil) if err != nil { - return err + return "", err } resp, err := httpClient.Do(req) if err != nil { - return err + return "", err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { - return err + return "", err } type respType struct { @@ -56,10 +103,9 @@ func GiteaUpdateChecker(httpEndpoint string) error { respData := respType{} err = json.Unmarshal(body, &respData) if err != nil { - return err + return "", err } - - return UpdateRemoteVersion(req.Context(), respData.Latest.Version) + return respData.Latest.Version, nil } // UpdateRemoteVersion updates the latest available version of Gitea diff --git a/modules/updatechecker/update_checker_test.go b/modules/updatechecker/update_checker_test.go new file mode 100644 index 000000000..301afd95e --- /dev/null +++ b/modules/updatechecker/update_checker_test.go @@ -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) +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 9cd2d1123..7fbbbfdf2 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -287,6 +287,7 @@ run_user_not_match = The 'run as' username is not the current username: %s -> %s internal_token_failed = Failed to generate internal token: %v secret_key_failed = Failed to generate secret key: %v save_config_failed = Failed to save configuration: %v +enable_update_checker_helper_forgejo = Periodically checks for new Forgejo versions by checking a DNS TXT record at release.forgejo.org. invalid_admin_setting = Administrator account setting is invalid: %v invalid_log_root_path = The log path is invalid: %v default_keep_email_private = Hide Email Addresses by Default diff --git a/services/cron/tasks_extended.go b/services/cron/tasks_extended.go index 4abf44cdb..569e8fbd2 100644 --- a/services/cron/tasks_extended.go +++ b/services/cron/tasks_extended.go @@ -142,7 +142,8 @@ func registerDeleteOldActions() { func registerUpdateGiteaChecker() { type UpdateCheckerConfig struct { BaseConfig - HTTPEndpoint string + HTTPEndpoint string + DomainEndpoint string } RegisterTaskFatal("update_checker", &UpdateCheckerConfig{ BaseConfig: BaseConfig{ @@ -150,10 +151,11 @@ func registerUpdateGiteaChecker() { RunAtStart: false, Schedule: "@every 168h", }, - HTTPEndpoint: "https://dl.gitea.com/gitea/version.json", + HTTPEndpoint: "https://dl.gitea.com/gitea/version.json", + DomainEndpoint: "release.forgejo.org", }, func(ctx context.Context, _ *user_model.User, config Config) error { updateCheckerConfig := config.(*UpdateCheckerConfig) - return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint) + return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint, updateCheckerConfig.DomainEndpoint) }) } diff --git a/templates/install.tmpl b/templates/install.tmpl index e9b267fa1..fda07fca6 100644 --- a/templates/install.tmpl +++ b/templates/install.tmpl @@ -152,7 +152,7 @@ - {{ctx.Locale.Tr "install.enable_update_checker_helper"}} + {{ctx.Locale.Tr "install.enable_update_checker_helper_forgejo"}}