Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit df9431b

Browse files
author
King Long Tse
authored
moved inline script from index.html to index.js. enabled content security policy (#163)
1 parent d99f15b commit df9431b

File tree

3 files changed

+104
-46
lines changed

3 files changed

+104
-46
lines changed

test-e2e/devServer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ const port = config.local.devServerPort;
4040
}
4141
})();
4242

43+
app.use(function(req, res, next) {
44+
// add 'unsafe-eval' if you want to test scripts that calls eval()
45+
res.setHeader("Content-Security-Policy", "script-src 'nonce-someNonce' 'unsafe-eval' 'strict-dynamic'");
46+
return next();
47+
});
48+
4349
// the tests loads tests from localhost:8000/analytics.js/v1/<write-key>/analytics.js
4450
app.use(
4551
'/analytics.js/v1/:writeKey/',

test-e2e/static/index.html

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,37 @@
99
<button>Load</button>
1010
</form>
1111
<p id='status-msg'></p>
12-
13-
<button id='track-product-viewed' onclick="analytics.track('Product Viewed')">
12+
<button id ='unsafe-eval'>Unsafe eval</button>
13+
<button id='track-product-viewed'>
1414
Track: Product Viewed
1515
</button>
16-
<button id='track-checkout-started' onclick="analytics.track('Checkout Started')">
16+
<button id='track-checkout-started'>
1717
Track: Checkout Started
1818
</button>
19-
<button id='track-coupon-denied' onclick="analytics.track('Coupon Denied')">
19+
<button id='track-coupon-denied'>
2020
Track: Coupon Denied
2121
</button>
2222
<br />
23-
<button id='page-home' onclick="analytics.page('Home')">Page: Home</button>
24-
<button id='page-about' onclick="analytics.page('About')">Page: About</button>
25-
<button id='page-contact' onclick="analytics.page('Contact')">Page: Contact</button>
23+
<button id='page-home'>Page: Home</button>
24+
<button id='page-about'>Page: About</button>
25+
<button id='page-contact'">Page: Contact</button>
2626
<br />
27-
<button id='identify-fathy' onclick="analytics.identify('fathy')">Identify: fathy</button>
28-
<button id='identify-spongebob' onclick="analytics.identify('spongebob', {'lastname':'squarepants'})">
27+
<button id='identify-fathy'>Identify: fathy</button>
28+
<button id='identify-spongebob'>
2929
Identify: spongebob
3030
</button>
31-
<button
32-
id='group'
33-
onclick="analytics.group(
34-
'group name',
35-
{
36-
'address': {
37-
'city': 'Vancouver',
38-
'country': 'Canada',
39-
'postalCode': 'V6b3E2',
40-
'state': 'BC',
41-
'street': '21 Jump St'
42-
},
43-
'avatar': 'does not exist',
44-
'description': 'a fake group',
45-
'email': '[email protected]',
46-
'employees': 3,
47-
'id': 1,
48-
'industry': 'sw eng',
49-
'name': 'libweb',
50-
'phone': '555-pizza',
51-
'website': 'www.google.com',
52-
'plan': 'business'
53-
},
54-
{
55-
'integrations': {
56-
'All': true
57-
}
58-
}, function() { console.log('group callback triggered')})"
59-
>
31+
<button id='group'>
6032
Group
6133
</button>
6234

63-
<button
64-
id='alias'
65-
onclick="analytics.alias('userId', 'previous id', {
66-
'integrations': { 'All': true }
67-
}, function(){ console.log('alias callback triggered')})"
68-
>
35+
<button id='alias'>
6936
Alias
7037
</button>
7138

72-
<button id='reset' onclick="analytics.reset()">Reset</button>
39+
<button id='reset'>Reset</button>
7340
<br />
7441

75-
<script>
42+
<script nonce="someNonce">
7643
const { searchParams } = new URL(document.location);
7744
const writeKey = searchParams.get("writeKey");
7845
const cdnHost = searchParams.get("cdnHost") || 'cdn.segment.com';
@@ -160,5 +127,6 @@
160127
status("ajs not loaded, enter a write key");
161128
}
162129
</script>
130+
<script src='index.js' nonce="someNonce"></script>
163131
</body>
164132
</html>

test-e2e/static/index.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
document
2+
.getElementById('track-product-viewed')
3+
.addEventListener('click', () => {
4+
analytics.track('Product Viewed');
5+
});
6+
document
7+
.getElementById('track-checkout-started')
8+
.addEventListener('click', () => {
9+
analytics.track('Checkout Started');
10+
});
11+
document.getElementById('track-coupon-denied').addEventListener('click', () => {
12+
analytics.track('Coupon Denied');
13+
});
14+
15+
document.getElementById('page-home').addEventListener('click', () => {
16+
analytics.page('Home');
17+
});
18+
document.getElementById('page-about').addEventListener('click', () => {
19+
analytics.page('About');
20+
});
21+
document.getElementById('page-contact').addEventListener('click', () => {
22+
analytics.page('Contact');
23+
});
24+
25+
document.getElementById('identify-fathy').addEventListener('click', () => {
26+
analytics.identify('fathy');
27+
});
28+
document.getElementById('identify-spongebob').addEventListener('click', () => {
29+
analytics.identify('spongebob', { lastname: 'squarepants' });
30+
});
31+
32+
document.getElementById('group').addEventListener('click', () => {
33+
analytics.group(
34+
'group name',
35+
{
36+
address: {
37+
city: 'Vancouver',
38+
country: 'Canada',
39+
postalCode: 'V6b3E2',
40+
state: 'BC',
41+
street: '21 Jump St'
42+
},
43+
avatar: 'does not exist',
44+
description: 'a fake group',
45+
46+
employees: 3,
47+
id: 1,
48+
industry: 'sw eng',
49+
name: 'libweb',
50+
phone: '555-pizza',
51+
website: 'www.google.com',
52+
plan: 'business'
53+
},
54+
{
55+
integrations: {
56+
All: true
57+
}
58+
},
59+
function() {
60+
console.log('group callback triggered');
61+
}
62+
);
63+
});
64+
65+
document.getElementById('alias').addEventListener('click', () => {
66+
analytics.alias(
67+
'userId',
68+
'previous id',
69+
{
70+
integrations: { All: true }
71+
},
72+
function() {
73+
console.log('alias callback triggered');
74+
}
75+
);
76+
});
77+
78+
document.getElementById('reset').addEventListener('click', () => {
79+
analytics.reset();
80+
});
81+
82+
document.getElementById('unsafe-eval').addEventListener('click', () => {
83+
eval('console.log(1234567)');
84+
});

0 commit comments

Comments
 (0)