From 94c6fc21e7c22d1bc872841fe6e5e9c7c73dc715 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sun, 11 Sep 2022 19:46:53 +0100 Subject: [PATCH] Question answer & topic encryption --- src/buttons/edit.js | 6 ++++-- src/lib/tickets/manager.js | 6 ++++-- src/modals/questions.js | 8 +++++--- src/modals/topic.js | 6 ++++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/buttons/edit.js b/src/buttons/edit.js index dc82a10..208e027 100644 --- a/src/buttons/edit.js +++ b/src/buttons/edit.js @@ -8,6 +8,8 @@ const { TextInputStyle, } = require('discord.js'); const emoji = require('node-emoji'); +const Cryptr = require('cryptr'); +const cryptr = new Cryptr(process.env.ENCRYPTION_KEY); module.exports = class EditButton extends Button { constructor(client, options) { @@ -52,7 +54,7 @@ module.exports = class EditButton extends Button { .setMinLength(5) .setPlaceholder(getMessage('modals.topic.placeholder')) .setRequired(true) - .setValue(ticket.topic || ''), + .setValue(ticket.topic ? cryptr.decrypt(ticket.topic) : ''), ), ), ); @@ -79,7 +81,7 @@ module.exports = class EditButton extends Button { .setMinLength(a.question.minLength) .setPlaceholder(a.question.placeholder) .setRequired(a.question.required) - .setValue(a.value || a.question.value), + .setValue(a.value ? cryptr.decrypt(a.value) : a.question.value), ); } else if (a.question.type === 'MENU') { return new ActionRowBuilder() diff --git a/src/lib/tickets/manager.js b/src/lib/tickets/manager.js index cf215d7..17a7618 100644 --- a/src/lib/tickets/manager.js +++ b/src/lib/tickets/manager.js @@ -15,6 +15,8 @@ const emoji = require('node-emoji'); const ms = require('ms'); const ExtendedEmbedBuilder = require('../embed'); const { logTicketEvent } = require('../logging'); +const Cryptr = require('cryptr'); +const cryptr = new Cryptr(process.env.ENCRYPTION_KEY); /** * @typedef {import('@prisma/client').Category & @@ -314,7 +316,7 @@ module.exports = class TicketManager { answers = category.questions.map(q => ({ questionId: q.id, userId: interaction.user.id, - value: interaction.fields.getTextInputValue(q.id), + value: interaction.fields.getTextInputValue(q.id) ? cryptr.encrypt(interaction.fields.getTextInputValue(q.id)) : '', })); if (category.customTopic) topic = interaction.fields.getTextInputValue(category.customTopic); } else if (action === 'topic') { @@ -549,7 +551,7 @@ module.exports = class TicketManager { id: channel.id, number, openingMessageId: sent.id, - topic, + topic: topic ? cryptr.encrypt(topic) : null, }; if (referencesTicketId) data.referencesTicket = { connect: { id: referencesTicketId } }; let message; diff --git a/src/modals/questions.js b/src/modals/questions.js index 3f35db1..6996dbb 100644 --- a/src/modals/questions.js +++ b/src/modals/questions.js @@ -2,6 +2,8 @@ const { Modal } = require('@eartharoid/dbf'); const { EmbedBuilder } = require('discord.js'); const ExtendedEmbedBuilder = require('../lib/embed'); const { logTicketEvent } = require('../lib/logging'); +const Cryptr = require('cryptr'); +const cryptr = new Cryptr(process.env.ENCRYPTION_KEY); module.exports = class QuestionsModal extends Modal { constructor(client, options) { @@ -49,7 +51,7 @@ module.exports = class QuestionsModal extends Modal { data: { questionAnswers: { update: interaction.fields.fields.map(f => ({ - data: { value: f.value }, + data: { value: f.value ? cryptr.encrypt(f.value) : '' }, where: { id: Number(f.customId) }, })), }, @@ -70,7 +72,7 @@ module.exports = class QuestionsModal extends Modal { ticket.questionAnswers .map(a => ({ name: a.question.label, - value: a.value || getMessage('ticket.answers.no_value'), + value: a.value ? cryptr.decrypt(a.value) : getMessage('ticket.answers.no_value'), })), ); await opening.edit({ embeds }); @@ -92,7 +94,7 @@ module.exports = class QuestionsModal extends Modal { const makeDiff = ticket => { const diff = {}; ticket.questionAnswers.forEach(a => { - diff[a.question.label] = a.value || getMessage('ticket.answers.no_value'); + diff[a.question.label] = a.value ? cryptr.decrypt(a.value) : getMessage('ticket.answers.no_value'); }); return diff; }; diff --git a/src/modals/topic.js b/src/modals/topic.js index db3e458..d0bb08d 100644 --- a/src/modals/topic.js +++ b/src/modals/topic.js @@ -2,6 +2,8 @@ const { Modal } = require('@eartharoid/dbf'); const { EmbedBuilder } = require('discord.js'); const ExtendedEmbedBuilder = require('../lib/embed'); const { logTicketEvent } = require('../lib/logging'); +const Cryptr = require('cryptr'); +const cryptr = new Cryptr(process.env.ENCRYPTION_KEY); module.exports = class TopicModal extends Modal { constructor(client, options) { @@ -36,7 +38,7 @@ module.exports = class TopicModal extends Modal { where: { id: interaction.channel.id }, }); const ticket = await client.prisma.ticket.update({ - data: { topic }, + data: { topic: topic ? cryptr.encrypt(topic) : null }, select, where: { id: interaction.channel.id }, }); @@ -70,7 +72,7 @@ module.exports = class TopicModal extends Modal { /** @param {ticket} ticket */ const makeDiff = ticket => { const diff = {}; - diff[getMessage('ticket.opening_message.fields.topic')] = ticket.topic; + diff[getMessage('ticket.opening_message.fields.topic')] = ticket.topic ? cryptr.decrypt(ticket.topic) : ' '; return diff; };