Skip to content

Commit

Permalink
feat: add optional title
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Aug 20, 2022
1 parent cf35fac commit 9257910
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/client/routes/home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
IconCopy,
IconCheck,
IconSettings,
IconHeading,
} from '@tabler/icons';

import Error from '../../components/info/error';
Expand All @@ -37,6 +38,7 @@ import { createSecret, burnSecret } from '../../api/secret';

const Home = () => {
const [text, setText] = useState('');
const [title, setTitle] = useState('');

const [ttl, setTTL] = useState(14400);
const [password, setPassword] = useState('');
Expand Down Expand Up @@ -79,10 +81,14 @@ const Home = () => {
}
}, [enablePassword]);

const onTextareChange = (event) => {
const onTextareaChange = (event) => {
setText(event.target.value);
};

const onTitleChange = (event) => {
setTitle(event.target.value);
};

const onSelectChange = (value) => {
setTTL(value);
};
Expand Down Expand Up @@ -124,6 +130,8 @@ const Home = () => {
event.preventDefault();

formData.append('text', text);

formData.append('title', title);
formData.append('password', password);
formData.append('ttl', ttl);
formData.append('allowedIp', allowedIp);
Expand Down Expand Up @@ -182,12 +190,20 @@ const Home = () => {
maxRows={secretId ? 4 : 1000}
autosize
placeholder="Write your sensitive information.."
onChange={onTextareChange}
onChange={onTextareaChange}
value={text}
readOnly={inputReadOnly}
error={secretError}
/>

<TextInput
icon={<IconHeading />}
placeholder="Title"
value={title}
onChange={onTitleChange}
readOnly={inputReadOnly}
/>

<Group spacing="lg">
<Select
value={ttl}
Expand Down
16 changes: 15 additions & 1 deletion src/client/routes/secret/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { useParams, Link } from 'react-router-dom';
import validator from 'validator';

import { Button, Group, Container, Textarea, TextInput, Stack, Title, Text } from '@mantine/core';
import { IconSquarePlus, IconDownload, IconLock, IconEye, IconPerspective } from '@tabler/icons';
import {
IconSquarePlus,
IconDownload,
IconLock,
IconEye,
IconPerspective,
IconHeading,
} from '@tabler/icons';

import Error from '../../components/info/error';

Expand All @@ -14,6 +21,7 @@ import { downloadFile } from '../../api/upload';
const Secret = () => {
const { secretId, encryptionKey = null } = useParams();
const [secret, setSecret] = useState(null);
const [title, setTitle] = useState(null);
const [isSecretOpen, setIsSecretOpen] = useState(false);
const [password, setPassword] = useState('');
const [isPasswordRequired, setIsPasswordRequired] = useState(false);
Expand Down Expand Up @@ -50,6 +58,10 @@ const Secret = () => {
} else {
setSecret(validator.unescape(json.secret));

if (json.title) {
setTitle(validator.unescape(json.title));
}

if (json.file) {
setFile(json.file);
}
Expand Down Expand Up @@ -111,6 +123,8 @@ const Secret = () => {

<Text>We will only show the secret once.</Text>

{title && <TextInput icon={<IconHeading />} value={title} readOnly />}

{isSecretOpen && (
<Textarea minRows={10} maxRows={30} value={secret} autosize readOnly />
)}
Expand Down
15 changes: 13 additions & 2 deletions src/server/controllers/secret.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ async function getSecretRoute(request, reply) {
Object.assign(result, { file: JSON.parse(data.file) });
}

if (data.title) {
Object.assign(result, { title: data.title });
}

Object.assign(result, { secret: decrypt(JSON.parse(data.secret), encryptionKey).toString() });

redis.deleteSecret(id);
Expand Down Expand Up @@ -79,7 +83,7 @@ async function secret(fastify) {
preValidation: [fastify.rateLimit, fastify.keyGeneration, fastify.attachment],
},
async (req, reply) => {
const { text, ttl, password, allowedIp, preventBurn } = req.body;
const { text, title, ttl, password, allowedIp, preventBurn } = req.body;
const { encryptionKey, secretId, file } = req.secret;

if (Buffer.byteLength(text?.value) > config.get('api.maxTextSize')) {
Expand All @@ -90,14 +94,21 @@ async function secret(fastify) {
});
}

if (title?.value.length > 255) {
return reply.code(413).send({
error: `The title is longer than 255 characters which is not allowed.`,
});
}

if (allowedIp?.value && !ipCheck(allowedIp.value)) {
return reply.code(409).send({ error: 'The IP address is not valid' });
}

const data = {
id: secretId,
title: validator.escape(title?.value),
secret: JSON.stringify(encrypt(validator.escape(text?.value), encryptionKey)),
allowedIp: allowedIp?.value,
allowedIp: validator.escape(allowedIp?.value),
};

if (password?.value) {
Expand Down
4 changes: 4 additions & 0 deletions src/server/services/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ async function createSecret(data, ttl) {
const key = `secret:${data.id}`;
const prepare = [key, 'secret', data.secret];

if (data.title) {
prepare.push(...['title', data.title]);
}

if (data.password) {
prepare.push(...['password', data.password]);
}
Expand Down

0 comments on commit 9257910

Please sign in to comment.