[PR #6862] [MERGED] fix(sub): send no-store on every subscription body #6486

Closed
opened 2026-11-02 04:09:07 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6862
Author: @yukh975
Created: 10/17/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei

Base: main ← Head: fix/sub-no-cache


📝 Commits (3)

  • 521491f fix(sub): send no-store on every subscription body
  • af287c9 Merge remote-tracking branch 'origin/main' into pr/6862
  • 3c9f7e1 refactor(sub): make no-store a rule of every subscription route group

📊 Changes

2 files changed (+35 additions, -11 deletions)

View changed files

📝 internal/sub/controller.go (+7 -11)
📝 internal/sub/controller_test.go (+28 -0)

📄 Description

What happens

Subscription bodies carry live state — traffic/expiry figures in Subscription-Userinfo, and the Happ/Incy routing headers (Routing, Routing-Enable, Profile-Update-Interval). Only the HTML page and ?format=info branches call setNoCacheHeaders:

Branch Cache-Control
HTML page (Accept: text/html) no-cache, no-store, must-revalidate
?format=info no-cache, no-store, must-revalidate
raw body (subs) absent
JSON profile (serveJsonBody) absent
Clash profile (serveClashBody) absent

With no Cache-Control, Pragma or Expires, an HTTP cache (client, CDN or reverse proxy) may store and re-serve a subscription for as long as it likes.

Reproduction (real case)

  1. Admin removes domain:example.com from DirectSites and adds it to ProxySites in the subscription routing template (subRoutingRules).
  2. The subscription response changes immediately — the Routing: header and the JSON body both show the new rule (verified against the panel).
  3. A client refreshes the subscription and keeps applying the old routing rules. It only picks the new ones up after the profile is deleted and re-imported, or after a manual routing edit.

This bites hardest with routing changes, because Routing: is a header: a cached copy silently keeps a stale deeplink alive.

Fix

Call setNoCacheHeaders(c) in the three body-serving paths:

  • subs() — before any branch, which covers the raw base64 body and the auto-detected Clash/JSON branches;
  • serveJsonBody() — covers /json/, ?format=json and ?view=raw;
  • serveClashBody() — covers /clash/, ?format=clash and ?view=raw.

Placed after the empty-body early returns so error/empty responses keep their current behaviour.

Test

TestSubscriptionBodiesAreNotCacheable drives all seven branches through the real router (raw, auto-detected Clash, auto-detected JSON, /json/, /json/?view=raw, /clash/, /clash/?view=raw) and asserts Cache-Control contains no-store.

Verified it fails without the fix on every branch:

--- FAIL: TestSubscriptionBodiesAreNotCacheable/raw_base64_body
    cache_headers_test.go:48: Cache-Control = "", want a no-store directive
...
FAIL	github.com/mhsanaei/3x-ui/v3/internal/sub

and passes with it:

--- PASS: TestSubscriptionBodiesAreNotCacheable (0.08s)
ok  	github.com/mhsanaei/3x-ui/v3/internal/sub

go vet clean, gofmt clean, full go test ./internal/sub/ green.

Scope

3 lines of production code + 1 test file. No behaviour change other than the headers; no new knobs.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6862 **Author:** [@yukh975](https://github.com/yukh975) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/24/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/sub-no-cache` --- ### 📝 Commits (3) - [`521491f`](https://github.com/MHSanaei/3x-ui/commit/521491f2fb6adc96a372556ca639ef4cbafe13a1) fix(sub): send no-store on every subscription body - [`af287c9`](https://github.com/MHSanaei/3x-ui/commit/af287c9d897135bce4af3cd2d3c56cb47db74ee1) Merge remote-tracking branch 'origin/main' into pr/6862 - [`3c9f7e1`](https://github.com/MHSanaei/3x-ui/commit/3c9f7e1f1076db13017ed45720ad919136b8ff8d) refactor(sub): make no-store a rule of every subscription route group ### 📊 Changes **2 files changed** (+35 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `internal/sub/controller.go` (+7 -11) 📝 `internal/sub/controller_test.go` (+28 -0) </details> ### 📄 Description ### What happens Subscription bodies carry live state — traffic/expiry figures in `Subscription-Userinfo`, and the Happ/Incy routing headers (`Routing`, `Routing-Enable`, `Profile-Update-Interval`). Only the HTML page and `?format=info` branches call `setNoCacheHeaders`: | Branch | `Cache-Control` | |---|---| | HTML page (`Accept: text/html`) | `no-cache, no-store, must-revalidate` | | `?format=info` | `no-cache, no-store, must-revalidate` | | **raw body (`subs`)** | **absent** | | **JSON profile (`serveJsonBody`)** | **absent** | | **Clash profile (`serveClashBody`)** | **absent** | With no `Cache-Control`, `Pragma` or `Expires`, an HTTP cache (client, CDN or reverse proxy) may store and re-serve a subscription for as long as it likes. ### Reproduction (real case) 1. Admin removes `domain:example.com` from `DirectSites` and adds it to `ProxySites` in the subscription routing template (`subRoutingRules`). 2. The subscription response changes immediately — the `Routing:` header and the JSON body both show the new rule (verified against the panel). 3. A client refreshes the subscription and **keeps applying the old routing rules**. It only picks the new ones up after the profile is deleted and re-imported, or after a manual routing edit. This bites hardest with routing changes, because `Routing:` is a *header*: a cached copy silently keeps a stale deeplink alive. ### Fix Call `setNoCacheHeaders(c)` in the three body-serving paths: - `subs()` — before any branch, which covers the raw base64 body **and** the auto-detected Clash/JSON branches; - `serveJsonBody()` — covers `/json/`, `?format=json` and `?view=raw`; - `serveClashBody()` — covers `/clash/`, `?format=clash` and `?view=raw`. Placed after the empty-body early returns so error/empty responses keep their current behaviour. ### Test `TestSubscriptionBodiesAreNotCacheable` drives all seven branches through the real router (raw, auto-detected Clash, auto-detected JSON, `/json/`, `/json/?view=raw`, `/clash/`, `/clash/?view=raw`) and asserts `Cache-Control` contains `no-store`. Verified it fails without the fix on every branch: ```text --- FAIL: TestSubscriptionBodiesAreNotCacheable/raw_base64_body cache_headers_test.go:48: Cache-Control = "", want a no-store directive ... FAIL github.com/mhsanaei/3x-ui/v3/internal/sub ``` and passes with it: ```text --- PASS: TestSubscriptionBodiesAreNotCacheable (0.08s) ok github.com/mhsanaei/3x-ui/v3/internal/sub ``` `go vet` clean, `gofmt` clean, full `go test ./internal/sub/` green. ### Scope 3 lines of production code + 1 test file. No behaviour change other than the headers; no new knobs. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:09:07 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6486
No description provided.