Skip to content

Prevent invalid subscription responses from overwriting config.yaml - #818

Open
Andyduck-ops wants to merge 1 commit into
MetaCubeX:mainfrom
Andyduck-ops:agent/fix-invalid-subscription-response
Open

Prevent invalid subscription responses from overwriting config.yaml#818
Andyduck-ops wants to merge 1 commit into
MetaCubeX:mainfrom
Andyduck-ops:agent/fix-invalid-subscription-response

Conversation

@Andyduck-ops

@Andyduck-ops Andyduck-ops commented Aug 11, 2026

Copy link
Copy Markdown

Problem

When updating a profile, CMFA assumes that every response body is a valid Clash configuration.

If a subscription server returns an error page or informational text instead of YAML, CMFA currently:

  1. accepts the response without checking its HTTP status;
  2. truncates and overwrites the existing config.yaml with the response body;
  3. only attempts to parse the file afterward.

This produces a misleading YAML error such as:

cannot unmarshal !!str into config.RawConfig

More importantly, the previously valid configuration has already been replaced by the invalid response.

Root cause

openUrl returned the response body regardless of the HTTP status, and the configuration download path passed that body directly to writeFile.

Configuration validation only happened later in UnmarshalAndPatch, after config.yaml had already been overwritten.

The original YAML involved in the reported case parses successfully with Mihomo. The parsing error was caused by a non-configuration response body from the subscription server, not by CRLF or flow-style YAML syntax.

Reproduction

Use a subscription endpoint that returns either:

  • HTTP 502 with an error page; or
  • HTTP 200 with plain text such as 检测到不受支持的客户端.

With an existing valid config.yaml, trigger a forced profile update.

Before this change, CMFA writes the response body to config.yaml and then reports a YAML unmarshalling error.

Fix

  • Reject non-2xx HTTP responses before reading them as subscription data.
  • Parse downloaded profile content with mihomo/config.UnmarshalRawConfig before writing it.
  • Preserve the existing config.yaml when the downloaded content is not a valid configuration.
  • Keep generic provider downloads on the existing unvalidated fetch path to limit the behavior change to the main profile configuration.

Tests

Added regression coverage for:

  • rejecting an HTTP 502 response;
  • rejecting an HTTP 200 plain-text response without overwriting the existing configuration;
  • accepting and writing a valid Clash YAML response.

The tests were formatted and added to the affected Go package. Full local execution was blocked by unavailable cached build dependencies, so CI verification is still required.

@Andyduck-ops Andyduck-ops changed the title Validate subscription responses before replacing config Prevent invalid subscription responses from overwriting config.yaml Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant