Skip to content

Commit

Permalink
Add x-11ty-rebuild-token to secure rebuild endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
antgel committed Oct 30, 2023
1 parent ce90158 commit 1c242aa
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const DEFAULT_OPTIONS = {
watch: [], // Globs to pass to separate dev server chokidar for watching
aliases: {}, // Aliasing feature
rebuildUrl: null, // POST URL to trigger rebuild
rebuildUrlToken: "", // Secret token in x-11ty-rebuild-token header

// Logger (fancier one is injected by Eleventy)
logger: {
Expand Down Expand Up @@ -421,6 +422,12 @@ class EleventyDevServer {

eleventyDevServerMiddleware(req, res, next) {
if (this.options.rebuildUrl && req.url === this.options.rebuildUrl && req.method === 'POST') {
const token = req.headers['x-11ty-rebuild-token'];
if (token !== this.options.rebuildUrlToken) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
return res.end('Forbidden');
}

this.eventBus.emit('eleventyDevServer.rebuild');
res.writeHead(200);
return res.end();
Expand Down

0 comments on commit 1c242aa

Please sign in to comment.