forked from tanner/qotnews
move purify to server side.
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
import fetch from 'isomorphic-fetch';
|
||||
|
||||
import { purify, purifyArray } from './_purify';
|
||||
|
||||
const API_URL = process.env.API_URL || 'http://localhost:33842';
|
||||
|
||||
export async function get(req, res) {
|
||||
const response = await fetch(`${API_URL}/api/${req.params.id}`);
|
||||
res.writeHead(response.status, { 'Content-Type': 'application/json' });
|
||||
res.end(await response.text());
|
||||
if (!response.ok) {
|
||||
return res.end(await response.text());
|
||||
}
|
||||
const data = await response.json();
|
||||
data.story = purify(data.story);
|
||||
data.related = purifyArray(data.related);
|
||||
res.end(JSON.stringify(data));
|
||||
}
|
25
webapp/src/routes/_purify.js
Normal file
25
webapp/src/routes/_purify.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import createDOMPurify from 'dompurify';
|
||||
import { JSDOM } from 'jsdom';
|
||||
|
||||
export const purify = (story, DOMPurify) => {
|
||||
if (!DOMPurify) {
|
||||
DOMPurify = createDOMPurify(new JSDOM('').window);
|
||||
}
|
||||
if (story.title) {
|
||||
story.title = DOMPurify.sanitize(story.title);
|
||||
}
|
||||
if (story.text) {
|
||||
story.text = DOMPurify.sanitize(story.text);
|
||||
}
|
||||
return story;
|
||||
};
|
||||
|
||||
export const purifyArray = (array, DOMPurify) => {
|
||||
if (array instanceof Array) {
|
||||
if (!DOMPurify) {
|
||||
DOMPurify = createDOMPurify(new JSDOM('').window);
|
||||
}
|
||||
return array.map(story => purify(story, DOMPurify));
|
||||
}
|
||||
return array;
|
||||
};
|
@@ -1,5 +1,7 @@
|
||||
import fetch from 'isomorphic-fetch';
|
||||
|
||||
import { purifyArray } from './_purify';
|
||||
|
||||
const API_URL = process.env.API_URL || 'http://localhost:33842';
|
||||
|
||||
export async function get(req, res) {
|
||||
@@ -9,5 +11,10 @@ export async function get(req, res) {
|
||||
};
|
||||
const response = await fetch(`${API_URL}/api?skip=${skip}&limit=${limit}`);
|
||||
res.writeHead(response.status, { 'Content-Type': 'application/json' });
|
||||
res.end(await response.text());
|
||||
if (!response.ok) {
|
||||
return res.end(await response.text());
|
||||
}
|
||||
const data = await response.json();
|
||||
data.stories = purifyArray(data.stories);
|
||||
res.end(JSON.stringify(data));
|
||||
}
|
@@ -1,5 +1,7 @@
|
||||
import fetch from 'isomorphic-fetch';
|
||||
|
||||
import { purifyArray } from './_purify';
|
||||
|
||||
const API_URL = process.env.API_URL || 'http://localhost:33842';
|
||||
|
||||
export async function get(req, res) {
|
||||
@@ -9,5 +11,10 @@ export async function get(req, res) {
|
||||
};
|
||||
const response = await fetch(`${API_URL}/api/search?q=${req.query.q}&skip=${skip}&limit=${limit}`);
|
||||
res.writeHead(response.status, { 'Content-Type': 'application/json' });
|
||||
res.end(await response.text());
|
||||
if (!response.ok) {
|
||||
return res.end(await response.text());
|
||||
}
|
||||
const data = await response.json();
|
||||
data.results = purifyArray(data.results);
|
||||
res.end(JSON.stringify(data));
|
||||
}
|
Reference in New Issue
Block a user