Skip to content

fix(MyAnonaMouse): persist refreshed mam_id so sessions don't expire after 30 days - #308

Open
director-mouse wants to merge 2 commits into
Chaptarr:developfrom
director-mouse:fix/mam-cookie-refresh
Open

director-mouse wants to merge 2 commits into
Chaptarr:developfrom
director-mouse:fix/mam-cookie-refresh

Conversation

@director-mouse

@director-mouse director-mouse commented Oct 10, 2026 •

Copy link
Copy Markdown

My MAM indexer has been losing auth on the 1st of every month for a while now, and after pulling the logs it turned out to be exactly 30 days after each session was created, to within the granularity of the hourly MyAnonaMouseAccountStatusService refresh. Two consecutive cycles lined up: a session created 2026-07-02 ~03:00 UTC died 2026-08-01 between 02:51 and 03:03 UTC, and the replacement created 2026-08-02 ~03:03 UTC died 2026-09-01 between 02:32 and 02:56 UTC. In both cases MAM started answering 403 with Error, you are not signed in on both jsonLoad.php?snatch_summary and loadSearchJSONbasic.php, with no network changes on my side.

MAM rotates the mam_id cookie via Set-Cookie, and the comment above the StoreResponseCookie lines in MyAnonaMouse.cs says the intent is to keep the "updated mam_id from MAM". The code never manages that, though. Every request sets request.Cookies["mam_id"] = Settings.MamId, and InitializeRequestCookies in HttpClient.cs lets request.Cookies overwrite whatever the persistent container holds (and with StoreRequestCookie = true it writes the stale value back into the container as well). So Chaptarr only ever presents the original cookie, and the original cookie has an absolute expiry of roughly 30 days.

This PR fixes that in the one place that already has the right plumbing. RefreshAccountStatus() runs hourly and MyAnonaMouseAccountStatusService already calls _indexerFactory.Update(definition) right after it to persist UserClass, IsVip and the unsatisfied counters. If the response carries a mam_id that differs from Settings.MamId, we write it into Settings.MamId and the existing persistence call saves it. Nothing else changes: the search and download paths keep overriding the jar with Settings.MamId, which is fine once that value is kept current, and the up-to-one-hour lag between MAM rotating and Chaptarr persisting is a non-issue given the old cookie stays valid for a month.

One thing worth knowing is that this only does anything while the status refresh succeeds. Because ProviderFactory reloads definitions from the DB on every call, a rotated mam_id written into Settings is thrown away if RefreshAccountStatus() throws before _indexerFactory.Update(definition) runs, and Chaptarr keeps presenting the last one that was saved. That's what happened between MAM's snatch_summary response change on 2026-09-23 and #189 (#189), which is in v0.9.965, so this PR has no other dependencies now.

A couple of things I'm not 100% sure about and would appreciate a second pair of eyes on:

  1. MAM rotates mam_id on every jsonLoad.php?snatch_summary response as far as I can tell, with Max-Age=1296000 (15 days) on the new cookie, and older values keep working after newer ones are issued. I don't know whether that's guaranteed behavior on MAM's side or just what it happens to do today.
  2. TestConnection() calls RefreshAccountStatus() on a throwaway definition, so a rotated value there won't be persisted, which I think is the correct behavior but worth calling out.

I've been running this patch since 2026-09-07 05:25 UTC, on a fresh session created at the same time (on v0.9.958 plus #189, and now on v0.9.965). That session's old 30 day mark was 2026-10-07 05:25 UTC, and it went straight past it with no 403s, with MAM issued a refreshed mam_id and Updated MAM account status logged on every hourly refresh. The patch applies cleanly to v0.9.965.

I've also added regression tests to MyAnonaMouseWedgeFixture: a rotated mam_id is sent on the next status request and saved by the refresh service, a missing or blank cookie keeps the current value, and a failed refresh (HTTP error or invalid summary) saves nothing. The two rotation tests fail without the fix. dotnet test src/Chaptarr.Core.Test/Chaptarr.Core.Test.csproj passes (3,043 tests), though locally I had to add -p:WarningsNotAsErrors=NU1902%3BNU1903 because restore currently fails on the existing SixLabors.ImageSharp 3.1.12 vulnerability warnings.

Summary by CodeRabbit

  • Bug Fixes
    • Account status refreshes now save a valid, updated MyAnonaMouse ID for subsequent requests. The existing ID is retained when no usable replacement is provided or when the refresh fails.

@director-mouse
director-mouse marked this pull request as ready for review October 10, 2026 22:14
Save rotated mam_id values from successful account-status responses in indexer settings so the existing refresh service persists them. This prevents requests from continually reusing the original session cookie until it expires.
Verify that a rotated mam_id is sent on the next account-status request and saved by the refresh service, that missing or blank cookies keep the current value, and that failed refreshes save nothing.
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The MAM account-status refresh now adopts a usable, changed mam_id response cookie after an HTTP success and before deserializing the account data. Tests cover persistence, subsequent requests, unusable cookies, and failed refreshes.

Changes: MAM ID Cookie Refresh

Layer / File(s) Summary
Persist a refreshed MAM ID
src/NzbDrone.Core/Indexers/MyAnonaMouse/MyAnonaMouse.cs
After an HTTP success, the refresh updates Settings.MamId when the response contains a nonblank mam_id cookie that differs from the current value.
Verify cookie refresh behavior
src/Chaptarr.Core.Test/Indexers/MyAnonaMouseWedgeFixture.cs
Tests cover sending and persisting a rotated ID, retaining the current ID for absent or unusable cookies, and not persisting a replacement after HTTP or summary failures.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: Minimal · up to fc125

This change saves a refreshed MyAnonaMouse session cookie so sign-ins no longer fail after about 30 days. The added tests cover the main success and failure cases, and no merge-blocking issue was found.

Security architecture risk: Minimal · up to fc125

The change uses the existing per-indexer credential field and saves it only after the background refresh returns successfully. The inspected change established no introduced security, ownership, or failure-containment regression.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — Each accepted response can replace the credential of its refreshed MAM indexer definition. Control of a common upstream response source could influence multiple MAM definitions using that source, but the new helper does not select arbitrary provider rows or introduce wider infrastructure authority.

Trust Boundaries and Controls

  • inferred — The intended new authority is the session issuer's ability to replace its stored credential. The refresh targets the configured account-status URL, defaults to HTTPS, and uses the builder's redirect-disabled setting. Generic cross-host redirect support therefore does not establish a reachable attack path for this request. Configurable plaintext HTTP predates the change and already exposes the outgoing session credential.

Resilience and Maintainability Implications

  • observed — Credential adoption occurs before response-body validation, but the background service saves only after the refresh returns normally. HTTP or summary-validation failures therefore skip that service's save. Missing, blank, and unchanged cookie values do not replace the configured credential.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: MyAnonaMouse now persists a refreshed mam_id so sessions do not expire after 30 days. It matches the raw summary, which describes saving a rotated mam_id during the ac…
Description check Passed The description covers the required template content. It explains the change and the reason for it, and it links the dependency on #189. It also describes the testing done, including the test command …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: MyAnonaMouse now persists a refreshed mam_id so sessions do not expire after 30 days. It matches the raw summary, which describes saving a rotated mam_id during the account-status refresh. The title is somewhat long at 82 characters, but it is specific and accurate.

Full details: Description check

Explanation

The description covers the required template content. It explains the change and the reason for it, and it links the dependency on #189. It also describes the testing done, including the test command and the local workaround. The template asks for a Database Migration answer (YES/NO), and the description does not state one. The description does not include the 'Fixes #' line, though it gives a related PR reference. The 'Screenshots' section is not needed for this non-UI change. The missing database migration answer is minor, so the description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fba440bd-61d3-4668-903b-bf818d7183d3
Commits

Reviewing files that changed from the base of the PR and between d88d5a4 and fc12598.

Files selected for processing: 2

  • src/Chaptarr.Core.Test/Indexers/MyAnonaMouseWedgeFixture.cs
  • src/NzbDrone.Core/Indexers/MyAnonaMouse/MyAnonaMouse.cs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

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