Skip to content

fix(reddit): read the subreddit's NSFW flag from the page Reddit serves - #54

Merged
codepurse merged 2 commits into
codepurse:mainfrom
asimovtekir:fix/reddit-nsfw-page-data
Nov 20, 2026
Merged

codepurse merged 2 commits into
codepurse:mainfrom
asimovtekir:fix/reddit-nsfw-page-data

Conversation

@asimovtekir

@asimovtekir asimovtekir commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The issue

With Smart blocking on and no Reddit sign-in, https://www.reddit.com/r/gonewild/ loads instead of redirecting to the blocked page (reddit_nsfw).

checkRedditSubredditNSFW() asks /r/<name>/about.json. Reddit now refuses that address to signed-out visitors with an HTML 403, "You've been blocked by network security". The refusal applies to every subreddit, SFW or not. The code read any non-OK response as "not NSFW":

if (!response.ok) { ... return false; }
Request, signed out Result
/r/gonewild/about.json 403 "blocked by network security"
the same, without the extension's User-Agent header 403
/r/pics/about.json (SFW) 403
old.reddit.com/r/gonewild/about.json 302 → /login/?reason=lor2
/r/gonewild/ (HTML) 200, after a JS challenge (?js_challenge=1&jsc_token=…)

So the check could never get an answer. Reddit does give one in the page it serves:

<reddit-page-data data='{"subreddit":{"id":"t5_2qq5c","isNsfw":true,…}}'>
<shreddit-screenview-data data='{"subreddit":{"id":"t5_2qq5c","name":"gonewild",…}}'>

A second defect sat on the same path. SOCIAL_SELECTORS.reddit matched [data-testid="post-container"] and .Post, which current Reddit no longer renders. Posts are <shreddit-post>, marked with an nsfw attribute when Reddit counts them as NSFW. On the page above, 0 of 28 posts marked nsfw were hidden.

The fix (content.js)

  • readRedditPageNsfw(name, document) returns subreddit.isNsfw from <reddit-page-data>, with no request. It is trusted only when it names the subreddit asked about, either directly or via <shreddit-screenview-data> carrying the same subreddit id. Otherwise it returns null, so data left behind by an in-app navigation is never borrowed. It also returns null while the element hasn't parsed or is malformed.
  • checkRedditSubredditNSFW() reads the page first when the subreddit asked about is the current page, then falls back to about.json. It now returns null when Reddit hasn't answered: a 403/429, a network error, or a reply without over18. Unknown never blocks and is never cached. A 403/429 pauses about.json for 10 minutes, because Reddit refuses the client, not one subreddit, and a feed would otherwise send a refused request for every post. shouldBlockRedditPage() blocks only on === true.
  • recheckRedditPage(). processContent() runs its Reddit check at document_start, before <reddit-page-data> has parsed. It also skips if a previous pass is still running when DOMContentLoaded's rerun arrives. The recheck runs on its own on DOMContentLoaded, and whenever the MutationObserver sees a <reddit-page-data> added (a tag-name compare, free on other sites). It respects the whitelist, smart blocking and the on/off switch, and does nothing if the address changed meanwhile.
  • Posts: shreddit-post is added to SOCIAL_SELECTORS.reddit, and shouldBlockSocialPost() hides a post that carries the nsfw attribute. The old selectors are kept as fallbacks.

about.json is kept, because it still answers when Reddit allows it (for example, signed-in sessions) and for subreddits linked from posts.

Testing

  • Chrome build tested
  • Firefox build tested
  • Popup checked
  • Options page checked
  • Target feature manually verified (Firefox; Chrome see below)

Both bundles were built from this branch with the steps of build-chrome.ps1 / build-firefox.ps1 (open-source build; build-dnr-ruleset.mjs --check passed), loaded into fresh profiles and driven by script (Firefox over WebDriver BiDi, Chrome for Testing over CDP). Signed out of Reddit. Extension pages were screenshotted; Reddit pages were checked by final URL and <reddit-page-data> text only.

Check Firefox 158.0.1 Chrome for Testing 158.0.8127.36 (headless)
Build installs / service worker runs ✅ ✅
Onboarding opens on install ✅ ✅
popup.html renders, no console errors ✅ ✅
options.html renders, no console errors ✅ ✅
r/pics (SFW) stays open ✅ ✅
r/gonewild → blocked.html?reason=reddit_nsfw ✅ (main: ❌ stays open) ⚠️ not testable headless — Reddit serves headless Chrome a page without <reddit-page-data> (r/pics too)
No BlockNSFW console errors on Reddit ✅ (main: ❌ NetworkError) ✅

Running the built extension found two things, fixed in the second commit:

  • <reddit-page-data> names the subreddit prefixedName: "r/<name>", not name; the first commit only matched because <shreddit-screenview-data> was also present. Both are now read.
  • In Firefox the content script's about.json request fails as a NetworkError rather than a 403 (on main too, where it printed a console error on every Reddit page). It now pauses about.json like a 403 and logs only in debug mode — otherwise the newly matched <shreddit-post>s would each send a failing request.

Automated: tests/reddit-nsfw.test.js, 16 tests, running the real functions from content.js in a vm: page data answering (name, prefixedName, or via screenview with the same id); name mismatch / stale / malformed / missing / nameless data → no answer; 403 → unknown, not cached, one request per backoff; network error → same pause, nothing logged; the page answering after a refusal; a 200 answer cached; no over18 → unknown; a link to another subreddit not borrowing the page's answer; smart blocking off; the recheck redirecting and leaving SFW, home and non-Reddit pages alone; the DOMContentLoaded and observer triggers; <shreddit-post nsfw>. npm test: 1425/1425.

Still to verify by hand: the r/gonewild redirect in a regular (non-headless) Chrome.

Privacy / Network Impact

  • No privacy/network behavior change
  • Privacy/network behavior changed and docs were updated

There are fewer Reddit requests, not more. The page's own flag is read locally, and about.json is skipped for 10 minutes after a refusal. PRIVACY_POLICY.md §5 says so. CHANGELOG.md and changelog.html have an entry under 2.0.8 → Fixed.

Notes

  • Not in this PR: the AI Image Blocker (nsfwjs) also missed this content. All 10 revealed full-size images on the page scored allow, with highest Porn+Hentai 0.40 and Sexy 0.67, under both the Balanced (0.60 / 0.90) and Strict (0.45 / 0.80) bars. That's a model or recall question, separate from this fix.
  • Unchanged: subreddits reached by in-app pushState navigation from the Reddit home feed. The observer trigger covers it if Reddit renders a new <reddit-page-data> on navigation. That hasn't been verified.

🤖 Generated with Claude Code

asimovtekir and others added 2 commits November 19, 2026 17:41
NSFW subreddits opened unblocked for signed-out visitors. The check asked
/r/<name>/about.json; Reddit now answers that with an HTML 403 ("You've
been blocked by network security") for every subreddit, SFW or not, and
the 403 was read as "not NSFW". Its HTML pages are served after a JS
challenge; its JSON endpoints are refused outright.

- readRedditPageNsfw(): the answer from <reddit-page-data> (subreddit.isNsfw),
  trusted only when it names the subreddit asked about (directly or via
  <shreddit-screenview-data> with the same id), so data left behind by an
  in-app navigation is not borrowed. No request is made.
- checkRedditSubredditNSFW() returns null, not false, when Reddit has not
  answered (403/429, network error, no over18). Unknown never blocks and is
  never cached. A 403/429 pauses about.json for 10 minutes: Reddit refuses
  the client, not one subreddit, and a feed would otherwise send a refused
  request per post.
- recheckRedditPage(): processContent's check runs at document_start, before
  the page data parses, and skips while a pass is running. Asked again on
  DOMContentLoaded and whenever a <reddit-page-data> is added.
- SOCIAL_SELECTORS.reddit matches <shreddit-post>, and its `nsfw` attribute
  hides the post. The old selectors matched no element on current Reddit.

Tests: tests/reddit-nsfw.test.js (14).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…equest

Found running the built extension in Firefox 158:

- Reddit's <reddit-page-data> names the subreddit as `prefixedName`
  ("r/pics"), not `name`. The redirect worked only because
  <shreddit-screenview-data> happened to be on the page too.
- Firefox's content script gets Reddit's about.json refusal as a
  NetworkError, not a 403 (on main as well). The catch returned null but
  did not pause, so with <shreddit-post> now matched, every post linking a
  subreddit would send a failing request and print a console error. It now
  pauses like a 403, and logs only in debug mode.

Tests: tests/reddit-nsfw.test.js (16).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codepurse

Copy link
Copy Markdown
Owner

Thanks for this, and for the clear write-up. I reviewed and tested it before merging:

  • npm test: 1425/1425 pass.
  • I built both bundles from this branch and compared them with main (same build, only content.js different), signed out of Reddit, in Chrome 157 and Firefox 160:
Page main This PR
r/pics open open
r/gonewild open blocked (reddit_nsfw)
r/OnOff open blocked (reddit_nsfw)
  • On a test page, a <shreddit-post nsfw> was hidden and a plain post was left alone.

Tip for Chrome: headless works once the user agent doesn't say HeadlessChrome. Reddit then serves <reddit-page-data> the same way it does to Firefox.

Merging. 2.0.8 is already built, so I'll move the changelog entry myself.

@codepurse
codepurse merged commit 4ff1a27 into codepurse:main Nov 20, 2026
codepurse added a commit that referenced this pull request Nov 28, 2026
2.0.8 is live on Firefox and Chrome, so the Christmas note ships as
2.0.9, with the Reddit fix that was merged after 2.0.8 was packaged.

- Both manifests, package.json and the README say 2.0.9.
- CHANGELOG.md: Unreleased becomes 2.0.9, dated 2026-11-28. The Reddit
  entry (PR #54) moves there from 2.0.8, whose packages didn't carry it.
- changelog.html gains a 2.0.9 section, marked Current, with the
  Christmas note and the Reddit fix; 2.0.8 loses the mark and the entry.
- data/version.json stays held until the stores carry the new version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants