Skip to content

Commit

Permalink
feat: Disable embedding/crawling on URLs (#28)
Browse files Browse the repository at this point in the history
Controlled with `config.discord.disableUrlEmbeds`, defaulted to `true`.

Also fixes bug where `;config set ...` wouldn't redact SECRET_KEYS
when returning the new config.
  • Loading branch information
edfletcher committed Dec 29, 2023
1 parent 9004c98 commit aeda423
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const _config = {
},

discord: {
disableUrlEmbeds: true, // wraps any URLs detected in angle-brackets (<>) before sending to Discord
userScriptOutputChannelId: '',
privMsgChannelStalenessTimeMinutes: 720,
privMsgChannelStalenessRemovalAlert: 0.1, // remaining of privMsgChannelStalenessTimeMinutes
Expand Down Expand Up @@ -175,7 +176,7 @@ const _config = {
enabled: false,
port: 4242,
proto: 'https',
host: "localhost",
host: 'localhost',
fqdn: os.hostname(),
ttlSecs: 30 * 60,
staticDir: HTTP_STATIC_DIR,
Expand Down
5 changes: 3 additions & 2 deletions discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
scopedRedisClient,
fmtDuration
} = require('./util');
const { wrapUrls } = require('./lib/wrapUrls');

require('./logger')('discord');
require('./lib/promRedisExport')('discord');
Expand Down Expand Up @@ -335,7 +336,7 @@ client.once('ready', async () => {
}

if (!raw) {
s = replaceIrcEscapes(s);
s = wrapUrls(replaceIrcEscapes(s));
}

toSend = formatForAllowedSpeakerResponse(s, raw);
Expand Down Expand Up @@ -447,7 +448,7 @@ client.once('ready', async () => {
const privMsg = '_(Only visible to you)_';

if (!raw) {
msgFormatted = `${privMsg} ${msgFormatted}`;
msgFormatted = `${privMsg} ${!chan.__drcSend ? wrapUrls(msgFormatted) : msgFormatted}`;
}

await _realSender(msgFormatted, raw);
Expand Down
5 changes: 3 additions & 2 deletions discord/ipcMessages/irc-channelJoined.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
replaceIrcEscapes,
scopedRedisClient
} = require('../../util');
const { wrapUrls } = require('../../lib/wrapUrls');

const { AvatarGenerators, createAvatarName } = require('../avatarGenerators');

Expand Down Expand Up @@ -40,7 +41,7 @@ module.exports = async function (parsed, context) {
}

msgChan.__drcSend = (s) => {
msgChan.send(s)
msgChan.send(wrapUrls(s))
.catch((err) => console.error(`send on ${joined.channel} failed! "${err.message}"`, err.stack, parsed, JSON.stringify(err)));
};

Expand Down Expand Up @@ -167,7 +168,7 @@ module.exports = async function (parsed, context) {
}
}

let content = replaceIrcEscapes(e.message).trim();
let content = wrapUrls(replaceIrcEscapes(e.message).trim());

if (e.type === 'action') {
content = `_${content}_`;
Expand Down
4 changes: 3 additions & 1 deletion discord/ipcMessages/irc-notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
persistPmChan
} = require('../common');
const { MessageEmbed, MessageActionRow, MessageButton } = require('discord.js');
const { wrapUrls } = require('../../lib/wrapUrls');

module.exports = async function (parsed, context) {
const {
Expand Down Expand Up @@ -150,7 +151,8 @@ module.exports = async function (parsed, context) {
}

const msChar = config.user.monospacePrivmsgs ? '`' : '';
const msg = msChar + isIgnored + e.message + isIgnored + msChar;
const msg = msChar + isIgnored + wrapUrls(e.message) + isIgnored + msChar;

if (msg.length && !e.message.match(/^\s*$/g)) {
try {
await client.channels.cache.get(chanId)?.send(msg);
Expand Down
9 changes: 6 additions & 3 deletions discord/userCommands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ const config = require('config');
const { PREFIX } = require('../../util');
const { formatKVs } = require('../common');

function redactedConfig () {
return config._replace(Object.assign({}, config), config._secretKeys, '*<REDACTED>*');
}

async function userCommandConfig (context, ...a) {
const key = PREFIX + ':userConfig';

switch (a[0]) {
case 'get':
return '\n\n' + formatKVs(_.get(
config._replace(Object.assign({}, config), config._secretKeys, '*<REDACTED>*'), a[1]));
return '\n\n' + formatKVs(_.get(redactedConfig(), a[1]));

case 'set':
{
Expand All @@ -31,7 +34,7 @@ async function userCommandConfig (context, ...a) {
await context.redis.set(key, JSON.stringify(config.user));
}

return '\n' + formatKVs(_.get(config, ppathArr.length ? ppathArr.join('.') : a[1]));
return '\n' + formatKVs(_.get(redactedConfig(), ppathArr.length ? ppathArr.join('.') : a[1]));
}

case 'load':
Expand Down
2 changes: 1 addition & 1 deletion http.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ redisListener.subscribe(PREFIX, (err) => {
app.close();
});

app.listen({host: config.http.host, port: config.http.port}, (err, addr) => {
app.listen({ host: config.http.host, port: config.http.port }, (err, addr) => {
if (err) {
throw err;
}
Expand Down
18 changes: 18 additions & 0 deletions lib/wrapUrls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const extractUrls = require('extract-urls');
const config = require('config');

function wrapUrls (text, wrapPrefix = '<', wrapPostfix = '>', checkIfEnabled = true) {
if (typeof (text) !== 'string') {
return text;
}

if (checkIfEnabled && !config.discord.disableUrlEmbeds) {
return text;
}

return extractUrls(text)?.reduce((ostr, url) => ostr.replace(url, `${wrapPrefix}${url}${wrapPostfix}`), text) ?? text;
}

module.exports = {
wrapUrls
};
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"discord-api-types": "^0.37.24",
"discord.js": "^13.3.1",
"eslint": "^7.32.0",
"extract-urls": "^1.4.0",
"fastify": "^3.29.4",
"inquirer": "^8.2.0",
"ioredis": "^5.3.0",
Expand Down

0 comments on commit aeda423

Please sign in to comment.