Skip to content

Commit 843f9bd

Browse files
committed
Prevent infinite redirect for doc-router /doc[^/]
Previously, it would just put the slice in the Location with no regards for what was in there. If there was no slash after the `/doc`, it would issue a relative redirect, which was still under `/doc`, causing an infinite redirect. - /docs/std -> s/std - /docs/s/std -> s/s/std - /docs/s/s/s/std -> ... We prevent this behavior here by only applying the `/doc` redirect if there is a slash directly after it, causing the default /stable rewrite fallback to be applied to everything else, which will in practice lead to nice 404 page. This should not break anything as all such queries currently run into these loops, including plain `/doc`.
1 parent a525d15 commit 843f9bd

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

terragrunt/modules/release-distribution/lambdas/doc-router/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ exports.handler = (event, context, callback) => {
104104
}
105105

106106
// Docs used to be under /doc, so redirect those for now
107-
if (request.uri.startsWith('/doc')) {
107+
if (request.uri.startsWith('/doc/')) {
108108
return redirect(request.uri.slice(4), callback);
109109
}
110110

terragrunt/modules/release-distribution/lambdas/doc-router/test.mjs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
// A test that asserts that the redirects work correctly.
2+
// It can be run with
3+
// node --test test.mjs
4+
15
import { test } from "node:test";
26
import assert from "node:assert";
37
import lambda from "./index.js";
48

59
function invokeHandler(uri) {
6-
return new Promise((res) => {
7-
const cb = (_, response) => {
8-
res(response);
10+
return new Promise((resolve) => {
11+
const callback = (_, response) => {
12+
resolve(response);
913
};
1014

15+
// Invoke the lambda handler with an event containing the request URI
1116
lambda.handler(
1217
{
1318
Records: [
@@ -21,7 +26,7 @@ function invokeHandler(uri) {
2126
],
2227
},
2328
undefined,
24-
cb
29+
callback
2530
);
2631
});
2732
}
@@ -68,3 +73,8 @@ test("ensure the default rewrite to /stable", async (t) => {
6873
const request = await invokeHandler("/std");
6974
assert.strictEqual(request.uri, "/stable/std");
7075
});
76+
77+
test("ensure no infinite redirect for /doc[^/]", async (t) => {
78+
const request = await invokeHandler("/docs/std");
79+
assert.strictEqual(request.uri, "/stable/docs/std");
80+
});

0 commit comments

Comments
 (0)