-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprivateVar.html
99 lines (87 loc) · 2.92 KB
/
privateVar.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<html>
<head>
<script src="js/helperFunctions.js"></script>
</head>
<body>
<div>
<button id="login" onclick=login()>Login and get new token</button>
<br />
<code id="loginResponse"></code>
</div>
<hr />
<div>
<button id="makeRequest" onclick=makeRequest()>Make an "authenticated" request</button>
<br />
<code id="requestResponse"></code>
</div>
<hr />
<div>
<textarea rows=3 cols=100
placeholder="Enter JS to execute on this page (simulate XSS). Can you retrieve the auth token?"
id="payload"></textarea>
<br />
<button id="execute" onclick=execute()>Execute</button>
</div>
<hr />
<hr />
<div>
<button onclick=clearCookies()>Clear Cookies</button>
<button onclick=clearGlobalVar()>Clear global variable</button>
<button onclick=clearLocalStorage()>Clear localStorage</button>
<button onclick=clearSessionStorage()>Clear sessionStorage</button>
<br />
<code id="clearResponse"></code>
</div>
</body>
<script>
function authModule() {
const fetch = window.fetch; // thanks to @coffeetocode who found a bypass!
// we have to protect the original `fetch` from getting overwritten via XSS
// https://twitter.com/coffeetocode/status/1312998927881314305
const authOrigins = ["https://tokenstorage.ropnop.dev", "http://localhost:3000"];
let token = '';
this.setToken = (value) => {
token = value
}
this.fetch = (resource, options) => {
let req = new Request(resource, options);
destOrigin = new URL(req.url).origin;
if (token && authOrigins.includes(destOrigin)) {
req.headers.set('Authorization', token);
}
return fetch(req)
}
}
const auth = new authModule();
function login() {
fetch("/api/login")
.then((res) => {
if (res.status == 200) {
return res.json()
} else {
throw Error(res.statusText)
}
})
.then(data => {
auth.setToken(data.token)
logResponse("loginResponse", `Private auth object set with token value: ${data.token}`)
})
.catch(console.error)
}
function makeRequest() {
auth.fetch("/api/echo", {headers: {"CustomHeader1": "foobar"}})
.then((res) => {
if (res.status == 200) {
return res.text()
} else {
throw Error(res.statusText)
}
}).then(responseText => logResponse("requestResponse", responseText))
.catch(console.error)
}
function execute() {
content = document.getElementById("payload").value;
eval(content);
}
</script>
</html>