@adonisjs/http-server 8.1.3 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { D as __exportAll, E as __commonJSMin, O as __require, S as httpResponseSerializer, T as debug_default, _ as BriskRoute, b as httpMiddleware, c as parseRoute, f as safeDecodeURI, g as RouteResource, h as RouteGroup, i as getPreviousUrl, k as __toESM, m as trustProxy, n as createSignedURL, p as toRoutesJSON, r as encodeUrl, s as mime$1, u as serializeCookie, v as Route, x as httpRequest, y as httpExceptionHandler } from "./helpers-CLk8RLHd.js";
1
+ import { A as __toESM, C as httpResponseSerializer, D as __commonJSMin, E as debug_default, O as __exportAll, S as httpRequest, _ as RouteResource, b as httpExceptionHandler, c as mime$1, d as serializeCookie, g as RouteGroup, h as trustProxy, i as getPreviousUrl, k as __require, l as parseRoute, m as toRoutesJSON, n as createSignedURL, p as safeDecodeURI, r as encodeUrl, v as BriskRoute, x as httpMiddleware, y as Route } from "./helpers-DHzJiDUz.js";
2
2
  import { n as findRoute, t as createURL } from "./helpers-Dqw8abku.js";
3
3
  import { createUrlBuilder } from "./src/client/url_builder.js";
4
4
  import { parse, stringify } from "@poppinss/qs";
@@ -206,6 +206,11 @@ const E_HTTP_REQUEST_ABORTED = class AbortException extends E_HTTP_EXCEPTION {
206
206
  * ```
207
207
  */
208
208
  var Redirect = class extends Macroable {
209
+ /**
210
+ * HTTP context reference, set by the response when creating
211
+ * the redirect instance during request handling.
212
+ */
213
+ ctx;
209
214
  /**
210
215
  * Array of allowed hosts for referrer-based redirects.
211
216
  * When empty, only the request's own host is allowed.
@@ -4298,6 +4303,7 @@ var HttpResponse = class extends Macroable {
4298
4303
  }
4299
4304
  redirect(path, forwardQueryString = false, statusCode = ResponseStatus.Found) {
4300
4305
  const handler = new Redirect(this.request, this, this.#router, this.#qs, this.#config.redirect);
4306
+ handler.ctx = this.ctx;
4301
4307
  if (forwardQueryString) handler.withQs();
4302
4308
  if (path === "back") return handler.status(statusCode).back();
4303
4309
  if (path) return handler.status(statusCode).toPath(path);
@@ -1,5 +1,5 @@
1
- import { c as parseRoute } from "../helpers-CLk8RLHd.js";
2
- import { _ as Qs, c as HttpRequest, i as HttpResponse, n as Server, r as HttpContext, s as Router, t as defineConfig } from "../define_config-drp-Wzwx.js";
1
+ import { l as parseRoute } from "../helpers-DHzJiDUz.js";
2
+ import { _ as Qs, c as HttpRequest, i as HttpResponse, n as Server, r as HttpContext, s as Router, t as defineConfig } from "../define_config-CCHiNpSl.js";
3
3
  import { t as createURL } from "../helpers-Dqw8abku.js";
4
4
  import { Container } from "@adonisjs/fold";
5
5
  import { Socket } from "node:net";
@@ -1310,13 +1310,45 @@ function safeDecodeURI(path, useSemicolonDelimiter) {
1310
1310
  //#endregion
1311
1311
  //#region src/helpers.ts
1312
1312
  /**
1313
+ * Validates that a URL is safe to use as a redirect destination.
1314
+ *
1315
+ * - Relative URLs must start with `/` and not be protocol-relative (`//`)
1316
+ * - Absolute URLs must parse successfully and their host must match
1317
+ * `currentHost` or be listed in `allowedHosts`
1318
+ *
1319
+ * When `currentHost` and `allowedHosts` are omitted, absolute URLs
1320
+ * are accepted as long as they parse successfully.
1321
+ *
1322
+ * @param url - The URL to validate
1323
+ * @param currentHost - The current request's Host header value
1324
+ * @param allowedHosts - Array of additionally allowed hosts
1325
+ */
1326
+ function isValidRedirectUrl(url, currentHost, allowedHosts) {
1327
+ if (typeof url !== "string" || url.trim() === "") return false;
1328
+ if (url.startsWith("//")) return false;
1329
+ if (url.startsWith("/")) try {
1330
+ return new URL(url, "http://localhost").host === "localhost";
1331
+ } catch {
1332
+ return false;
1333
+ }
1334
+ try {
1335
+ const parsed = new URL(url);
1336
+ /**
1337
+ * When no host constraints are provided, accept any
1338
+ * parseable absolute URL
1339
+ */
1340
+ if (!currentHost && (!allowedHosts || allowedHosts.length === 0)) return true;
1341
+ if (currentHost && parsed.host === currentHost) return true;
1342
+ if (allowedHosts && allowedHosts.length > 0 && allowedHosts.includes(parsed.host)) return true;
1343
+ return false;
1344
+ } catch {
1345
+ return false;
1346
+ }
1347
+ }
1348
+ /**
1313
1349
  * Returns the previous URL from the request's `Referer` header,
1314
1350
  * validated against the request's `Host` header and an optional
1315
- * list of allowed hosts.
1316
- *
1317
- * The referrer is accepted when its host matches the request's
1318
- * `Host` header or is listed in `allowedHosts`. Otherwise the
1319
- * `fallback` value is returned.
1351
+ * list of allowed hosts using `isValidRedirectUrl`.
1320
1352
  *
1321
1353
  * @param headers - The incoming request headers
1322
1354
  * @param allowedHosts - Array of allowed referrer hosts
@@ -1326,12 +1358,7 @@ function getPreviousUrl(headers, allowedHosts, fallback) {
1326
1358
  let referrer = headers["referer"] || headers["referrer"];
1327
1359
  if (!referrer) return fallback;
1328
1360
  if (Array.isArray(referrer)) referrer = referrer[0];
1329
- try {
1330
- const parsed = new URL(referrer);
1331
- const host = headers["host"];
1332
- if (host && parsed.host === host) return referrer;
1333
- if (allowedHosts.length > 0 && allowedHosts.includes(parsed.host)) return referrer;
1334
- } catch {}
1361
+ if (isValidRedirectUrl(referrer, headers["host"], allowedHosts)) return referrer;
1335
1362
  return fallback;
1336
1363
  }
1337
1364
  /**
@@ -1481,4 +1508,4 @@ function appendQueryString(uri, queryString, qsParser) {
1481
1508
  return mergedQueryString ? `${pathname}?${mergedQueryString}` : pathname;
1482
1509
  }
1483
1510
  //#endregion
1484
- export { tracing_channels_exports as C, __exportAll as D, __commonJSMin as E, __require as O, httpResponseSerializer as S, debug_default as T, BriskRoute as _, matchRoute as a, httpMiddleware as b, parseRoute as c, parseRange as d, safeDecodeURI as f, RouteResource as g, RouteGroup as h, getPreviousUrl as i, __toESM as k, routeInfo as l, trustProxy as m, createSignedURL as n, middlewareInfo as o, toRoutesJSON as p, encodeUrl as r, mime as s, appendQueryString as t, serializeCookie as u, Route as v, canWriteResponseBody as w, httpRequest as x, httpExceptionHandler as y };
1511
+ export { __toESM as A, httpResponseSerializer as C, __commonJSMin as D, debug_default as E, __exportAll as O, httpRequest as S, canWriteResponseBody as T, RouteResource as _, isValidRedirectUrl as a, httpExceptionHandler as b, mime as c, serializeCookie as d, parseRange as f, RouteGroup as g, trustProxy as h, getPreviousUrl as i, __require as k, parseRoute as l, toRoutesJSON as m, createSignedURL as n, matchRoute as o, safeDecodeURI as p, encodeUrl as r, middlewareInfo as s, appendQueryString as t, routeInfo as u, BriskRoute as v, tracing_channels_exports as w, httpMiddleware as x, Route as y };
package/build/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { C as tracing_channels_exports, _ as BriskRoute, d as parseRange, g as RouteResource, h as RouteGroup, v as Route, w as canWriteResponseBody } from "./helpers-CLk8RLHd.js";
2
- import { _ as Qs, a as CookieSerializer, c as HttpRequest, d as Redirect, f as E_CANNOT_LOOKUP_ROUTE, g as errors_exports, h as E_ROUTE_NOT_FOUND, i as HttpResponse, l as CookieParser, m as E_HTTP_REQUEST_ABORTED, n as Server, o as ResponseStatus, p as E_HTTP_EXCEPTION, r as HttpContext, s as Router, t as defineConfig, u as CookieClient } from "./define_config-drp-Wzwx.js";
1
+ import { T as canWriteResponseBody, _ as RouteResource, f as parseRange, g as RouteGroup, v as BriskRoute, w as tracing_channels_exports, y as Route } from "./helpers-DHzJiDUz.js";
2
+ import { _ as Qs, a as CookieSerializer, c as HttpRequest, d as Redirect, f as E_CANNOT_LOOKUP_ROUTE, g as errors_exports, h as E_ROUTE_NOT_FOUND, i as HttpResponse, l as CookieParser, m as E_HTTP_REQUEST_ABORTED, n as Server, o as ResponseStatus, p as E_HTTP_EXCEPTION, r as HttpContext, s as Router, t as defineConfig, u as CookieClient } from "./define_config-CCHiNpSl.js";
3
3
  import Macroable from "@poppinss/macroable";
4
4
  import is from "@sindresorhus/is";
5
5
  //#region src/exception_handler.ts
@@ -7,14 +7,25 @@ import { type SignedURLOptions } from './types/url_builder.ts';
7
7
  import type { RouteMatchers, RouteJSON, MatchItRouteToken } from './types/route.ts';
8
8
  import { type MiddlewareFn, type RouteHandlerInfo, type MiddlewareHandlerInfo, type ParsedGlobalMiddleware, type ParsedNamedMiddleware } from './types/middleware.ts';
9
9
  export { createURL };
10
+ /**
11
+ * Validates that a URL is safe to use as a redirect destination.
12
+ *
13
+ * - Relative URLs must start with `/` and not be protocol-relative (`//`)
14
+ * - Absolute URLs must parse successfully and their host must match
15
+ * `currentHost` or be listed in `allowedHosts`
16
+ *
17
+ * When `currentHost` and `allowedHosts` are omitted, absolute URLs
18
+ * are accepted as long as they parse successfully.
19
+ *
20
+ * @param url - The URL to validate
21
+ * @param currentHost - The current request's Host header value
22
+ * @param allowedHosts - Array of additionally allowed hosts
23
+ */
24
+ export declare function isValidRedirectUrl(url: string, currentHost?: string, allowedHosts?: string[]): boolean;
10
25
  /**
11
26
  * Returns the previous URL from the request's `Referer` header,
12
27
  * validated against the request's `Host` header and an optional
13
- * list of allowed hosts.
14
- *
15
- * The referrer is accepted when its host matches the request's
16
- * `Host` header or is listed in `allowedHosts`. Otherwise the
17
- * `fallback` value is returned.
28
+ * list of allowed hosts using `isValidRedirectUrl`.
18
29
  *
19
30
  * @param headers - The incoming request headers
20
31
  * @param allowedHosts - Array of allowed referrer hosts
@@ -1,3 +1,3 @@
1
- import { a as matchRoute, c as parseRoute, i as getPreviousUrl, l as routeInfo, n as createSignedURL, o as middlewareInfo, r as encodeUrl, s as mime, t as appendQueryString, u as serializeCookie } from "../helpers-CLk8RLHd.js";
1
+ import { a as isValidRedirectUrl, c as mime, d as serializeCookie, i as getPreviousUrl, l as parseRoute, n as createSignedURL, o as matchRoute, r as encodeUrl, s as middlewareInfo, t as appendQueryString, u as routeInfo } from "../helpers-DHzJiDUz.js";
2
2
  import { t as createURL } from "../helpers-Dqw8abku.js";
3
- export { appendQueryString, createSignedURL, createURL, encodeUrl, getPreviousUrl, matchRoute, middlewareInfo, mime, parseRoute, routeInfo, serializeCookie };
3
+ export { appendQueryString, createSignedURL, createURL, encodeUrl, getPreviousUrl, isValidRedirectUrl, matchRoute, middlewareInfo, mime, parseRoute, routeInfo, serializeCookie };
@@ -5,6 +5,7 @@ import type { HttpResponse } from './response.ts';
5
5
  import type { ResponseConfig } from './types/response.ts';
6
6
  import type { RoutesList, LookupList, URLOptions, GetRoutesForMethod, RouteBuilderArguments } from './types/url_builder.ts';
7
7
  import Macroable from '@poppinss/macroable';
8
+ import type { HttpContext } from './http_context/main.ts';
8
9
  /**
9
10
  * Provides a fluent API for constructing HTTP redirect responses.
10
11
  *
@@ -29,6 +30,11 @@ import Macroable from '@poppinss/macroable';
29
30
  */
30
31
  export declare class Redirect extends Macroable {
31
32
  #private;
33
+ /**
34
+ * HTTP context reference, set by the response when creating
35
+ * the redirect instance during request handling.
36
+ */
37
+ ctx?: HttpContext;
32
38
  /**
33
39
  * Array of allowed hosts for referrer-based redirects.
34
40
  * When empty, only the request's own host is allowed.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonisjs/http-server",
3
- "version": "8.1.3",
3
+ "version": "8.2.0",
4
4
  "description": "AdonisJS HTTP server with support packed with Routing and Cookies",
5
5
  "main": "build/index.js",
6
6
  "type": "module",