| [9af201e] | 1 | import utils from '../utils.js';
|
|---|
| 2 | import platform from '../platform/index.js';
|
|---|
| 3 |
|
|---|
| 4 | export default platform.hasStandardBrowserEnv
|
|---|
| 5 | ? // Standard browser envs support document.cookie
|
|---|
| 6 | {
|
|---|
| 7 | write(name, value, expires, path, domain, secure, sameSite) {
|
|---|
| 8 | if (typeof document === 'undefined') return;
|
|---|
| 9 |
|
|---|
| 10 | const cookie = [`${name}=${encodeURIComponent(value)}`];
|
|---|
| 11 |
|
|---|
| 12 | if (utils.isNumber(expires)) {
|
|---|
| 13 | cookie.push(`expires=${new Date(expires).toUTCString()}`);
|
|---|
| 14 | }
|
|---|
| 15 | if (utils.isString(path)) {
|
|---|
| 16 | cookie.push(`path=${path}`);
|
|---|
| 17 | }
|
|---|
| 18 | if (utils.isString(domain)) {
|
|---|
| 19 | cookie.push(`domain=${domain}`);
|
|---|
| 20 | }
|
|---|
| 21 | if (secure === true) {
|
|---|
| 22 | cookie.push('secure');
|
|---|
| 23 | }
|
|---|
| 24 | if (utils.isString(sameSite)) {
|
|---|
| 25 | cookie.push(`SameSite=${sameSite}`);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | document.cookie = cookie.join('; ');
|
|---|
| 29 | },
|
|---|
| 30 |
|
|---|
| 31 | read(name) {
|
|---|
| 32 | if (typeof document === 'undefined') return null;
|
|---|
| 33 | // Match name=value by splitting on the semicolon separator instead of building a
|
|---|
| 34 | // RegExp from `name` — interpolating an unescaped string into a RegExp would let
|
|---|
| 35 | // metacharacters (e.g. `.+?` in an attacker-influenced cookie name) cause ReDoS or
|
|---|
| 36 | // match the wrong cookie. Browsers may serialize cookie pairs as either ";" or
|
|---|
| 37 | // "; ", so ignore optional whitespace before each cookie name.
|
|---|
| 38 | const cookies = document.cookie.split(';');
|
|---|
| 39 | for (let i = 0; i < cookies.length; i++) {
|
|---|
| 40 | const cookie = cookies[i].replace(/^\s+/, '');
|
|---|
| 41 | const eq = cookie.indexOf('=');
|
|---|
| 42 | if (eq !== -1 && cookie.slice(0, eq) === name) {
|
|---|
| 43 | return decodeURIComponent(cookie.slice(eq + 1));
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | return null;
|
|---|
| 47 | },
|
|---|
| 48 |
|
|---|
| 49 | remove(name) {
|
|---|
| 50 | this.write(name, '', Date.now() - 86400000, '/');
|
|---|
| 51 | },
|
|---|
| 52 | }
|
|---|
| 53 | : // Non-standard browser env (web workers, react-native) lack needed support.
|
|---|
| 54 | {
|
|---|
| 55 | write() {},
|
|---|
| 56 | read() {
|
|---|
| 57 | return null;
|
|---|
| 58 | },
|
|---|
| 59 | remove() {},
|
|---|
| 60 | };
|
|---|