Skip to content

Commit

Permalink
double slashes redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofinamorvieira committed Jan 4, 2025
1 parent bf36300 commit c22c3c4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import { sequence } from 'astro:middleware';
import { isDraftModeEnabled } from './lib/draftMode';
import logToRollbar from './lib/logToRollbar';

export const removeDoubleSlashes: MiddlewareHandler = async (context, next) => {
const { request } = context;
const url = new URL(request.url);

if (url.pathname.includes('//')) {
url.pathname = url.pathname.replace(/\/{2,}/g, '/');
return Response.redirect(url.toString(), 301);
}

return next();
};

export const rollbar: MiddlewareHandler = async ({ request, params }, next) => {
try {
return await next();
Expand Down Expand Up @@ -40,10 +52,8 @@ export const basicAuth: MiddlewareHandler = (context, next) => {

if (basicAuth) {
const authValue = basicAuth.split(' ')[1];

if (authValue) {
const [user, pwd] = Buffer.from(authValue, 'base64').toString('utf-8').split(':');

if (user === 'dato' && pwd === SECRET_API_TOKEN) {
return next();
}
Expand All @@ -60,4 +70,4 @@ export const basicAuth: MiddlewareHandler = (context, next) => {
});
};

export const onRequest = sequence(rollbar, security, basicAuth);
export const onRequest = sequence(removeDoubleSlashes, rollbar, security, basicAuth);

0 comments on commit c22c3c4

Please sign in to comment.