Skip to content
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

Add a NSFW protection #520

Merged
merged 6 commits into from
Sep 11, 2024
Merged

Add a NSFW protection #520

merged 6 commits into from
Sep 11, 2024

Conversation

H-Shay
Copy link
Contributor

@H-Shay H-Shay commented Sep 10, 2024

Adds a protection which utilizes https://github.com/infinitered/nsfwjs to determine if images sent into a protected room are NSFW. If so, mjolnir automatically redacts the image.
Also adds a config option to manually set the sensitivity of the protection.

Copy link
Member

@turt2live turt2live left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall lgtm - thanks! We may want more classifiers in the future, and supporting that looks easy.

src/protections/NsfwProtection.ts Outdated Show resolved Hide resolved
src/protections/ProtectionManager.ts Outdated Show resolved Hide resolved
test/integration/nsfwProtectionTest.ts Outdated Show resolved Hide resolved
Comment on lines 51 to 55
const isMedia = msgtype === 'm.image' || formattedBody.toLowerCase().includes('<img');

if (isMedia) {
const mxc = content["url"]
const image = await mjolnir.client.downloadContent(mxc)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break if there is an event with msgtype !== m.image and an img tag in the html? As now content['url'] is undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likely, yea. I think we should just remove the formatted body condition for now - parsing the HTML can be a future PR.

const isMedia = msgtype === 'm.image' || formattedBody.toLowerCase().includes('<img');

if (isMedia) {
const mxc = content["url"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldnt the thumbnail be checked as well? It might be a different image?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, though thumbnails are a bit weird already. This can be revisted in a future PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it helps this is how i've laid out Draupnir's attempt to copy this protection, so that it's possible to scrape out more urls from the message https://github.com/the-draupnir-project/Draupnir/blob/gnuxie/nsfwjs/src/protections/NSFWImageProtection.tsx#L55-L71 (incomplete code though, would like to avoid depending on @tensorflow/tfjs-node )

import { Mjolnir } from "../Mjolnir";
import * as nsfw from 'nsfwjs';
import {LogLevel} from "matrix-bot-sdk";
import { node } from '@tensorflow/tfjs-node';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will pull in over 285MB of dependencies btw, which is more weight than the entire mjolnir docker image.

https://pkg-size.dev/@tensorflow%2Ftfjs-node

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image size isn't really a concern here, but if you're aware of alternatives, we can certainly consider them.

@H-Shay H-Shay merged commit 4488706 into main Sep 11, 2024
5 checks passed
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.

4 participants