-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcowl.js
153 lines (132 loc) · 4.23 KB
/
cowl.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Temporary implementation of COWL's LWorkers atop iframes
//
// Rebasing firefox is taking a bit longer than expected, please come
// back soon for the actual implementation that can handle code that
// is not necessarily honest-but-curious.
//
// Unlike our actual LWorker implementation this can only be used at
// the top-level and while the current label is public.
(function() {
if (!window.LWorker) {
function LWorker(url, privacy, trust) {
if (!this instanceof LWorker) {
return new LWorker(url, privacy,trust);
}
if (!url || !/^http[s]?:\/\//.test(url)) {
throw new Error("Url must be a full path");
}
if (privacy && typeof privacy === 'string') {
privacy = new Label(privacy);
}
if (trust && typeof trust === 'string') {
trust = new Label(trust);
}
var self = this;
self.id = id();
self.src = lworkerBlobURI(self.id, url, privacy, trust);
self._iframe = document.createElement('iframe');
self._iframe.sandbox = 'allow-scripts allow-forms';
self._iframe.height = 0;
self._iframe.width = 0;
self._iframe.style = 'visibility:hidden; display:none';
self._iframe.src = self.src;
document.body.appendChild(self._iframe);
window.addEventListener('message', function(event) {
if (event.data.id && event.data.id == self.id && self._onmessage) {
self._onmessage(event.data.message);
}
});
}
window.LWorker = LWorker;
}
LWorker.prototype.postMessage = function(obj) {
this._iframe.contentWindow.postMessage({id: this.id, message: obj },'*');
}
LWorker.prototype.onmessage = function(obj) {
this._iframe.postMessage(obj,'*');
}
Object.defineProperty(LWorker.prototype, 'onmessage', {
get : function() { return this._onmessage; },
set : function (cb) { this._onmessage = cb; }
});
function id() {
var array = new Uint32Array(1);
crypto.getRandomValues(array);
return array[0].toString();
};
// -- DO NOT EDIT --
function __workerCode() { /*
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://cowl.ws/cowl.js"></script>
<script>
COWL.enable();
COWL.privacyLabel = %privacyLabel;
COWL.trustLabel = %trustLabel;
%clearance
postMessage = function(obj) {
parent.postMessage({id:%id, message:obj},'*');
};
function importScript(url) {
var source = document.createElement('script');
source.src = url;
document.body.appendChild(source);
}
(function() {
var _onmessage;
window.addEventListener('message', function(event) {
if (event.data.id && event.data.id == %id && _onmessage) {
_onmessage(event.data.message);
}
});
Object.defineProperty(window, 'onmessage', {
get : function() { return _onmessage; },
set : function (cb) { _onmessage = cb; }
});
})();
</script>
</head>
<body>
<script src="%src"></script>
</body>
</html>
*/ }
// -- DO NOT EDIT --
function lworkerBlobURI(id, src, privacyClearance, trustClearance) {
COWL.enable();
var privacyLabel = showLabel(COWL.privacyLabel);
var trustLabel = showLabel(COWL.trustLabel);
var clearance = "";
if (privacyClearance) {
clearance += 'COWL.privacyClearance = ' +
showLabel(opts.privacyClearance) + ';';
}
if (trustClearance) {
clearance += 'COWL.trustClearance = ' +
showLabel(opts.trustClearance) + ';';
}
var src = __workerCode.toString().replace(/%privacyLabel/g,privacyLabel)
.replace(/%trustLabel/g,trustLabel)
.replace(/%clearance/g,clearance)
.replace(/%id/g,id.toString())
.replace(/%src/g,src)
.split('\n');
var blob = new Blob([src.splice(1,src.length-2).join('\n')],
{type: 'text/html'});
return URL.createObjectURL(blob);
}
function showLabel(label) {
if (label.isEmpty)
return 'new Label()';
label = label.toString();
var roles = label.toString().replace(/^Label\((.*)\)/,"$1").split(").and(");
roles.forEach(function(role, idx) {
role = role.replace(/moz-role:/g,'');
roles[idx] = "new Role('" +
role.replace(/^Role\((.*)\)/,"$1").split(").or(").join("').or('") +
"')";
});
return 'new Label('+roles.join(').and(')+')';
}
})();