SPB Git

Repository Public

proxy.ts 3.6 KB 96 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : agentilab-web8 * File             : proxy.ts9 * Module           : Core10 * Repository       : https://git.spboucher.ai/agentilab-web11 * Project Website  : Not specified12 * Created          : 2026-12-0513 * Last Modified    : 2026-12-0514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : Proprietary34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { NextResponse } from 'next/server';44import type { NextRequest } from 'next/server';45import { basicAuthOk, hstsHeader } from './lib/security/headers';4647/**48 * Comportements décidés à l'exécution, pour que le même artefact serve le49 * staging et la production :50 *  - staging : authentification HTTP Basic, noindex, robots.txt fermé ;51 *  - HSTS progressif quand HSTS_MAX_AGE est défini.52 */53export function proxy(request: NextRequest) {54  const staging = process.env.SITE_ENV === 'staging';55  const hsts = hstsHeader(process.env.HSTS_MAX_AGE);5657  if (staging) {58    const user = process.env.STAGING_USER;59    const password = process.env.STAGING_PASSWORD;60    if (!user || !password) {61      return new NextResponse('Staging indisponible : accès non configuré.', {62        status: 503,63        headers: { 'X-Robots-Tag': 'noindex, nofollow' },64      });65    }66    // L’optimiseur d’images relit ses sources (/brands/…) par une requête interne qui ne porte67    // pas l’en-tête Authorization : ces fichiers, déjà publics sur les sites officiels des68    // produits, sont servis sans authentification, mais toujours en noindex.69    const openAsset = request.nextUrl.pathname.startsWith('/brands/');70    if (!openAsset && !basicAuthOk(request.headers.get('authorization'), user, password)) {71      return new NextResponse('Authentification requise.', {72        status: 401,73        headers: {74          'WWW-Authenticate': 'Basic realm="AgentiLab staging", charset="UTF-8"',75          'X-Robots-Tag': 'noindex, nofollow',76        },77      });78    }79    if (request.nextUrl.pathname === '/robots.txt') {80      return new NextResponse('User-agent: *\nDisallow: /\n', {81        headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-Robots-Tag': 'noindex, nofollow' },82      });83    }84  }8586  const response = NextResponse.next();87  if (staging) response.headers.set('X-Robots-Tag', 'noindex, nofollow');88  if (hsts) response.headers.set(hsts.key, hsts.value);89  return response;90}9192export const config = {93  // /healthz reste ouvert pour la supervision ; il ne révèle rien.94  matcher: ['/((?!healthz).*)'],95};96
Files main
Menu

Theme