Repository navigation
fix(reddit): read the subreddit's NSFW flag from the page Reddit serves - #54
Merged
Merged
Conversation
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>
Owner
|
Thanks for this, and for the clear write-up. I reviewed and tested it before merging:
Tip for Chrome: headless works once the user agent doesn't say Merging. 2.0.8 is already built, so I'll move the changelog entry myself. |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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":/r/gonewild/about.jsonUser-Agentheader/r/pics/about.json(SFW)old.reddit.com/r/gonewild/about.json/login/?reason=lor2/r/gonewild/(HTML)?js_challenge=1&jsc_token=…)So the check could never get an answer. Reddit does give one in the page it serves:
A second defect sat on the same path.
SOCIAL_SELECTORS.redditmatched[data-testid="post-container"]and.Post, which current Reddit no longer renders. Posts are<shreddit-post>, marked with annsfwattribute when Reddit counts them as NSFW. On the page above, 0 of 28 posts markednsfwwere hidden.The fix (
content.js)readRedditPageNsfw(name, document)returnssubreddit.isNsfwfrom<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 returnsnull, so data left behind by an in-app navigation is never borrowed. It also returnsnullwhile 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 toabout.json. It now returnsnullwhen Reddit hasn't answered: a 403/429, a network error, or a reply withoutover18. Unknown never blocks and is never cached. A 403/429 pausesabout.jsonfor 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 atdocument_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.shreddit-postis added toSOCIAL_SELECTORS.reddit, andshouldBlockSocialPost()hides a post that carries thensfwattribute. The old selectors are kept as fallbacks.about.jsonis kept, because it still answers when Reddit allows it (for example, signed-in sessions) and for subreddits linked from posts.Testing
Both bundles were built from this branch with the steps of
build-chrome.ps1/build-firefox.ps1(open-source build;build-dnr-ruleset.mjs --checkpassed), 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.popup.htmlrenders, no console errorsoptions.htmlrenders, no console errorsblocked.html?reason=reddit_nsfwmain: ❌ stays open)<reddit-page-data>(r/pics too)main: ❌NetworkError)Running the built extension found two things, fixed in the second commit:
<reddit-page-data>names the subredditprefixedName: "r/<name>", notname; the first commit only matched because<shreddit-screenview-data>was also present. Both are now read.about.jsonrequest fails as aNetworkErrorrather than a 403 (onmaintoo, where it printed a console error on every Reddit page). It now pausesabout.jsonlike 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 fromcontent.jsin avm: 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; noover18→ 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
There are fewer Reddit requests, not more. The page's own flag is read locally, and
about.jsonis skipped for 10 minutes after a refusal.PRIVACY_POLICY.md§5 says so.CHANGELOG.mdandchangelog.htmlhave an entry under 2.0.8 → Fixed.Notes
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.pushStatenavigation 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