Skip to content

Commit ca09e6a

Browse files
committed
?key= authentication, closes simonw#3
1 parent efaba44 commit ca09e6a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "puppeteer-screenshot",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Take screenshot of a website",
55
"private": true,
66
"license": "MIT",

screenshot.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
const { parse } = require('url');
2+
const { timingSafeEqual } = require('crypto');
23
const { getScreenshot } = require('./chromium');
34
const { getInt, getUrlFromPath, isValidUrl } = require('./validator');
45

6+
const SECRET_KEY = process.env.SECRET_KEY;
7+
8+
const compare = (a, b) => {
9+
try {
10+
return timingSafeEqual(Buffer.from(a, "utf8"), Buffer.from(b, "utf8"));
11+
} catch {
12+
return false;
13+
}
14+
};
15+
516
module.exports = async function (req, res) {
17+
if (!compare(req.query.key, SECRET_KEY)) {
18+
res.statusCode = 403;
19+
res.setHeader('Content-Type', 'text/html');
20+
res.end('<h1>Bad ?key=</h1><p>Permission denied</p>');
21+
return;
22+
}
623
try {
724
const { pathname = '/', query = {} } = parse(req.url, true);
825
const { type = 'png', quality, fullPage } = query;

0 commit comments

Comments
 (0)