Set-Cookie

Set-Cookie

Tags
Tech
Web Dev
Published
Author

์ฟ ํ‚ค๋ž€

  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์—์„œ ๋ฐ›์€ ์ฟ ํ‚ค๋ฅผ ์ €์žฅํ•˜๊ณ , HTTP ์š”์ฒญ์‹œ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์—ญํ• 
  • HTTP์—์„œ๋Š” Set-Cookie๋กœ ํ‘œํ˜„๋˜๋ฉฐ ์‘๋‹ตํ—ค๋”์—์„œ ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ๋กœ ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌ

๋ณด์•ˆ

Secure

Secure์„ ์ ์šฉํ•˜๋ฉด https์ธ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅ

HttpOnly

XXS ๊ณต๊ฒฉ์„ ๋ฐฉ์ง€ํ•˜๋ฉฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ ‘๊ทผ ๋ถˆ๊ฐ€(document.cookie)ํ•˜๊ฒŒ ๋จ HTTP ์ „์†ก์—๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ

SameSite

XSRF ๊ณต๊ฒฉ ๋ฐฉ์ง€ํ•˜๋ฉฐ, ์š”์ฒญ ๋„๋ฉ”์ธ๊ณผ ์ฟ ํ‚ค์— ์„ค์ •๋œ ๋„๋ฉ”์ธ์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋งŒ ์ฟ ํ‚ค ์ „์†ก
  • samesite=strict : ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์ดํŠธ ์™ธ๋ถ€์—์„œ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ, ์ด ์˜ต์…˜์ด ์žˆ๋Š” ์ฟ ํ‚ค๋Š” ์ ˆ๋Œ€๋กœ ์ „์†ก์ด ์•ˆ๋จ
  • samsite-lax
  1. "์•ˆ์ „ํ•œ" HTTP๋ฉ”์„œ๋“œ์ธ ๊ฒฝ์šฐ(ex: get๋ฐฉ์‹, post๋ฐฉ์‹ X)
  1. ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ ํƒ์ƒ‰์—์„œ ์ด๋ฃจ์–ด์งˆ ๊ฒฝ์šฐ(๋ธŒ๋ผ์šฐ์ € ์ฃผ์†Œ์ฐฝ์—์„œ url์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ)
  • samesite-none : ์™ธ๋ถ€์— ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•˜๊ฒ ๋‹ค.(๋ณด์•ˆ ์ปจํ…์ŠคํŠธ๊ฐ€ ํ•„์š”ํ•จ)
ํฌ๋กฌ ๋ธŒ์•„์šฐ์ €๋Š” ํ•ด๋‹น ์ฟ ํ‚ค๋Š” ๋ฐ˜๋“œ์‹œ Secure๊ฐ€ ์„ค์ •๋œ ์ฟ ํ‚ค์—ฌ์•ผํ•จ

์†์„ฑ

Expires

๋งŒ๋ฃŒ์ผ์ด ๋˜๋ฉด ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•จ

max-age

์ฟ ํ‚ค์˜ ์ตœ๋Œ€ ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋ƒ„

Domain

๋ช…์‹œํ•œ ๋„๋ฉ”์ธ์„ ๋‚˜ํƒ€๋ƒ„

path

์ด ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•œ ํ•˜์œ„ ๊ฒฝ๋กœ ํŽ˜์ด์ง€๋งŒ ์ฟ ํ‚ค๊ฐ€ ์ ‘๊ทผ๋จ

์ฟ ํ‚คํ•จ์ˆ˜

getCookie(name)

// ์ฃผ์–ด์ง„ ์ด๋ฆ„์˜ ์ฟ ํ‚ค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, // ์กฐ๊ฑด์— ๋งž๋Š” ์ฟ ํ‚ค๊ฐ€ ์—†๋‹ค๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. function getCookie(name) { let matches = document.cookie.match(new RegExp( "(?:^|; )" + name.replace(/([\\.$?*|{}\\(\\)\\[\\]\\\\\\/\\+^])/g, '\\\\$1') + "=([^;]*)" )); return matches ? decodeURIComponent(matches[1]) : undefined; }

setCookie(name,value,options)

function setCookie(name, value, options = {}) { options = { path: '/', // ํ•„์š”ํ•œ ๊ฒฝ์šฐ, ์˜ต์…˜ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ...options }; if (options.expires instanceof Date) { options.expires = options.expires.toUTCString(); } let updatedCookie = encodeURIComponent(name) + "=" + encodeURIComponent(value); for (let optionKey in options) { updatedCookie += "; " + optionKey; let optionValue = options[optionKey]; if (optionValue !== true) { updatedCookie += "=" + optionValue; } } document.cookie = updatedCookie; } // Example of use: setCookie('user', 'John', {secure: true, 'max-age': 3600});

deleteCookie(name)

function deleteCookie(name) { setCookie(name, "", { 'max-age': -1 }) }
์ฃผ์˜ : ๊ฐฑ์‹ ์ด๋‚˜ ์‚ญ์ œ๋Š” ๋™์ผํ•œ ๋„๋ฉ”์ธ๊ณผ ๊ฒฝ๋กœ์—์„œ๋งŒ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ

ย