Web security Fundamentals
Web security
Local Storage
- LocalStorage is super convenient to use because it always uses JavaScript to access a token. Its contents cannot be automatically sent anywhere. Therefore, localStorage is immune to cross-site request forgery (CSRF) attacks.
- On the downside, localStorage is potentially vulnerable to cross-site scripting (XSS) attacks. If an attacker can inject malicious JavaScript into a webpage, they can steal an access token in localStorage. Also, unlike cookies, localStorage doesnโt provide secure attributes that you can set to block attacks.
Cookies
An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to a user's web browser.
Cookies are not accessible via JavaScript provided the proper secure attributes for the cookies are set. This makes cookie data less vulnerable than localStorage data to JavaScript-based attacks.
Cookies have a limited storage capacity of 4KB, you might not be able to store some tokens that way. You may also need to put an access token in the HTTP Authorization request header with some APIs, which means cookies wonโt work to store the tokens in all cases.
They are used for 3 main purposes
Session management Logins, shopping carts, game scores, or anything else the server should remember
Personalization User preferences, themes, and other settings
Tracking Recording and analyzing user behavior
HttpOnly
- HttpOnly is an additional flag included in a Set-Cookie HTTP response header. If the HttpOnly flag is included in the HTTP response header, the cookie cannot be accessed through the client-side script (if the browser supports it).
- It is not accessible with document.cookie with JS and you can just send it to the server. (if the browser supports it).
- As a result, even if a cross-site scripting (XSS) flaw exists, and a user accidentally accesses a link that exploits this flaw, the browser will not reveal the cookie to a third party.
Cross-Site Scripting
- XSS attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user.
CORS
๐๐ข๐ฅ๐ฆ ๐๐๐ฎ๐ป๐ฑ๐ ๐ณ๐ผ๐ฟ ๐๐ฟ๐ผ๐๐-๐ข๐ฟ๐ถ๐ด๐ถ๐ป ๐ฅ๐ฒ๐๐ผ๐๐ฟ๐ฐ๐ฒ ๐ฆ๐ต๐ฎ๐ฟ๐ถ๐ป๐ด
๐๐ต'๐ด ๐ข ๐ธ๐ข๐บ ๐ง๐ฐ๐ณ ๐ฃ๐ณ๐ฐ๐ธ๐ด๐ฆ๐ณ๐ด ๐ต๐ฐ ๐ฅ๐ฆ๐ต๐ฆ๐ณ๐ฎ๐ช๐ฏ๐ฆ ๐ธ๐ฉ๐ฆ๐ต๐ฉ๐ฆ๐ณ ๐ฐ๐ณ ๐ฏ๐ฐ๐ต ๐ต๐ฉ๐ฆ๐บ ๐ด๐ฉ๐ฐ๐ถ๐ญ๐ฅ ๐ข๐ญ๐ญ๐ฐ๐ธ ๐ณ๐ฆ๐ฒ๐ถ๐ฆ๐ด๐ต๐ด ๐ง๐ณ๐ฐ๐ฎ ๐ฐ๐ฏ๐ฆ ๐ฅ๐ฐ๐ฎ๐ข๐ช๐ฏ ๐ต๐ฐ ๐ข๐ค๐ค๐ฆ๐ด๐ด ๐ณ๐ฆ๐ด๐ฐ๐ถ๐ณ๐ค๐ฆ๐ด ๐ง๐ณ๐ฐ๐ฎ ๐ข๐ฏ๐ฐ๐ต๐ฉ๐ฆ๐ณ ๐ฅ๐ฐ๐ฎ๐ข๐ช๐ฏ. ๐๐ต'๐ด ๐ถ๐ด๐ฆ๐ฅ ๐ธ๐ฉ๐ฆ๐ฏ ๐บ๐ฐ๐ถ ๐ธ๐ข๐ฏ๐ต ๐ต๐ฐ ๐ฆ๐ฎ๐ฃ๐ฆ๐ฅ ๐ค๐ฐ๐ฏ๐ต๐ฆ๐ฏ๐ต ๐ง๐ณ๐ฐ๐ฎ ๐ฐ๐ฏ๐ฆ ๐ด๐ช๐ต๐ฆ ๐ช๐ฏ๐ต๐ฐ ๐ข๐ฏ๐ฐ๐ต๐ฉ๐ฆ๐ณ ๐ด๐ช๐ต๐ฆ.
๐ง๐ต๐ฒ๐ฟ๐ฒ ๐ฎ๐ฟ๐ฒ ๐๐๐ผ ๐๐๐ฝ๐ฒ๐ ๐ผ๐ณ ๐๐ข๐ฅ๐ฆ ๐ต๐ฒ๐ฎ๐ฑ๐ฒ๐ฟ๐: โ Access-Control-Allow-Origin โก Access-Control-Allow-Methods
๐ ๐ง๐ต๐ฒ ๐ณ๐ถ๐ฟ๐๐ ๐ต๐ฒ๐ฎ๐ฑ๐ฒ๐ฟ tells the browser which domains are allowed to access the requested resource.
๐ ๐ง๐ต๐ฒ ๐๐ฒ๐ฐ๐ผ๐ป๐ฑ ๐ต๐ฒ๐ฎ๐ฑ๐ฒ๐ฟ tell the browser which methods (e.g., GET, POST ) are allowed.
CSRF
CSRF stands for Cross-Site Request Forgery is a web security vulnerability that allows an attacker to induce users to perform actions that they do not intend to perform. An attack that forces an end user to execute unwanted actions on a web application in which theyโre currently authenticated. A site is making requests to your site pretending to be another user.
JSON Web Token
- (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object.
- This information can be verified and trusted because it is signed digitally.
- JWT tokens can be signed with a secret.
How to Secure Cookies From Hackers
Secure attribute In production, the Secure attribute will ensure that the cookies are only sent over the encrypted connection. If a website doesn't use an SSL certificate and https protocol, the server will not send the cookies.
HttpOnly Attribute With this attribute, you forbid JavaScript code from accessing cookies. It protects the cookies from cross-site scripting attacks (XSS).
SameSite Attribute It controls when to send cookies with the cross-site requests. This attribute provides some protection against cross-site request forgery attacks (CSRF).
There are 3 values this attribute accepts:
- Strict: When you use SameSite=Strict attribute with the cookie, the browser will send that cookie only for the same-site requests. If a different domain sends the request, no cookie with the SameSite=Strict attribute will be sent.
- Lax - a cookie is only set when the domain in the URL of the browser matches the domain of the cookie, thus eliminating third partyโs domains.
- None - The browser sends cookies for both cross-site and same-site requests. If you use SameSite=None for your cookie, you have to specify the Secure attribute along with it.