mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6404] [MERGED] fix(geofile): verify downloaded geo databases against published digests #6254
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6254
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6404
Author: @rokokol
Created: 9/3/2026
Status: ✅ Merged
Merged: 9/5/2026
Merged by: @MHSanaei
Base:
main← Head:fix/geofile-digest-verification📝 Commits (2)
0f66cfcfix(geofile): verify downloaded geo databases against published digests806b74afix(geofile): pin the release and scope atomicity to one upstream📊 Changes
2 files changed (+641 additions, -80 deletions)
View changed files
📝
internal/web/service/server.go(+247 -80)➕
internal/web/service/server_geofile_test.go(+394 -0)📄 Description
Summary
ServerService.UpdateGeofilenow verifies every downloaded geo database against the SHA-256 its upstream publishes, pins each download to one immutable release, installs an upstream's databases atomically, and no longer restarts Xray when nothing changed.Why
UpdateGeofilewrote whatever the three upstreams returned straight into the Xray asset folder withos.Create+io.Copyand no integrity check of any kind. Xray parses these databases when it builds its domain and IP matchers, so a corrupted or substituted file takes the core down at its next start.The panel already does the right thing for the other artifact it downloads.
installXrayverifies the release archive against the SHA-256 published in its.dgstsidecar (fetchXrayDigestSHA256/parseXrayDigestSHA256, covered byserver_xray_checksum_test.go). The geo databases were the only download in the panel that skipped that step, even though all three upstreams publish a<asset>.sha256sumnext to every.dat:Loyalsoldier/v2ray-rules-datrunetfreedom/russia-v2ray-rules-datchocolate4u/Iran-v2ray-rulesWhat changed
1. Verify before installing. Each
.datis downloaded to a staging folder, its.sha256sumsidecar is fetched, and the digest is compared against the bytes that actually arrived.2. Pin the release. The asset and its sidecar are two requests, so fetching both from
releases/latest/download/…lets GitHub re-resolvelatestin between. These upstreams publish several times a day —202609022346,202609030908and202609031849are three tags from a single day — so a release landing mid-batch would check release N+1's digest against release N's bytes and report a healthy upstream as tampered with. The tag is now resolved once per upstream from the redirect GitHub already returns, and both the asset and its digest are fetched from it. This is why an entry is modelled as repo + asset rather than an opaque URL.3. Atomic per upstream, not across all six. A
geoip/geositepair from one release installs together or not at all. Coupling the three independent repositories buys no integrity and costs a lot: one transient 5xx would discard every verified download and re-fetch tens of megabytes on the next attempt. Each upstream now succeeds or aborts on its own and errors are collected, as the function did before this change.4. No restart on a no-op refresh. The conditional
If-Modified-SinceGET was already there, butRestartXrayService()ran unconditionally afterwards, dropping every client connection even when all six upstreams answered304.The digest line is matched by base name
The upstreams disagree on the name field, so
sha256sum --checksemantics — look up the name written in the file — fail on a perfectly good download from chocolate4u:A leading
*,sha256sum's binary-mode marker, is stripped for the same reason: nothing stops an upstream regenerating with-b.Type of change
Areas affected
How was this tested?
New suite in
internal/web/service/server_geofile_test.go, stdlibtestingwithhttptestupstreams that mimic GitHub'sreleases/latest/download→releases/download/<tag>redirect, and a throwaway DB. Each test was checked to fail without the fix by reverting the relevant part and watching it go red:TestParseGeofileDigest(build-path,binary-mode-marker)TestParseGeofileDigest_ErrorsTestUpdateGeofileRejectsDigestMismatchTestUpdateGeofileInstallsNeitherFileOfAFailedUpstreamgeoip.datis provably downloaded before the corruptgeosite.dat, rather than relying on map orderTestUpdateGeofileKeepsGoodUpstreamWhenAnotherFailsTestUpdateGeofileSurvivesReleaseRotationlatestre-resolved, a healthy upstream is reported as "corrupted or has been tampered with"TestUpdateGeofileSkipsRestartWhenNotModifiedThe
.sha256sumasset names and the304behaviour were also checked against the live upstreams: all six sidecars exist atreleases/latest/download/, GitHub honoursIf-Modified-Sincethrough the release redirect and answers304with a zero-byte body, and the first hop of that redirect is what carries the concrete tag:Endpoint level:
POST /panel/api/server/updateGeofile(and/updateGeofile/:fileName). The first run installs and restarts; an immediate second run gets304for all six, installs nothing, and does not restart the core.Screenshots / recordings
N/A — no UI change.
Breaking changes
None for configuration or API shape. One behaviour change worth calling out: a failed download or digest check now aborts that upstream's update and returns an error, where before every failure was collected into a message and whatever succeeded was installed regardless. Files from the other upstreams are still installed, so a single flaky repository no longer costs the whole refresh — but a repository whose
geoipverifies and whosegeositedoes not will now install neither, by design.Checklist
go build ./...and the test suite pass locally.npm run lint,npm run typecheck, andnpm run buildpass. — N/A, no frontend change🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.