-
Notifications
You must be signed in to change notification settings - Fork 64
feat: ban user if they post link within 10mins of account creation #3531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d2103d2
feat: ban user if they post link within 10mins of account creation
tefkah 3ba66a7
fix: only change links in comments, only add nofollow
tefkah 968d383
fix: apply robo-comments
tefkah fbcd796
fix: check properly
tefkah 1646645
fix: also double check norrmal discussion api
tefkah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
31 changes: 31 additions & 0 deletions
31
client/containers/Pub/PubDocument/PubDiscussions/Discussion/commentEditorMarks.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { DOMOutputSpec, MarkSpec } from 'prosemirror-model'; | ||
|
|
||
| import { baseMarks } from 'components/Editor/schemas/base'; | ||
|
|
||
| // this just adds a rel=nofollow to the link | ||
| const commentLinkMark: MarkSpec = { | ||
| ...baseMarks.link, | ||
| toDOM: (node) => { | ||
| const attrs = { ...node.attrs }; | ||
| const hasInvalidTarget = attrs.target && typeof attrs.target !== 'string'; | ||
|
|
||
| if (hasInvalidTarget) { | ||
| attrs.target = null; | ||
| } | ||
|
|
||
| const { pubEdgeId, ...restAttrs } = attrs; | ||
|
|
||
| return [ | ||
| 'a', | ||
| { | ||
| 'data-pub-edge-id': pubEdgeId, | ||
| ...restAttrs, | ||
| rel: 'nofollow', | ||
| }, | ||
| ] as DOMOutputSpec; | ||
| }, | ||
| }; | ||
|
|
||
| export const commentEditorCustomMarks = { | ||
| link: commentLinkMark, | ||
| }; |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import type { DocJson, NewAccountLinkCommentTriggerSource, UserSpamTagFields } from 'types'; | ||
|
|
||
| import { type Mark, Node } from 'prosemirror-model'; | ||
|
|
||
| import { editorSchema } from 'client/components/Editor'; | ||
| import { SpamTag, User } from 'server/models'; | ||
| import { contextFromUser, notify } from 'server/spamTag/notifications'; | ||
| import { upsertSpamTag } from 'server/spamTag/userQueries'; | ||
|
|
||
| const DEFAULT_NEW_ACCOUNT_LINK_COMMENT_WINDOW_MINUTES = 10; | ||
|
|
||
| const parsedWindowMinutes = parseInt( | ||
| process.env.NEW_ACCOUNT_LINK_COMMENT_WINDOW_MINUTES || | ||
| DEFAULT_NEW_ACCOUNT_LINK_COMMENT_WINDOW_MINUTES.toString(), | ||
| 10, | ||
| ); | ||
|
|
||
| const IS_WINDOW_MINUTES_VALID = Number.isFinite(parsedWindowMinutes) && parsedWindowMinutes > 0; | ||
|
|
||
| const NEW_ACCOUNT_LINK_COMMENT_WINDOW_MINUTES = IS_WINDOW_MINUTES_VALID | ||
| ? parsedWindowMinutes | ||
| : DEFAULT_NEW_ACCOUNT_LINK_COMMENT_WINDOW_MINUTES; | ||
|
|
||
| const NEW_ACCOUNT_LINK_COMMENT_WINDOW_MS = NEW_ACCOUNT_LINK_COMMENT_WINDOW_MINUTES * 60 * 1000; | ||
| const URL_REGEX = /\b(?:https?:\/\/|www\.)[^\s<]+/i; | ||
| const MAX_TRIGGER_VALUE_LENGTH = 500; | ||
|
|
||
| type AutoBanNewAccountLinkCommentOptions = { | ||
| userId: string; | ||
| text: string; | ||
| content: DocJson; | ||
| source: NewAccountLinkCommentTriggerSource; | ||
| }; | ||
|
|
||
| const extractUrlFromString = (value: string): string | null => { | ||
| if (!value) { | ||
| return null; | ||
| } | ||
|
|
||
| const matchedUrl = value.match(URL_REGEX)?.[0]; | ||
| if (!matchedUrl) { | ||
| return null; | ||
| } | ||
|
|
||
| return matchedUrl.slice(0, MAX_TRIGGER_VALUE_LENGTH); | ||
| }; | ||
|
|
||
| const hasValidContentShape = (value: unknown): value is DocJson => { | ||
| if (!value || typeof value !== 'object') { | ||
| return false; | ||
| } | ||
|
|
||
| const content = value as { type?: unknown }; | ||
| return typeof content.type === 'string'; | ||
| }; | ||
|
|
||
| const extractFirstLinkFromContent = (content: DocJson): string | null => { | ||
| if (!hasValidContentShape(content)) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| const contentTree = Node.fromJSON(editorSchema, content); | ||
| const links: Mark[] = []; | ||
|
|
||
| contentTree.descendants((node) => { | ||
| node.marks.forEach((mark) => { | ||
| if (mark.type.name === 'link') { | ||
| links.push(mark); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| const linkFromTree = links[0]?.attrs.href; | ||
| if (typeof linkFromTree !== 'string' || !linkFromTree.length) { | ||
| return null; | ||
| } | ||
|
|
||
| return linkFromTree.slice(0, MAX_TRIGGER_VALUE_LENGTH); | ||
| } catch { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| const getAccountAgeMs = (createdAt: Date | null | undefined): number => { | ||
| if (!(createdAt instanceof Date)) { | ||
| return Number.POSITIVE_INFINITY; | ||
| } | ||
|
|
||
| return Date.now() - createdAt.getTime(); | ||
| }; | ||
|
|
||
| const buildTriggerFields = ( | ||
| linkValue: string, | ||
| source: NewAccountLinkCommentTriggerSource, | ||
| accountAgeMs: number, | ||
| ): UserSpamTagFields => { | ||
| return { | ||
| newAccountLinkCommentTriggers: [ | ||
| { | ||
| source, | ||
| value: linkValue, | ||
| accountAgeMinutes: Math.max(0, Math.floor(accountAgeMs / (60 * 1000))), | ||
| triggeredAt: new Date().toISOString(), | ||
| }, | ||
| ], | ||
| }; | ||
| }; | ||
|
|
||
| export const autoBanForNewAccountLinkComment = async ( | ||
| options: AutoBanNewAccountLinkCommentOptions, | ||
| ): Promise<boolean> => { | ||
| const { userId, text, content, source } = options; | ||
|
|
||
| const user = await User.findOne({ | ||
| where: { id: userId }, | ||
| include: [{ model: SpamTag, as: 'spamTag' }], | ||
| }); | ||
|
|
||
| const accountAgeMs = getAccountAgeMs(user?.createdAt); | ||
| const shouldSkipAutoBan = !user || accountAgeMs > NEW_ACCOUNT_LINK_COMMENT_WINDOW_MS; | ||
|
Comment on lines
+115
to
+121
|
||
|
|
||
| if (shouldSkipAutoBan) { | ||
| return false; | ||
| } | ||
|
|
||
| const linkFromTree = extractFirstLinkFromContent(content); | ||
| const linkFromText = extractUrlFromString(text); | ||
|
|
||
| const firstLink = linkFromTree || linkFromText; | ||
|
|
||
| if (!firstLink) { | ||
| return false; | ||
| } | ||
|
|
||
| const previousStatus = user.spamTag?.status ?? null; | ||
|
|
||
| const fields = buildTriggerFields(firstLink, source, accountAgeMs); | ||
|
|
||
| const { spamTag, user: taggedUser } = await upsertSpamTag({ | ||
| userId, | ||
| status: 'confirmed-spam', | ||
| fields, | ||
| }); | ||
|
|
||
| const shouldNotify = previousStatus !== 'confirmed-spam' && process.env.NODE_ENV !== 'test'; | ||
|
|
||
| if (shouldNotify) { | ||
| await notify( | ||
| 'new-account-link-comment-ban', | ||
| contextFromUser(taggedUser, { | ||
| previousStatus, | ||
| spamFields: spamTag.fields as UserSpamTagFields, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.