Skip to content

Prevent infinite redirect for doc-router /doc[^/] #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ jobs:
popd
fi
done

javascript-tests:
name: JavaScript Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v3
with:
node-version: 24
- name: Run tests
run: |
node --test terragrunt/modules/release-distribution/lambdas/doc-router/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ exports.handler = (event, context, callback) => {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// A test that asserts that the redirects work correctly.
// It can be run with
// node --test test.mjs

import { test } from "node:test";
import assert from "node:assert";
import lambda from "./index.js";

function invokeHandler(uri) {
return new Promise((resolve) => {
const callback = (_, response) => {
resolve(response);
};

// Invoke the lambda handler with an event containing the request URI
lambda.handler(
{
Records: [
{
cf: {
request: {
uri,
},
},
},
],
},
undefined,
callback
);
});
}

test("example expected redirects", async (t) => {
const redirects = [
// root
{ from: "/", to: "/stable/" },
{ from: "/index.html", to: "/stable/" },
// crate redirects
{ from: "/regex", to: "https://docs.rs/regex" },
{ from: "/log", to: "https://docs.rs/log" },
{ from: "/rand/0.9.1/rand/", to: "https://docs.rs/rand/0.9.1/rand/" },
// trpl
{ from: "/trpl", to: "/stable/book" },
{ from: "/stable/trpl/hello", to: "/stable/book/hello" },
{ from: "/nightly/trpl", to: "/nightly/book" },
// adv-book
{ from: "/adv-book", to: "/stable/nomicon" },
{ from: "/stable/adv-book/hello", to: "/stable/nomicon/hello" },
{ from: "/nightly/adv-book", to: "/nightly/nomicon" },
// master
{ from: "/master/hello", to: "/nightly/hello" },
// /doc
{ from: "/doc/std", to: "/std" },
{ from: "/doc/hello", to: "/hello" },
];

for (const redir of redirects) {
await t.test(`redirect ${redir.from}`, async (t) => {
const response = await invokeHandler(redir.from);

assert(["301", "302"].includes(response.status));

const location = response.headers.location[0];
assert.strictEqual(location.key, "Location");

assert.strictEqual(location.value, redir.to);
});
}
});

test("ensure the default rewrite to /stable", async (t) => {
const request = await invokeHandler("/std");
assert.strictEqual(request.uri, "/stable/std");
});

test("ensure no infinite redirect for /doc[^/]", async (t) => {
const request = await invokeHandler("/docs/std");
assert.strictEqual(request.uri, "/stable/docs/std");
});