cookie-to-guard
Version:
Library for protecting cookies from tampering
1 lines • 3.07 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.CookieGuard=t():e.CookieGuard=t()}(self,()=>{return e={497:(e,t)=>{var i;class o{constructor(e){if(!e||!e.cookieName)throw new Error("CookieGuard: cookieName is required");this.config={cookieName:e.cookieName,signatureName:`${e.cookieName}_sig`,redirectUrl:e.redirectUrl||null,onDetect:e.onDetect||null,autoReset:!1!==e.autoReset,domain:e.domain||null,secure:e.secure||!1,sameSite:e.sameSite||"Lax",secret:e.secret||this._generateSecret()},this._initialize()}setSecureCookie(e,t={}){const{days:i=365,path:o="/"}=t,r=((e,t)=>{let i=0;for(let t=0;t<e.length;t++)i=(i<<5)-i+e.charCodeAt(t),i|=0;return i.toString(36)+t})(e,this.config.secret);let s=`${this.config.cookieName}=${encodeURIComponent(e)}; expires=${new Date(Date.now()+864e5*i).toUTCString()}; path=${o}; SameSite=${this.config.sameSite}; `;this.config.domain&&(s+=`domain=${this.config.domain}; `),this.config.secure&&(s+="Secure; "),document.cookie=s,document.cookie=`${this.config.signatureName}=${encodeURIComponent(r)}; `+s.split("; ").slice(1).join("; ")}_initialize(){if(!this.initialized){if(this._verifyCookie(),"undefined"!=typeof window){const e=window.fetch,t=this;window.fetch=async function(...i){return t._verifyCookie(),e.apply(this,i)}}this.initialized=!0}}_generateSecretKey(){return"cg_"+Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15)}_getCookie(e){const t=document.cookie.split(";");for(let i of t)if(i=i.trim(),i.startsWith(e+"="))return decodeURIComponent(i.substring(e.length+1));return null}_setCookie(e,t,i=365){const o=new Date;o.setTime(o.getTime()+24*i*60*60*1e3);const r="expires="+o.toUTCString();document.cookie=e+"="+encodeURIComponent(t)+";"+r+";path=/"}_deleteCookie(e){document.cookie=e+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"}_createSignature(e){return e.split("").reverse().join("")+this.secretKey}_verifySignature(e,t){return this._createSignature(e)===t}_verifyCookie(){const e=this._getCookie(this.cookieName),t=this._getCookie(this.cookieName+"_sig");e&&t?this._verifySignature(e,t)||this._handleCookieTampering():e&&!t&&this._setCookie(this.cookieName+"_sig",this._createSignature(e))}_handleCookieTampering(){this.onDetect&&"function"==typeof this.onDetect&&this.onDetect(),this.autoReset&&(this._deleteCookie(this.cookieName),this._deleteCookie(this.cookieName+"_sig")),this.redirectUrl&&(window.location.href=this.redirectUrl)}setSecureCookie(e,t=365){this._setCookie(this.cookieName,e,t),this._setCookie(this.cookieName+"_sig",this._createSignature(e),t)}getSecureCookie(){return this._verifyCookie(),this._getCookie(this.cookieName)}deleteSecureCookie(){this._deleteCookie(this.cookieName),this._deleteCookie(this.cookieName+"_sig")}}e.exports?e.exports=o:void 0===(i=(()=>o).apply(t,[]))||(e.exports=i)}},t={},function i(o){var r=t[o];if(void 0!==r)return r.exports;var s=t[o]={exports:{}};return e[o](s,s.exports,i),s.exports}(497);var e,t});