mirror of https://github.com/MaxLeiter/Drift
Merge pull request #40 from maxall41/better-validation
server: Better validation using celebratepull/52/head
commit
056a2bd3ce
@ -1,57 +1,72 @@
|
|||||||
import { Router } from 'express'
|
import { celebrate, Joi } from "celebrate";
|
||||||
import secretKey from '../lib/middleware/secret-key';
|
import { Router } from "express";
|
||||||
import { File } from '../lib/models/File'
|
import { File } from "@lib/models/File";
|
||||||
|
import secretKey from "@lib/middleware/secret-key";
|
||||||
|
|
||||||
export const files = Router()
|
export const files = Router();
|
||||||
|
|
||||||
files.get("/raw/:id", secretKey, async (req, res, next) => {
|
files.get("/raw/:id",
|
||||||
|
celebrate({
|
||||||
|
params: {
|
||||||
|
id: Joi.string().required(),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
secretKey,
|
||||||
|
async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const file = await File.findOne({
|
const file = await File.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: req.params.id
|
id: req.params.id
|
||||||
},
|
},
|
||||||
attributes: ["title", "content"],
|
attributes: ["title", "content"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
if (!file) {
|
return res.status(404).json({ error: "File not found" })
|
||||||
return res.status(404).json({ error: "File not found" })
|
}
|
||||||
}
|
|
||||||
|
// TODO: JWT-checkraw files
|
||||||
// TODO: JWT-checkraw files
|
if (file?.post?.visibility === "private") {
|
||||||
if (file?.post?.visibility === "private") {
|
// jwt(req as UserJwtRequest, res, () => {
|
||||||
// jwt(req as UserJwtRequest, res, () => {
|
// res.json(file);
|
||||||
// res.json(file);
|
// })
|
||||||
// })
|
res.json(file);
|
||||||
res.json(file);
|
} else {
|
||||||
} else {
|
res.json(file);
|
||||||
res.json(file);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
files.get("/html/:id", async (req, res, next) => {
|
files.get("/html/:id",
|
||||||
|
celebrate({
|
||||||
|
params: {
|
||||||
|
id: Joi.string().required(),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const file = await File.findOne({
|
const file = await File.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: req.params.id
|
id: req.params.id
|
||||||
},
|
},
|
||||||
attributes: ["html"],
|
attributes: ["html"],
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return res.status(404).json({ error: "File not found" })
|
return res.status(404).json({ error: "File not found" })
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'text/plain')
|
res.setHeader('Content-Type', 'text/plain')
|
||||||
res.setHeader('Cache-Control', 'public, max-age=4800')
|
res.setHeader('Cache-Control', 'public, max-age=4800')
|
||||||
res.status(200).write(file.html)
|
res.status(200).write(file.html)
|
||||||
res.end()
|
res.end()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
export { auth } from './auth';
|
export { auth } from "./auth";
|
||||||
export { posts } from './posts';
|
export { posts } from "./posts";
|
||||||
export { users } from './users';
|
export { users } from "./users";
|
||||||
export { files } from './files';
|
export { files } from "./files";
|
||||||
|
|||||||
@ -1,182 +1,209 @@
|
|||||||
import { Router } from 'express'
|
import { Router } from "express";
|
||||||
// import { Movie } from '../models/Post'
|
import { celebrate, Joi } from "celebrate";
|
||||||
import { File } from '../lib/models/File'
|
import { File } from '@lib/models/File'
|
||||||
import { Post } from '../lib/models/Post';
|
import { Post } from '@lib/models/Post';
|
||||||
import jwt, { UserJwtRequest } from '../lib/middleware/jwt';
|
import jwt, { UserJwtRequest } from '@lib/middleware/jwt';
|
||||||
import * as crypto from "crypto";
|
import * as crypto from "crypto";
|
||||||
import { User } from '../lib/models/User';
|
import { User } from '@lib/models/User';
|
||||||
import secretKey from '../lib/middleware/secret-key';
|
import secretKey from '@lib/middleware/secret-key';
|
||||||
import markdown from '../lib/render-markdown';
|
import markdown from '@lib/render-markdown';
|
||||||
|
|
||||||
export const posts = Router()
|
export const posts = Router();
|
||||||
|
|
||||||
posts.post('/create', jwt, async (req, res, next) => {
|
const postVisibilitySchema = (value: string) => {
|
||||||
|
if (value === 'public' || value === 'private') {
|
||||||
|
return value;
|
||||||
|
} else {
|
||||||
|
throw new Error('Invalid post visibility');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
posts.post(
|
||||||
|
"/create",
|
||||||
|
jwt,
|
||||||
|
celebrate({
|
||||||
|
body: {
|
||||||
|
title: Joi.string().required(),
|
||||||
|
files: Joi.any().required(),
|
||||||
|
visibility: Joi.string().custom(postVisibilitySchema, 'valid visibility').required(),
|
||||||
|
userId: Joi.string().required(),
|
||||||
|
password: Joi.string().optional(),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
if (!req.body.files) {
|
let hashedPassword: string = ''
|
||||||
throw new Error("Please provide files.")
|
if (req.body.visibility === 'protected') {
|
||||||
}
|
hashedPassword = crypto.createHash('sha256').update(req.body.password).digest('hex');
|
||||||
|
}
|
||||||
if (!req.body.title) {
|
|
||||||
throw new Error("Please provide a title.")
|
const newPost = new Post({
|
||||||
}
|
title: req.body.title,
|
||||||
|
visibility: req.body.visibility,
|
||||||
if (!req.body.userId) {
|
password: hashedPassword,
|
||||||
throw new Error("No user id provided.")
|
})
|
||||||
}
|
|
||||||
|
await newPost.save()
|
||||||
if (!req.body.visibility) {
|
await newPost.$add('users', req.body.userId);
|
||||||
throw new Error("Please provide a visibility.")
|
const newFiles = await Promise.all(req.body.files.map(async (file) => {
|
||||||
}
|
const html = getHtmlFromFile(file);
|
||||||
|
const newFile = new File({
|
||||||
if (req.body.visibility === 'protected' && !req.body.password) {
|
title: file.title,
|
||||||
throw new Error("Please provide a password.")
|
content: file.content,
|
||||||
}
|
sha: crypto.createHash('sha256').update(file.content).digest('hex').toString(),
|
||||||
|
html
|
||||||
let hashedPassword: string = ''
|
|
||||||
if (req.body.visibility === 'protected') {
|
|
||||||
hashedPassword = crypto.createHash('sha256').update(req.body.password).digest('hex');
|
|
||||||
}
|
|
||||||
|
|
||||||
const newPost = new Post({
|
|
||||||
title: req.body.title,
|
|
||||||
visibility: req.body.visibility,
|
|
||||||
password: hashedPassword,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
await newPost.save()
|
await newFile.$set("user", req.body.userId);
|
||||||
await newPost.$add('users', req.body.userId);
|
await newFile.$set("post", newPost.id);
|
||||||
const newFiles = await Promise.all(req.body.files.map(async (file) => {
|
await newFile.save();
|
||||||
const renderAsMarkdown = ['markdown', 'md', 'mdown', 'mkdn', 'mkd', 'mdwn', 'mdtxt', 'mdtext', 'text', '']
|
return newFile;
|
||||||
const fileType = () => {
|
}))
|
||||||
const pathParts = file.title.split(".")
|
|
||||||
const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : ""
|
|
||||||
return language
|
|
||||||
}
|
|
||||||
const type = fileType()
|
|
||||||
let contentToRender: string = (file.content || '');
|
|
||||||
|
|
||||||
if (!renderAsMarkdown.includes(type)) {
|
await Promise.all(newFiles.map((file) => {
|
||||||
contentToRender =
|
newPost.$add("files", file.id);
|
||||||
`~~~${type}
|
newPost.save();
|
||||||
${file.content}
|
}))
|
||||||
~~~`
|
|
||||||
} else {
|
res.json(newPost);
|
||||||
contentToRender = '\n' + file.content;
|
|
||||||
}
|
|
||||||
const html = markdown(contentToRender)
|
|
||||||
const newFile = new File({
|
|
||||||
title: file.title,
|
|
||||||
content: file.content,
|
|
||||||
sha: crypto.createHash('sha256').update(file.content).digest('hex').toString(),
|
|
||||||
html
|
|
||||||
})
|
|
||||||
|
|
||||||
await newFile.$set("user", req.body.userId);
|
|
||||||
await newFile.$set("post", newPost.id);
|
|
||||||
await newFile.save();
|
|
||||||
return newFile;
|
|
||||||
}))
|
|
||||||
|
|
||||||
await Promise.all(newFiles.map((file) => {
|
|
||||||
newPost.$add("files", file.id);
|
|
||||||
newPost.save();
|
|
||||||
}))
|
|
||||||
|
|
||||||
res.json(newPost);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
posts.get("/", secretKey, async (req, res, next) => {
|
posts.get("/", secretKey, async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const posts = await Post.findAll({
|
const posts = await Post.findAll({
|
||||||
attributes: ["id", "title", "visibility", "createdAt"],
|
attributes: ["id", "title", "visibility", "createdAt"],
|
||||||
})
|
})
|
||||||
res.json(posts);
|
|
||||||
} catch (e) {
|
res.json(posts);
|
||||||
next(e);
|
} catch (e) {
|
||||||
}
|
next(e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
posts.get("/mine", jwt, secretKey, async (req: UserJwtRequest, res, next) => {
|
posts.get("/mine", jwt, secretKey, async (req: UserJwtRequest, res, next) => {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
return res.status(401).json({ error: "Unauthorized" })
|
return res.status(401).json({ error: "Unauthorized" })
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const user = await User.findByPk(req.user.id, {
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
model: Post,
|
||||||
|
as: "posts",
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
model: File,
|
||||||
|
as: "files"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({ error: "User not found" })
|
||||||
}
|
}
|
||||||
|
return res.json(user.posts?.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()))
|
||||||
|
} catch (error) {
|
||||||
|
next(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
posts.get(
|
||||||
|
"/:id",
|
||||||
|
celebrate({
|
||||||
|
params: {
|
||||||
|
id: Joi.string().required(),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
async (req: UserJwtRequest, res, next) => {
|
||||||
try {
|
try {
|
||||||
const user = await User.findByPk(req.user.id, {
|
const post = await Post.findOne({
|
||||||
include: [
|
where: {
|
||||||
{
|
id: req.params.id,
|
||||||
model: Post,
|
},
|
||||||
as: "posts",
|
include: [
|
||||||
include: [
|
{
|
||||||
{
|
model: File,
|
||||||
model: File,
|
as: "files",
|
||||||
as: "files"
|
attributes: [
|
||||||
}
|
"id",
|
||||||
]
|
"title",
|
||||||
},
|
"content",
|
||||||
|
"sha",
|
||||||
|
"createdAt",
|
||||||
|
"updatedAt",
|
||||||
],
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
model: User,
|
||||||
|
as: "users",
|
||||||
|
attributes: ["id", "username"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!post) {
|
||||||
|
return res.status(404).json({ error: "Post not found" })
|
||||||
|
}
|
||||||
|
|
||||||
|
// if public or unlisted, cache
|
||||||
|
if (post.visibility === 'public' || post.visibility === 'unlisted') {
|
||||||
|
res.set('Cache-Control', 'public, max-age=4800')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (post.visibility === 'public' || post?.visibility === 'unlisted') {
|
||||||
|
secretKey(req, res, () => {
|
||||||
|
res.json(post);
|
||||||
})
|
})
|
||||||
if (!user) {
|
} else if (post.visibility === 'private') {
|
||||||
return res.status(404).json({ error: "User not found" })
|
jwt(req as UserJwtRequest, res, () => {
|
||||||
}
|
res.json(post);
|
||||||
return res.json(user.posts?.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()))
|
|
||||||
} catch (error) {
|
|
||||||
next(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
posts.get("/:id", async (req, res, next) => {
|
|
||||||
try {
|
|
||||||
const post = await Post.findOne({
|
|
||||||
where: {
|
|
||||||
id: req.params.id
|
|
||||||
},
|
|
||||||
include: [
|
|
||||||
{
|
|
||||||
model: File,
|
|
||||||
as: "files",
|
|
||||||
attributes: ["id", "title", "content", "sha", "createdAt", "updatedAt"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: User,
|
|
||||||
as: "users",
|
|
||||||
attributes: ["id", "username"],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
|
} else if (post.visibility === 'protected') {
|
||||||
if (!post) {
|
const { password } = req.query
|
||||||
throw new Error("Post not found.")
|
if (!password || typeof password !== 'string') {
|
||||||
}
|
return jwt(req as UserJwtRequest, res, () => {
|
||||||
|
|
||||||
|
|
||||||
if (post.visibility === 'public' || post?.visibility === 'unlisted') {
|
|
||||||
secretKey(req, res, () => {
|
|
||||||
res.json(post);
|
|
||||||
})
|
|
||||||
} else if (post.visibility === 'private') {
|
|
||||||
jwt(req as UserJwtRequest, res, () => {
|
|
||||||
res.json(post);
|
|
||||||
})
|
|
||||||
} else if (post.visibility === 'protected') {
|
|
||||||
const { password } = req.query
|
|
||||||
if (!password || typeof password !== 'string') {
|
|
||||||
return jwt(req as UserJwtRequest, res, () => {
|
|
||||||
res.json(post);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
const hash = crypto.createHash('sha256').update(password).digest('hex').toString()
|
|
||||||
if (hash !== post.password) {
|
|
||||||
return res.status(400).json({ error: "Incorrect password." })
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json(post);
|
res.json(post);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
const hash = crypto.createHash('sha256').update(password).digest('hex').toString()
|
||||||
|
if (hash !== post.password) {
|
||||||
|
return res.status(400).json({ error: "Incorrect password." })
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(post);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
next(e);
|
next(e);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
function getHtmlFromFile(file: any) {
|
||||||
|
const renderAsMarkdown = ['markdown', 'md', 'mdown', 'mkdn', 'mkd', 'mdwn', 'mdtxt', 'mdtext', 'text', ''];
|
||||||
|
const fileType = () => {
|
||||||
|
const pathParts = file.title.split(".");
|
||||||
|
const language = pathParts.length > 1 ? pathParts[pathParts.length - 1] : "";
|
||||||
|
return language;
|
||||||
|
};
|
||||||
|
const type = fileType();
|
||||||
|
let contentToRender: string = (file.content || '');
|
||||||
|
|
||||||
|
if (!renderAsMarkdown.includes(type)) {
|
||||||
|
contentToRender =
|
||||||
|
`~~~${type}
|
||||||
|
${file.content}
|
||||||
|
~~~`;
|
||||||
|
} else {
|
||||||
|
contentToRender = '\n' + file.content;
|
||||||
|
}
|
||||||
|
const html = markdown(contentToRender);
|
||||||
|
return html;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,16 +1,14 @@
|
|||||||
import { Router } from 'express'
|
import { Router } from "express";
|
||||||
// import { Movie } from '../models/Post'
|
// import jwt from "@lib/middleware/jwt";
|
||||||
import { User } from '../lib/models/User'
|
// import { User } from "@lib/models/User";
|
||||||
import jwt from '../lib/middleware/jwt'
|
|
||||||
|
|
||||||
export const users = Router()
|
export const users = Router();
|
||||||
|
|
||||||
users.get('/', jwt, async (req, res, next) => {
|
|
||||||
try {
|
|
||||||
const allUsers = await User.findAll()
|
|
||||||
res.json(allUsers)
|
|
||||||
} catch (error) {
|
|
||||||
next(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
// users.get("/", jwt, async (req, res, next) => {
|
||||||
|
// try {
|
||||||
|
// const allUsers = await User.findAll();
|
||||||
|
// res.json(allUsers);
|
||||||
|
// } catch (error) {
|
||||||
|
// next(error);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|||||||
Loading…
Reference in New Issue