20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t...

38
@PhilippeDeRyck [email protected] PHILIPPE DE RYCK COOKIES VS T OKENS A PARADOXICAL CHOICE

Transcript of 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t...

Page 1: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck – [email protected] DE RYCK

COOKIES VS TOKENSA PARADOXICAL CHOICE

Page 2: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 2

SHOULD YOU EVER USECOOKIES FOR YOUR API?

Page 3: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 3

Authentication

Authorization

Session management

Page 4: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck

• Founder of Pragmatic Web Security• In-depth web security training for developers• Covering web security, API security & Angular security

• 15+ years of security experience• Web security instructor and conference speaker• Author of Primer on client-side web security• Creator of Web Security Fundamentals on edX

• Course curator of the SecAppDev course• Yearly security course targeted towards developers• More information on https://secappdev.org

• Foodie and professional chefGOOGLE DEVELOPER EXPERT

PH.D. IN WEB SECURITY

HTTPS://PRAGMATICWEBSECURITY.COM

DR. PHILIPPE DE RYCK

Page 5: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 5

Works fine with a stateful REST

backend

Page 6: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 6

Works fine with a stateful REST

backend

Might benefit from a stateless REST backend

Page 7: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 8

SESSION DATA REPRESENTATION AND LOCALITY

How will you represent session data?Do you keep the data on the client or the server?

Page 8: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 9

21 3 4

Page 9: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 10

2

1

3 4

Page 10: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 12

Page 11: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 13

CAN YOU SPOTA PROBLEM HERE?

String token = "eyJhbGciOiJIUzI1NiIsInR5c...zWfOkEE";try {

DecodedJWT jwt = JWT.decode(token);} catch (JWTDecodeException exception){

//Invalid token}

123456

Page 12: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 14

String token = "eyJhbGciOiJIUzI1NiIsInR5c...zWfOkEE";try {

DecodedJWT jwt = JWT.decode(token);} catch (JWTDecodeException exception){

//Invalid token}

123456

String token = "eyJhbGciOiJIUzI1NiIsInR5c...zWfOkEE";try {

Algorithm algorithm = Algorithm.HMAC256("secret");JWTVerifier verifier = JWT.require(algorithm)

.build(); //Reusable verifier instanceDecodedJWT jwt = verifier.verify(token);

} catch (JWTVerificationException exception){//Invalid signature/claims

}

123456789

Decoding only

Signature verification

Page 13: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 15

Page 14: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck

HMAC-BASED JWT SIGNATURES

16

data yxzN...sFno=

yxzN...sFno=

GENERATE HMAC

VERIFY HMAC

yxzN...sFno=

HMAC

SECRET KEY

data

data

Message is the same as the one that was signed

Message differs from the one

that was signed

Page 15: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck

ASYMMETRIC JWT SIGNATURES

17

payload yxzN...sFno=

GENERATE SIGNATURE

VERIFY SIGNATURE

SIGNATURE

PRIVATE KEY

Message is the same as the one that was signed

Message differs from the one

that was signed

PUBLIC KEY

C171...dfb

yxzN...sFno=

payload C171...dfb

C171...dfb

Page 16: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 19

SESSION DATA STORAGE

Where do you store your session data in the browser?

Page 17: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

“ “The browser offers a storage that can’t be read by JavaScript: HttpOnly cookies. It’s a good way to identify a requester

without risking XSS attacks.

Page 18: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 21HttpOnly cookies

Page 19: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck

THE DEAL WITH HTTPONLY

• The HttpOnly flag resolves a consequence of an XSS attack• Stealing the session identifier becomes a lot harder• But you still have an XSS vulnerability in your application

• XSS allows the attacker to execute arbitrary code• That code can trigger authenticated requests, modify the DOM, ...

• HttpOnly is still recommended, because it raises the bar• XSS attacks become a little bit harder to execute and to persist• XSS attacks from subdomains become less powerful (with domain-based cookies)

• In Chrome, HttpOnly prevents cookies from entering the rendering process• Useful to reduce the impact of CPU-based Spectre and Meltdown attacks

22

Page 20: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 23

Page 21: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck

COMPARING CLIENT-SIDE STORAGE MECHANISMS

24

Available to the entire origin

LOCALSTORAGE SESSIONSTORAGE IN-MEMORY COOKIES

Survives a page reload

Cannot be shielded from malicious code

Application code required for handling

Available to the window and children

Survives a page reload

Can be a bit shielded from malicious code

Application code required for handling

Available to running code only

Does not survive a page reload

Can be shielded from malicious code

Application code required for handling

Can be fully hidden from JavaScript

Survives a page reload

Can be shielded from malicious code

Application code not required for handling

Page 22: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 25

SESSION DATA TRANSPORT

How will you send session data to the server?

Page 23: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 26

Cookie: ID=42

Cookie: JWT=eyJhbGci…

Authorization: Bearer 42

Authorization: Bearer eyJhbGci…

Page 24: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 27

WHICH OF THESE IS THE BEST PRACTICEFOR ISOLATED APPLICATIONS?A. Session=...; Secure; HttpOnly

B. __Secure-Session=...; Secure; HttpOnly

C. __Host-Session=...; Secure; HttpOnly

D. __Host-Session=...; Secure; HttpOnly; SameSite

E. __Host-Session=...; Secure; HttpOnly; SameSite; LockOrigin

Page 25: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 29

Load unrelated page

Legitimate requests within the application

Restogradecontext

Maliciousfoodcontext Forged requests

Page 26: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck

OVERVIEW OF CSRF DEFENSES

• Hidden form tokens (synchronizer tokens)• Requires server-side storage of CSRF tokens, which may be resource-intensive

• Double submit cookies (transparent tokens)• Stateless CSRF defense mechanism• Extremely compatible with client-side JavaScript applications (e.g. AngularJS)

• Checking the origin header• Useful when other context information is missing• Plays an important role when accessing APIs with Cross-Origin Resource Sharing (CORS)• Practical defense during the setup of a WebSocket connection

• SameSite cookies• Addresses the root of the problem, but browser support is still limited

31

Page 27: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

“ “Angular's HttpClient has built-in support for

[the double submit cookie pattern]

Page 28: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 33

!

Page 29: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 34

Page 30: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 35

CAN YOU SPOT THESECURITY ISSUE HERE?

Page 31: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 36

Page 32: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 38

HOW TO AUTHORIZE THELOADING OF DOM RESOURCES(IMG, SCRIPT, ...)?

Page 33: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 40

Can contain identifiers & session objects Can contain identifiers & session objects

COOKIES AUTHORIZATION HEADER

Only works well with a single domain Freedom to include headers to any domain

Automatically handled by the browser Requires custom code to get, store and send session data

Always present, including on DOM resources

Only present on XHR calls, unless you add it through a ServiceWorker

Page 34: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck 41

SHOULD YOU EVER USECOOKIES FOR YOUR API?

SURE, WHY NOT?

Page 35: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

“ “The browser offers a storage that can’t be read by JavaScript: HttpOnly cookies. It’s a good way to identify a requester

without risking XSS attacks.

Page 36: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

“ “Unfortunately, lately I've seen more and more people recommending to use JWT for managing user sessions in

their web applications. This is a terrible, terrible idea

Page 37: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck

1-day workshops

5-day dual-track program

Whiteboard hacking (aka hands-on Threat Modeling)

Building secure web & web service applications

Securing Kubernetes the hard way

Jim Manico

Sebastien Deleersnyder

Jimmy Mesta

Crypto, AppSec Processes, web security, access control, mobile security, ...

Page 38: 20181021 - Cookies vs tokens - A paradoxial choice (AppSec ... · Authentication Authorization t @PhilippeDeRyck ... ASYMMETRICJWT SIGNATURES 17 payload yxzN...sFno= GENERATESIGNATURE

@PhilippeDeRyck – [email protected] DE RYCK

/in/PhilippeDeRyck @PhilippeDeRyck

[email protected]