Skip to content

Commit fd0151f

Browse files
committed
Added a route for resources of articles
Resources of articles in the "res/" direcotory can now be publicly accessed. My solution is no short of a genius one, however it is possilbe that is might have introduced some security vulns. I hope it didn't.
1 parent 8581641 commit fd0151f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

app.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,38 @@ app.get('/:article', async (req, res) => {
320320
await sendNotFound(res, config.pathTo404)
321321
})
322322

323+
// Le article cdn has arrived 🧢.
324+
app.get('/:article/*', async (req, res) => {
325+
// Remove the article from the path
326+
let localPath = req.path.replace(/^\/[^/]*/, "")
327+
328+
/*
329+
* If i understand things correctly there is no way for the client to escape
330+
* the article's res directory, because '..' gets resolved in the entire url
331+
* beforehand.
332+
*/
333+
334+
// Get the full resource path of the article.
335+
let article = (await dbop.articleUrlid(sql_con, req.params.article))[0]
336+
337+
// Handle the article not existing.
338+
if (!article[0]) {
339+
sendNotFound(res, config.pathTo404);
340+
return;
341+
}
342+
// Building the whole path.
343+
let resourcePath = path.resolve(path.join(config.articleDirectory, article[0].pathToArticle, config.articleResourceSubdir, localPath))
344+
345+
// Handle the file not existing
346+
if(!fssync.existsSync(resourcePath)) {
347+
sendNotFound(res, config.pathTo404);
348+
return;
349+
}
350+
351+
// And off you go!
352+
res.sendFile(resourcePath)
353+
})
354+
323355
app.get('/', async (req, res) => {
324356
let result = (await dbop.articles(sql_con, "%", 5))[0]
325357
// Doing your mom

0 commit comments

Comments
 (0)