[PR #5495] [MERGED] fix(sub): error on oversized external subscription instead of silent truncation #5903

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5495
Author: @n0ctal
Created: 6/22/2026
Status: ✅ Merged
Merged: 6/23/2026
Merged by: @MHSanaei

Base: main ← Head: pr-0015-bound-external-sub


📝 Commits (2)

  • 4057770 fix(sub): error instead of silently truncating oversized subscription
  • 7443dc2 Merge branch 'main' into pr-0015-bound-external-sub

📊 Changes

2 files changed (+51 additions, -2 deletions)

View changed files

📝 internal/sub/external_subscription.go (+8 -2)
➕ internal/sub/external_subscription_test.go (+43 -0)

📄 Description

Summary

The external subscription fetcher capped the remote body with a plain io.LimitReader(resp.Body, subscriptionMaxBytes), which silently truncates at the 2 MiB limit and decodes whatever prefix arrived. This switches to the established N+1 overflow-detection pattern and returns an error on oversize instead of truncating.

Why

doFetchSubscriptionLinks aggregates a remote newline list of share links. When the body exceeds 2 MiB the old code returned a partial list — and the cut can land mid-link — so the client renders a complete-looking but actually truncated set of configs. Erroring is safer than silently serving a corrupted subset; the existing caller already falls back to the last cached value on error.

Scope

  • internal/sub/external_subscription.go: read subscriptionMaxBytes+1 via LimitReader; if the body exceeds the limit, return a new errSubscriptionBodyTooLarge (same *subError style as the existing errBadStatus).
  • internal/sub/external_subscription_test.go (new): over-limit body returns the sentinel error and nil links; an exactly-at-limit body round-trips its link.

Validation

gofmt clean (touched files), go vet ./internal/sub/, go build ./..., and the two new tests pass.

Risk

Behavioral change: a legitimately >2 MiB subscription previously rendered truncated-partial and now errors, so the caller serves the last cached value (or nil) instead. The 2 MiB cap itself is unchanged. 2 MiB of newline-separated links is tens of thousands of entries, so exceeding it indicates a misbehaving or hostile provider far more than a real need. Single-file revert.


🔄 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/5495 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 6/22/2026 **Status:** ✅ Merged **Merged:** 6/23/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `pr-0015-bound-external-sub` --- ### 📝 Commits (2) - [`4057770`](https://github.com/MHSanaei/3x-ui/commit/40577709f0a0d40a861a5e90c8f22ce50b05d746) fix(sub): error instead of silently truncating oversized subscription - [`7443dc2`](https://github.com/MHSanaei/3x-ui/commit/7443dc2f911855a0e0b55019dab43e06f527b6de) Merge branch 'main' into pr-0015-bound-external-sub ### 📊 Changes **2 files changed** (+51 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `internal/sub/external_subscription.go` (+8 -2) ➕ `internal/sub/external_subscription_test.go` (+43 -0) </details> ### 📄 Description ## Summary The external subscription fetcher capped the remote body with a plain `io.LimitReader(resp.Body, subscriptionMaxBytes)`, which silently truncates at the 2 MiB limit and decodes whatever prefix arrived. This switches to the established N+1 overflow-detection pattern and returns an error on oversize instead of truncating. ## Why `doFetchSubscriptionLinks` aggregates a remote newline list of share links. When the body exceeds 2 MiB the old code returned a partial list — and the cut can land mid-link — so the client renders a complete-looking but actually truncated set of configs. Erroring is safer than silently serving a corrupted subset; the existing caller already falls back to the last cached value on error. ## Scope - `internal/sub/external_subscription.go`: read `subscriptionMaxBytes+1` via `LimitReader`; if the body exceeds the limit, return a new `errSubscriptionBodyTooLarge` (same `*subError` style as the existing `errBadStatus`). - `internal/sub/external_subscription_test.go` (new): over-limit body returns the sentinel error and nil links; an exactly-at-limit body round-trips its link. ## Validation `gofmt` clean (touched files), `go vet ./internal/sub/`, `go build ./...`, and the two new tests pass. ## Risk Behavioral change: a legitimately >2 MiB subscription previously rendered truncated-partial and now errors, so the caller serves the last cached value (or nil) instead. The 2 MiB cap itself is unchanged. 2 MiB of newline-separated links is tens of thousands of entries, so exceeding it indicates a misbehaving or hostile provider far more than a real need. Single-file revert. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:04:54 +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#5903
No description provided.