/
VegaWrapper.js
486 lines (439 loc) · 19.3 KB
/
VegaWrapper.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
'use strict';
/* global module */
var makeValidator = require('domain-validator'),
parseWikidataValue = require('wd-type-parser');
module.exports = VegaWrapper;
module.exports.removeColon = removeColon;
/**
* Utility function to remove trailing colon from a protocol
* @param {string} protocol
* @return {string}
*/
function removeColon(protocol) {
return protocol && protocol.length && protocol[protocol.length - 1] === ':'
? protocol.substring(0, protocol.length - 1) : protocol;
}
/**
* Shared library to wrap around vega code
* @param {Object} wrapperOpts Configuration options
* @param {Object} wrapperOpts.datalib Vega's datalib object
* @param {Object} wrapperOpts.datalib.load Vega's data loader
* @param {Function} wrapperOpts.datalib.load.loader Vega's data loader function
* @param {Function} wrapperOpts.datalib.extend similar to jquery's extend()
* @param {boolean} wrapperOpts.useXhr true if we should use XHR, false for node.js http loading
* @param {boolean} wrapperOpts.isTrusted true if the graph spec can be trusted
* @param {Object} wrapperOpts.domains allowed protocols and a list of their domains
* @param {Object} wrapperOpts.domainMap domain remapping
* @param {Function} wrapperOpts.logger
* @param {Function} wrapperOpts.parseUrl
* @param {Function} wrapperOpts.formatUrl
* @param {string} [wrapperOpts.languageCode]
* @constructor
*/
function VegaWrapper(wrapperOpts) {
var self = this;
// Copy all options into this object
self.objExtender = wrapperOpts.datalib.extend;
self.objExtender(self, wrapperOpts);
self.validators = {};
self.datalib.load.loader = function (opt, callback) {
var error = callback || function (e) { throw e; }, url;
try {
url = self.sanitizeUrl(opt); // enable override
} catch (err) {
error(err);
return;
}
// Process data response
var cb = function (error, data) {
return self.dataParser(error, data, opt, callback);
};
if (self.useXhr) {
return self.datalib.load.xhr(url, opt, cb);
} else {
return self.datalib.load.http(url, opt, cb);
}
};
self.datalib.load.sanitizeUrl = self.sanitizeUrl.bind(self);
// Prevent accidental use
self.datalib.load.file = alwaysFail;
if (self.useXhr) {
self.datalib.load.http = alwaysFail;
} else {
self.datalib.load.xhr = alwaysFail;
}
}
/**
* Check if host was listed in the allowed domains, normalize it, and get correct protocol
* @param {string} host
* @returns {Object}
*/
VegaWrapper.prototype.sanitizeHost = function sanitizeHost(host) {
// First, map the host
host = (this.domainMap && this.domainMap[host]) || host;
if (this.testHost('https:', host)) {
return {host: host, protocol: 'https:'};
} else if (this.testHost('http:', host)) {
return {host: host, protocol: 'http:'};
}
return undefined;
};
/**
* Test host against the list of allowed domains based on the protocol
* @param {string} protocol
* @param {string} host
* @returns {boolean}
*/
VegaWrapper.prototype.testHost = function testHost(protocol, host) {
if (!this.validators[protocol]) {
var domains = this._getProtocolDomains(protocol);
if (domains) {
this.validators[protocol] = makeValidator(domains, protocol === 'https:' || protocol === 'http:');
} else {
return false;
}
}
return this.validators[protocol].test(host);
};
/**
* Gets allowed domains for a given protocol. Assumes protocol ends with a ':'.
* Handles if this.domains's keys do not end in the ':'.
* @param {string} protocol
* @return {[]|false}
* @private
*/
VegaWrapper.prototype._getProtocolDomains = function _getProtocolDomains(protocol) {
return this.domains[protocol] || this.domains[removeColon(protocol)];
};
/**
* Validate and update urlObj to be safe for client-side and server-side usage
* @param {Object} opt passed by the vega loader, and will add 'graphProtocol' param
* @returns {boolean} true on success
*/
VegaWrapper.prototype.sanitizeUrl = function sanitizeUrl(opt) {
// In some cases we may receive a badly formed URL in a form customprotocol:https://...
opt.url = opt.url.replace(/^([a-z]+:)https?:\/\//, '$1//');
var decodedPathname,
isRelativeProtocol = /^\/\//.test(opt.url),
urlParts = this.parseUrl(opt),
sanitizedHost = this.sanitizeHost(urlParts.host);
if (!sanitizedHost) {
throw new Error('URL hostname is not whitelisted: ' + opt.url);
}
urlParts.host = sanitizedHost.host;
if (!urlParts.protocol) {
// node.js mode only - browser's url parser will always set protocol to current one
// Update protocol-relative URLs
urlParts.protocol = sanitizedHost.protocol;
isRelativeProtocol = true;
}
// Save original protocol to post-process the data
opt.graphProtocol = urlParts.protocol;
if (opt.type === 'open') {
// Trim the value here because mediawiki will do it anyway, so we might as well save on redirect
decodedPathname = decodeURIComponent(urlParts.pathname).trim();
switch (urlParts.protocol) {
case 'http:':
case 'https:':
// The default protocol for the open action is wikititle, so if isRelativeProtocol is set,
// we treat the whole pathname as title (without the '/' prefix).
if (!isRelativeProtocol) {
// If we get http:// and https:// protocol hardcoded, remove the '/wiki/' prefix instead
if (!/^\/wiki\/.+$/.test(decodedPathname)) {
throw new Error('wikititle: http(s) links must begin with /wiki/ prefix');
}
decodedPathname = decodedPathname.substring('/wiki'.length);
}
opt.graphProtocol = 'wikititle';
// fall-through
case 'wikititle:':
// wikititle:///My_page or wikititle://en.wikipedia.org/My_page
// open() at this point may only be used to link to a Wiki page, as it may be invoked
// without a click, thus potentially causing a privacy issue.
if (Object.keys(urlParts.query).length !== 0) {
throw new Error('wikititle: query parameters are not allowed');
}
if (!/^\/[^|]+$/.test(decodedPathname)) {
throw new Error('wikititle: invalid title');
}
urlParts.pathname = '/wiki/' + encodeURIComponent(decodedPathname.substring(1).replace(' ', '_'));
urlParts.protocol = sanitizedHost.protocol;
break;
default:
throw new Error('"open()" action only allows links with wikititle protocol, e.g. wikititle:///My_page');
}
} else {
switch (urlParts.protocol) {
case 'http:':
case 'https:':
if (!this.isTrusted) {
throw new Error('HTTP and HTTPS protocols are not supported for untrusted graphs.\n' +
'Use wikiraw:, wikiapi:, wikirest:, wikirawupload:, and other protocols.\n' +
'See https://www.mediawiki.org/wiki/Extension:Graph#External_data');
}
// keep the original URL
break;
case 'wikiapi:':
// wikiapi:///?action=query&list=allpages
// Call to api.php - ignores the path parameter, and only uses the query
urlParts.query = this.objExtender(urlParts.query, {format: 'json', formatversion: '2'});
urlParts.pathname = '/w/api.php';
urlParts.protocol = sanitizedHost.protocol;
opt.addCorsOrigin = true;
break;
case 'wikirest:':
// wikirest:///api/rest_v1/page/...
// Call to RESTbase api - requires the path to start with "/api/"
// The /api/... path is safe for GET requests
if (!/^\/api\//.test(urlParts.pathname)) {
throw new Error('wikirest: protocol must begin with the /api/ prefix');
}
// keep urlParts.query
// keep urlParts.pathname
urlParts.protocol = sanitizedHost.protocol;
break;
case 'wikiraw:':
case 'tabular:':
case 'map:':
// wikiraw:///MyPage/data
// Get content of a wiki page, where the path is the title
// of the page with an additional leading '/' which gets removed.
// Uses mediawiki api, and extract the content after the request
// Query value must be a valid MediaWiki title string, but we only ensure
// there is no pipe symbol, the rest is handled by the api.
decodedPathname = decodeURIComponent(urlParts.pathname);
if (!/^\/[^|\x1F]+$/.test(decodedPathname)) {
throw new Error(urlParts.protocol + ' invalid title');
}
if (urlParts.protocol === 'wikiraw:') {
urlParts.query = {
format: 'json',
formatversion: '2',
action: 'query',
prop: 'revisions',
rvprop: 'content',
titles: decodedPathname.substring(1)
};
} else {
urlParts.query = {
format: 'json',
formatversion: '2',
action: 'jsondata',
title: decodedPathname.substring(1)
};
if (urlParts.siteLanguage || this.languageCode) {
urlParts.query.uselang = urlParts.siteLanguage || this.languageCode;
}
}
urlParts.pathname = '/w/api.php';
urlParts.protocol = sanitizedHost.protocol;
opt.addCorsOrigin = true;
break;
case 'wikifile:':
// wikifile:///Einstein_1921.jpg
// Get an image for the graph, e.g. from commons, by using Special:Redirect
urlParts.pathname = '/wiki/Special:Redirect/file' + urlParts.pathname;
urlParts.protocol = sanitizedHost.protocol;
// keep urlParts.query
break;
case 'wikirawupload:':
// wikirawupload://upload.wikimedia.org/wikipedia/commons/3/3e/Einstein_1921.jpg
// Get an image for the graph, e.g. from commons
// This tag specifies any content from the uploads.* domain, without query params
this._validateExternalService(urlParts, sanitizedHost, opt.url);
urlParts.query = {};
// keep urlParts.pathname
break;
case 'wikidatasparql:':
// wikidatasparql:///?query=<QUERY>
// Runs a SPARQL query, converting it to
// https://query.wikidata.org/bigdata/namespace/wdq/sparql?format=json&query=...
this._validateExternalService(urlParts, sanitizedHost, opt.url);
if (!urlParts.query || !urlParts.query.query) {
throw new Error('wikidatasparql: missing query parameter in: ' + opt.url);
}
// Only keep the "query" parameter
urlParts.query = {query: urlParts.query.query};
urlParts.pathname = '/bigdata/namespace/wdq/sparql';
opt.headers = this.objExtender(opt.headers || {}, {'Accept': 'application/sparql-results+json'});
break;
case 'geoshape:':
case 'geoline:':
// geoshape:///?ids=Q16,Q30 or geoshape:///?query=...
// Get geoshapes data from OSM database by supplying Wikidata IDs
// https://maps.wikimedia.org/shape?ids=Q16,Q30
// 'geoline:' is an identical service, except that it returns lines instead of polygons
this._validateExternalService(urlParts, sanitizedHost, opt.url, 'geoshape:');
if (!urlParts.query || (!urlParts.query.ids && !urlParts.query.query)) {
throw new Error(opt.graphProtocol + ' missing ids or query parameter in: ' + opt.url);
}
// the query object is not modified
urlParts.pathname = '/' + removeColon(opt.graphProtocol);
break;
case 'mapsnapshot:':
// mapsnapshot:///?width=__&height=__&zoom=__&lat=__&lon=__ [&style=__]
// Converts it into a snapshot image request for Kartotherian:
// https://maps.wikimedia.org/img/{style},{zoom},{lat},{lon},{width}x{height}[@{scale}x].{format}
// (scale will be set to 2, and format to png)
if (!urlParts.query) {
throw new Error('mapsnapshot: missing required parameters');
}
validate(urlParts, 'width', 1, 4096);
validate(urlParts, 'height', 1, 4096);
validate(urlParts, 'zoom', 0, 22);
validate(urlParts, 'lat', -90, 90, true);
validate(urlParts, 'lon', -180, 180, true);
var query = urlParts.query;
if (query.style && !/^[-_0-9a-z]+$/.test(query.style)) {
throw new Error('mapsnapshot: if style is given, it must be letters/numbers/dash/underscores only');
}
if (query.lang && !/^[-_0-9a-zA-Z]+$/.test(query.lang)) {
throw new Error('mapsnapshot: if lang is given, it must be letters/numbers/dash/underscores only');
}
// Uses the same configuration as geoshape service, so reuse settings
this._validateExternalService(urlParts, sanitizedHost, opt.url, 'geoshape:');
urlParts.pathname = '/img/' + (query.style || 'osm-intl') + ',' + query.zoom + ',' +
query.lat + ',' + query.lon + ',' + query.width + 'x' + query.height + '@2x.png';
urlParts.query = {}; // deleting it would cause errors in mw.Uri()
if (query.lang) {
urlParts.query.lang = query.lang;
}
break;
default:
throw new Error('Unknown protocol ' + opt.url);
}
}
return this.formatUrl(urlParts, opt);
};
function validate(urlParts, name, min, max, isFloat) {
var value = urlParts.query[name];
if (value === undefined) {
throw new Error(urlParts.protocol + ' parameter ' + name + ' is not set');
}
if (!(isFloat ? /^-?[0-9]+\.?[0-9]*$/ : /^-?[0-9]+$/).test(value)) {
throw new Error(urlParts.protocol + ' parameter ' + name + ' is not a number');
}
value = isFloat ? parseFloat(value) : parseInt(value);
if (value < min || value > max) {
throw new Error(urlParts.protocol + ' parameter ' + name + ' is not valid');
}
}
VegaWrapper.prototype._validateExternalService = function _validateExternalService(urlParts, sanitizedHost, url, protocolOverride) {
var protocol = protocolOverride || urlParts.protocol,
domains = this._getProtocolDomains(protocol);
if (!domains) {
throw new Error(protocol + ': protocol is disabled: ' + url);
}
if (urlParts.isRelativeHost) {
urlParts.host = domains[0];
urlParts.protocol = this.sanitizeHost(urlParts.host).protocol;
} else {
urlParts.protocol = sanitizedHost.protocol;
}
if (!this.testHost(protocol, urlParts.host)) {
throw new Error(protocol + ': URL must either be relative (' + protocol + '///...), or use one of the allowed hosts: ' + url);
}
};
/**
* Performs post-processing of the data requested by the graph's spec
*/
VegaWrapper.prototype.dataParser = function dataParser(error, data, opt, callback) {
if (!error) {
try {
data = this.parseDataOrThrow(data, opt);
} catch (e) {
error = e;
}
}
if (error) data = undefined;
callback(error, data);
};
/**
* Parses the response from MW Api, throwing an error or logging warnings
*/
VegaWrapper.prototype.parseMWApiResponse = function parseMWApiResponse(data) {
data = JSON.parse(data);
if (data.error) {
throw new Error('API error: ' + JSON.stringify(data.error));
}
if (data.warnings) {
this.logger('API warnings: ' + JSON.stringify(data.warnings));
}
return data;
};
/**
* Performs post-processing of the data requested by the graph's spec, and throw on error
*/
VegaWrapper.prototype.parseDataOrThrow = function parseDataOrThrow(data, opt) {
switch (opt.graphProtocol) {
case 'wikiapi:':
data = this.parseMWApiResponse(data);
break;
case 'wikiraw:':
data = this.parseMWApiResponse(data);
try {
data = data.query.pages[0].revisions[0].content;
} catch (e) {
throw new Error('Page content not available ' + opt.url);
}
break;
case 'tabular:':
case 'map:':
data = this.parseMWApiResponse(data).jsondata;
var metadata = [{
description: data.description,
license_code: data.license.code,
license_text: data.license.text,
license_url: data.license.url,
sources: data.sources
}];
if (opt.graphProtocol === 'tabular:') {
var fields = data.schema.fields.map(function (v) {
return v.name;
});
data = {
meta: metadata,
fields: data.schema.fields,
data: data.data.map(function (v) {
var row = {}, i;
for (i = 0; i < fields.length; i++) {
// Need to copy nulls too -- Vega has no easy way to test for undefined
row[fields[i]] = v[i];
}
return row;
})
}
} else {
metadata[0].zoom = data.zoom;
metadata[0].latitude = data.latitude;
metadata[0].longitude = data.longitude;
data = {
meta: metadata,
data: data.data
};
}
break;
case 'wikidatasparql:':
data = JSON.parse(data);
if (!data.results || !Array.isArray(data.results.bindings)) {
throw new Error('SPARQL query result does not have "results.bindings"');
}
data = data.results.bindings.map(function (row) {
var key, result = {};
for (key in row) {
if (row.hasOwnProperty(key)) {
result[key] = parseWikidataValue(row[key]);
}
}
return result;
});
break;
}
return data;
};
/**
* Throw an error when called
*/
function alwaysFail() {
throw new Error('Disabled');
}