Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RFC6265 Cookies and CookieJar for Node.js

License

NotificationsYou must be signed in to change notification settings

salesforce/tough-cookie

A Node.js implementation ofRFC6265 for cookie parsing, storage, and retrieval.

Getting Started

Install Tough Cookie usingnpm:

npm install tough-cookie

oryarn:

yarn add tough-cookie

Usage

import{Cookie,CookieJar}from'tough-cookie'// parse a `Cookie` request headerconstreqCookies='ID=298zf09hf012fh2; csrf=u32t4o3tb3gg43; _gat=1'.split(';').map(Cookie.parse)// generate a `Cookie` request headerconstcookieHeader=reqCookies.map((cookie)=>cookie.cookieString()).join(';')// parse a Set-Cookie response headerconstresCookie=Cookie.parse('foo=bar; Domain=example.com; Path=/; Expires=Tue, 21 Oct 2025 00:00:00 GMT',)// generate a Set-Cookie response headerconstsetCookieHeader=cookie.toString()// store and retrieve cookiesconstcookieJar=newCookieJar()// uses the in-memory store by defaultawaitcookieJar.setCookie(resCookie,'https://example.com/')constmatchingCookies=awaitcookieJar.getCookies('https://example.com/')

Important

For more detailed usage information, refer to theAPI docs.

RFC6265bis

Support forRFC6265bis is being developed. As these revisions toRFC6252 arestill inActive Internet-Draft state, the areas of support that follow are subject to change.

SameSite Cookies

This change makes it possible for servers, and supporting clients, to mitigate certain types of CSRFattacks by disallowingSameSite cookies from being sent cross-origin.

Example

import{CookieJar}from'tough-cookie'constcookieJar=newCookieJar()// uses the in-memory store by default// storing cookies with various SameSite attributesawaitcookieJar.setCookie('strict=authorized; SameSite=strict','http://example.com/index.html',)awaitcookieJar.setCookie('lax=okay; SameSite=lax','http://example.com/index.html',)awaitcookieJar.setCookie('normal=whatever','http://example.com/index.html')// retrieving cookies using a SameSite contextconstlaxCookies=awaitcookieJar.getCookies('http://example.com/index.html',{// the first cookie (strict=authorized) will not be returned if the context is 'lax'// but the other two cookies will be returnedsameSiteContext:'lax',})

Note

It is highly recommended that you readRFC6265bis - Section 8.8 for more details on SameSite cookies, security considerations, and defense in depth.

Cookie Prefixes

Cookie prefixes are a way to indicate that a given cookie was set with a set of attributes simply byinspecting the first few characters of the cookie's name.

Two prefixes are defined:

  • "__Secure-"

    If a cookie's name begins with a case-sensitive match for the string__Secure-, then the cookie was set with a "Secure" attribute.

  • "__Host-"

    If a cookie's name begins with a case-sensitive match for the string__Host-, then the cookie was set with a "Secure" attribute, a "Path" attribute with a value of "/", and no "Domain" attribute.

IfprefixSecurity is enabled forCookieJar, then cookies that match the prefixes defined above but donot obey the attribute restrictions are not added.

You can define this functionality by passing in theprefixSecurity option toCookieJar. It can be one of 3 values:

  1. silent: (default) Enable cookie prefix checking but silently fail to add the cookie if conditions are not met.
  2. strict: Enable cookie prefix checking and error out if conditions are not met.
  3. unsafe-disabled: Disable cookie prefix checking.

IfignoreError is passed in astrue when setting a cookie then the error is silent regardless of theprefixSecurity option (assuming it's enabled).

Example

import{CookieJar,MemoryCookieStore}from'tough-cookie'constcookieJar=newCookieJar(newMemoryCookieStore(),{prefixSecurity:'silent',})// this cookie will be silently ignored since the url is insecure (http)awaitcookieJar.setCookie('__Secure-SID=12345; Domain=example.com; Secure;','http://example.com',)// this cookie will be stored since the url is secure (https)awaitcookieJar.setCookie('__Secure-SID=12345; Domain=example.com; Secure;','https://example.com',)

Note

It is highly recommended that you readRFC6265bis - Section 4.1.3 for more details on Cookie Prefixes.

Node.js Version Support

We follow theNode.js release schedule and supportall versions that are in Active LTS or Maintenance. We will always do a major release when dropping supportfor older versions of node, and we will do so in consultation with our community.

About

RFC6265 Cookies and CookieJar for Node.js

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp