@server/next 0.48.0 → 0.48.1

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.
Files changed (2) hide show
  1. package/index.js +30 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1021,6 +1021,16 @@ function seconds(expires) {
1021
1021
  if (!ms) throw new Error(`Invalid \`expires\`: "${expires}"`);
1022
1022
  return Math.round(ms / 1e3);
1023
1023
  }
1024
+ var looksLikeOurs = (token) => {
1025
+ const parts = token.split(".");
1026
+ if (parts.length !== 3) return false;
1027
+ try {
1028
+ const header = JSON.parse(atob(parts[0].replace(/-/g, "+").replace(/_/g, "/")));
1029
+ return header?.alg === "HS256";
1030
+ } catch {
1031
+ return false;
1032
+ }
1033
+ };
1024
1034
  var bearer = (ctx) => {
1025
1035
  const header = ctx.headers.authorization;
1026
1036
  if (!header) return;
@@ -1034,8 +1044,13 @@ async function read(ctx, strategy) {
1034
1044
  if (!token) return;
1035
1045
  const payload = await verifyJwt(token, ctx.options.secrets);
1036
1046
  if (!payload) {
1037
- if (inCookie(strategy)) return;
1038
- throw ServerError_default.AUTH_INVALID_TOKEN();
1047
+ if (!inCookie(strategy)) throw ServerError_default.AUTH_INVALID_TOKEN();
1048
+ ctx.clearCookie = NAME;
1049
+ ctx.options.log?.message(
1050
+ "auth",
1051
+ looksLikeOurs(token) ? "discarded a session cookie signed with a key that is not in SECRETS. If you rotated it, keep the previous value: secrets: [current, previous]" : "discarded a session cookie that was not issued by this app"
1052
+ );
1053
+ return;
1039
1054
  }
1040
1055
  return payload;
1041
1056
  }
@@ -1592,8 +1607,13 @@ function entry2(options) {
1592
1607
  try {
1593
1608
  claims2 = await check(token, issuer, allowed, claimNames);
1594
1609
  } catch (error) {
1595
- if (options.cookie) return;
1596
- throw error;
1610
+ if (!options.cookie) throw error;
1611
+ ctx.clearCookie = options.cookie;
1612
+ ctx.options.log?.message(
1613
+ "auth",
1614
+ `discarded a ${options.cookie} cookie that ${issuer} did not sign, or that has expired`
1615
+ );
1616
+ return;
1597
1617
  }
1598
1618
  ctx.auth = {
1599
1619
  issuedAt: new Date((claims2.iat ?? 0) * 1e3),
@@ -2170,6 +2190,12 @@ async function parseResponse(out, ctx) {
2170
2190
  applyCors(out, ctx);
2171
2191
  applySecurity(out, ctx);
2172
2192
  out = await applyCache(out, ctx);
2193
+ if (ctx.clearCookie) {
2194
+ out.headers.append(
2195
+ "set-cookie",
2196
+ `${ctx.clearCookie}=; Path=/; Max-Age=0; HttpOnly`
2197
+ );
2198
+ }
2173
2199
  if (ctx.time?.times?.length > 1) {
2174
2200
  out.headers.set("Server-Timing", ctx.time.headers());
2175
2201
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.48.0",
3
+ "version": "0.48.1",
4
4
  "description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
5
5
  "homepage": "https://server-js.com/",
6
6
  "repository": "github:franciscop/server-next",