Sign and verify session tokens (HS256) with WebCrypto
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 55/100
- Type d'issue
- Fonctionnalité
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- node.js, typescript
- Domaine
- authentication, backend, security
Piste de recherche
Read packages/server/src/webhook.ts for the existing crypto.subtle HMAC usage, then create packages/server/src/security/session-token.ts and its tests at packages/server/tests/security/session-token.test.ts. Implement the specified compact HS256 token format, claim validation, origin and conversation checks, expiry skew, and key rotation; use RFC 7515 Appendix A.1 as a verification vector. Done means the listed acceptance tests pass, including malformed and tampered tokens.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Right now the widget picks its own conversationId and the server trusts it. With a shared store (Redis, Postgres), anyone who guesses another visitor's id can read that conversation's context and add to it. The fix in the next release is a short-lived signed session token: the server gives the widget a token bound to one conversation and one origin, and checks it on every request. This issue is the token format itself, as a new file with no dependencies. The /session route that hands tokens out comes later.
This one needs care, so it's for someone who has had a couple of PRs merged. I'll review it closely.
What to do
Create packages/server/src/security/session-token.ts. Use a compact JWS (header.payload.signature, base64url, no padding) with HS256 through crypto.subtle, so it runs on Node 22, Workers, Bun and Deno with no dependency.
export interface SessionClaims {
v: 1;
sid: string; // session id
cid: string; // conversation id
org: string; // origin the token was issued for
uid?: string;
tier?: string;
iat: number; // seconds
exp: number; // seconds
}
/** Imports a secret (at least 32 bytes as UTF-8) as an HMAC key. */
export function importSessionKey(secret: string): Promise<CryptoKey>;
/** Signs with keys[0] and puts its index in the header as `kid`. */
export function signSession(claims: SessionClaims, keys: CryptoKey[]): Promise<string>;
export function verifySession(
token: string,
keys: CryptoKey[],
opts: { origin: string | null; conversationId: string; now?: number; clockSkewSec?: number }
): Promise<
| { ok: true; claims: SessionClaims }
| { ok: false; code: 'malformed' | 'bad_signature' | 'expired' | 'origin_mismatch' | 'conversation_mismatch' }
>;
- Header:
{"alg":"HS256","typ":"JWT","kid":"<index>"}. Reject any otheralg, includingnone. - Verify with
crypto.subtle.verify, never by comparing strings. Try the key named bykidfirst, then the others, so rotating secrets doesn't log everyone out. clockSkewSecdefaults to 60 and applies toexp.- Check the signature before trusting anything in the payload. Return
malformedfor a token that doesn't have three parts, isn't valid base64url or JSON, or has a missing or wrongly typed claim. importSessionKeythrows a clear error when the secret is shorter than 32 bytes.- Don't create keys or call
crypto.subtleat module load. Workers don't allow that at global scope.
Where to look
packages/server/src/webhook.tsalready usescrypto.subtleHMAC for webhook signatures- RFC 7515, Appendix A.1, has an HS256 example with its key, usable as a test vector for verification
Acceptance criteria
- New
packages/server/src/security/session-token.tsandpackages/server/tests/security/session-token.test.ts - Round trip: sign, then verify, gives back the same claims
- Tests for an expired token, expired but within the skew, a changed payload, a changed signature,
alg: none, a wrong origin, a wrong conversation, a malformed token, and rotation (signed with an old key that's second in the list) - The RFC 7515 A.1 token verifies with its key (the claims check can be skipped for it)
- A secret shorter than 32 bytes throws
- Not exported from
@chatcops/serveryet, and no changeset needed (pnpm changeset --empty) -
pnpm test:coverageandpnpm lintpass
Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off dev, and open your PR against dev with Closes #<this issue number>. Setup notes (pnpm, build before test) are in CONTRIBUTING.md.
- Langage dominant
- TypeScript
- Étoiles
- 13
- Forks
- 7
- Merge moyen
- 4 h 11 min
- PR mergées (30 j)
- 122
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de codercops/chatcops
-
enhancement hacktoberfest help wanted server
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
Les mainteneurs répondent en général sous 1 jour
-
Document the pre-chat formPeut-être pris @masyarakatai l’a pris il y a 5 jours. Ouvertedocumentation good first issue hacktoberfest widget
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
codercops/chatcops#70 · 3 commentaires · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
-
enhancement hacktoberfest help wanted server
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
Les mainteneurs répondent en général sous 1 jour
-
Create @chatcops/store-redis for Upstash and other Redis clientsPeut-être pris @bhushan4work l’a pris il y a 1 jour. Ouverteenhancement hacktoberfest help wanted server
codercops/chatcops#248 · 2 commentaires · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
-
Create @chatcops/store-sqlite, a store for single-server setupsPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverteenhancement hacktoberfest help wanted server
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de codercops/chatcops
Issues similaires
-
ready-for-agent
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
xavxyz/bei-shan-lin-shui#57 ·
Les mainteneurs répondent en général sous 1 jour
-
[BUG] rag: sentence content can cross the reported sentence boundariesPeut-être pris @LJK-ikun l’a pris aujourd’hui. Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Les mainteneurs répondent en général sous 1 jour
-
bug needs-triage
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
Les mainteneurs répondent en général sous 1 jour
-
README: the Resource URI Schemas example's file://documents/* pattern only matches that literal URIOuverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
platformatic/mcp#361 ·
Les mainteneurs répondent en général sous 1 jour
-
最近文档这个弹窗,左右宽度没有最小宽度限制Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
siyuan-note/siyuan#20799 · 7 commentaires ·
Les mainteneurs répondent en général sous 1 jour