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
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,8 @@ pollReports: false
# Whether or not new reports, received either by webapi or polling,
# should be printed to our managementRoom.
displayReports: true

# How sensitive the NsfwProtection should be, which determines if an image should be redacted. A number between 0 - .99,
# with a lower number indicating greater sensitivity, possibly resulting in images being more aggressively flagged
# and redacted as NSFW
nsfwSensitivity: .6
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"dependencies": {
"@sentry/node": "^7.17.2",
"@sentry/tracing": "^7.17.2",
"@tensorflow/tfjs-node": "^4.21.0",
"await-lock": "^2.2.2",
"body-parser": "^1.20.1",
"config": "^3.3.8",
Expand All @@ -56,6 +57,7 @@
"js-yaml": "^4.1.0",
"jsdom": "^16.6.0",
"matrix-appservice-bridge": "8.1.2",
"nsfwjs": "^4.1.0",
"parse-duration": "^1.0.2",
"pg": "^8.8.0",
"prom-client": "^14.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface IConfig {
enabled: boolean;
}
}
nsfwSensitivity: number

/**
* Config options only set at runtime. Try to avoid using the objects
Expand Down Expand Up @@ -248,7 +249,7 @@ const defaultConfig: IConfig = {
enabled: false,
},
},

nsfwSensitivity: .6,
// Needed to make the interface happy.
RUNTIME: {
},
Expand Down
85 changes: 85 additions & 0 deletions src/protections/NsfwProtection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2024 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Protection } from "./IProtection";
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.



export class NsfwProtection extends Protection {
settings = {};
// @ts-ignore
private model: any;

constructor() {
super();
}

async initialize() {
this.model = await nsfw.load();
}

public get name(): string {
return 'NsfwProtection';
}

public get description(): string {
return "Scans all images sent into a protected room to determine if the image is " +
"NSFW. If it is, the image will automatically be redacted.";
}

public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<any> {
if (event['type'] === 'm.room.message') {
const content = event['content'] || {};
const msgtype = content['msgtype'] || 'm.text';
const formattedBody = content['formatted_body'] || '';
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 )

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 decodedImage = await node.decodeImage(image.data, 3);
const predictions = await this.model.classify(decodedImage)

for (const prediction of predictions) {
if (prediction["className"] === "Porn") {
if (prediction["probability"] > mjolnir.config.nsfwSensitivity) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.INFO, "NSFWProtection", `Redacting ${event["event_id"]} for inappropriate content.`);
try {
mjolnir.client.redactEvent(roomId, event["event_id"])
} catch (err) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "NSFWProtection", `There was an error redacting ${event["event_id"]}: ${err}`);

}
}
} else if (prediction["className"] === "Hentai") {
if (prediction["probability"] > mjolnir.config.nsfwSensitivity) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.INFO, "NSFWProtection", `Redacting ${event["event_id"]} for inappropriate content.`);
try {
mjolnir.client.redactEvent(roomId, event["event_id"])
} catch (err) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "NSFWProtection", `There was an error redacting ${event["event_id"]}: ${err}`);
}
}
}
turt2live marked this conversation as resolved.
Show resolved Hide resolved
}
decodedImage.dispose()
}
}
}
}
5 changes: 5 additions & 0 deletions src/protections/ProtectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { htmlEscape } from "../utils";
import { ERROR_KIND_FATAL, ERROR_KIND_PERMISSION } from "../ErrorCache";
import { RoomUpdateError } from "../models/RoomUpdateError";
import { LocalAbuseReports } from "./LocalAbuseReports";
import {NsfwProtection} from "./NsfwProtection";

const PROTECTIONS: Protection[] = [
new FirstMessageIsImage(),
Expand All @@ -42,6 +43,7 @@ const PROTECTIONS: Protection[] = [
new DetectFederationLag(),
new JoinWaveShortCircuit(),
new LocalAbuseReports(),
new NsfwProtection()
];

const ENABLED_PROTECTIONS_EVENT_TYPE = "org.matrix.mjolnir.enabled_protections";
Expand Down Expand Up @@ -100,6 +102,9 @@ export class ProtectionManager {
protection.settings[key].setValue(value);
}
if (protection.enabled) {
if (protection.name === "NsfwProtection") {
(protection as NsfwProtection).initialize()
}
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
for (let roomId of this.mjolnir.protectedRoomsTracker.getProtectedRooms()) {
await protection.startProtectingRoom(this.mjolnir, roomId);
}
Expand Down
1 change: 0 additions & 1 deletion test/integration/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { read as configRead } from "../../src/config";
import { makeMjolnir, teardownManagementRoom } from "./mjolnirSetupUtils";
import { register } from "prom-client";
import dns from 'node:dns';

// Necessary for CI: Node 17+ defaults to using ipv6 first, but Github Actions does not support ipv6
Expand Down
66 changes: 66 additions & 0 deletions test/integration/nsfwProtectionTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {newTestUser} from "./clientHelper";

import {MatrixClient} from "matrix-bot-sdk";
import {getFirstReaction} from "./commands/commandUtils";
import {strict as assert} from "assert";
import { readFileSync } from 'fs';

describe("Test: NSFW protection", function () {
let client: MatrixClient;
let room: string;
this.beforeEach(async function () {
client = await newTestUser(this.config.homeserverUrl, {name: {contains: "nsfw-protection"}});
await client.start();
const mjolnirId = await this.mjolnir.client.getUserId();
room = await client.createRoom({ invite: [mjolnirId] });
await client.joinRoom(room);
await client.joinRoom(this.config.managementRoom);
await client.setUserPowerLevel(mjolnirId, room, 100)
})
this.afterEach(async function () {
await client.stop();
})

function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}


it("Nsfw protection doesn't redact sfw images", async function() {
this.timeout(20000);

await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir enable NsfwProtection` });
});

const data = readFileSync('test_tree.jpg')
const mxc = await client.uploadContent(data, 'image/png')
let content = {"msgtype": "m.image", "body": "test.jpeg", "url": mxc}
let imageMessage = await client.sendMessage(room, content)

await delay(500)
let processedImage = await client.getEvent(room, imageMessage);
assert.equal(Object.keys(processedImage.content).length, 3, "This event should not have been redacted");
});

it("Nsfw protection redacts nsfw images", async function() {
this.timeout(20000);
// dial the sensitivity on the protection way up so that all images are flagged as NSFW
this.mjolnir.config.nsfwSensitivity = 0.0
H-Shay marked this conversation as resolved.
Show resolved Hide resolved

await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir enable NsfwProtection` });
});

const data = readFileSync('test_tree.jpg')
const mxc = await client.uploadContent(data, 'image/png')
let content = {"msgtype": "m.image", "body": "test.jpeg", "url": mxc}
let imageMessage = await client.sendMessage(room, content)

await delay(500)
let processedImage = await client.getEvent(room, imageMessage);
assert.equal(Object.keys(processedImage.content).length, 0, "This event should have been redacted");
});
});
Binary file added test_tree.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"skipLibCheck": true,
"alwaysStrict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
Expand Down
Loading
Loading