-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
253 lines (236 loc) · 7.29 KB
/
index.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
'use strict';
const debug = require('debug')('protocol-handler');
const pCatchIf = require('p-catch-if');
const pTry = require('p-try');
const reProtocol = /^([a-z0-9.+-]+:)/i;
const BLACKLISTED_PROTOCOLS = ['http:', 'https:', 'file:', 'blob:', 'about:'];
const defineProperty = (obj, name, value) => Object.defineProperty(obj, name, { value });
const isProtocolRelative = url => url.trim().startsWith('//');
/**
* Custom error indicating invalid, unknown or blacklisted protocol
* @augments Error
*/
class ProtocolError extends Error {
/**
*
* @param {ProtocolError.code} code Error code
* @param {String} message Error message
*/
constructor(code, message) {
super(message);
this.code = code;
}
toJSON() {
return {
name: this.name,
code: this.code,
message: this.message
};
}
}
defineProperty(ProtocolError.prototype, 'name', ProtocolError.name);
/**
* @typedef {Object} ProtocolError.code
* @property {Number} ERR_PROTOCOL_INVALID
* @property {Number} ERR_PROTOCOL_UNKNOWN
* @property {Number} ERR_PROTOCOL_BLACKLISTED
*/
ProtocolError.ERR_PROTOCOL_INVALID = -1;
ProtocolError.ERR_PROTOCOL_UNKNOWN = 1;
ProtocolError.ERR_PROTOCOL_BLACKLISTED = 2;
/**
* Create protocol handler
* @class
*/
class ProtocolHandler {
/**
* @constructor
* @param {ProtocolHandlerOptions} [options={}] protocol handler options
*/
constructor({ blacklist = [] } = {}) {
this._blacklist = [...BLACKLISTED_PROTOCOLS, ...blacklist];
this._handlers = new Map();
}
/**
* Registers protocol handler
* @param {String} scheme protocol scheme
* @param {ProtocolCallback} handler protocol handler
* @returns {ProtocolHandler} instance to allow chaining
* @throws {ProtocolError} throws if protocol scheme is invalid or blacklisted
*
* @example
* // register multiple handlers
* const handler = new ProtocolHandler();
* handler
* .protocol('s3://', resolve)
* .protocol('gdrive://', resolve);
*/
protocol(scheme, handler) {
debug('atempt register scheme: %s', scheme);
const protocol = getProtocol(scheme);
if (!protocol) {
throw new ProtocolError(
ProtocolError.ERR_PROTOCOL_INVALID,
`Invalid protocol: \`${scheme}\``
);
}
debug('protocol=%s', protocol);
if (this._blacklist.includes(protocol)) {
throw new ProtocolError(
ProtocolError.ERR_PROTOCOL_BLACKLISTED,
`Registering handler for \`${scheme}\` is not allowed.`
);
}
this._handlers.set(protocol, handler);
debug('scheme registered: %s', scheme);
return this;
}
/**
* @property {Set<String>} protocols registered protocols
*
* @example
* // check if protocol is registered
* const handler = new ProtocolHandler();
* handler.protocol('s3://', resolve);
* console.log(handler.protocols.has('s3:'));
* //=> true
*/
get protocols() {
return new Set(this._handlers.keys());
}
async _resolve(url) {
debug('url=%s', url);
if (url && isProtocolRelative(url)) return url;
const protocol = getProtocol(url);
if (!protocol) {
throw new ProtocolError(
ProtocolError.ERR_PROTOCOL_INVALID,
`Invalid url: \`${url}\``
);
}
debug('protocol=%s', protocol);
const handler = this._handlers.get(protocol);
if (handler) {
const resolvedUrl = await pTry(() => handler(url));
debug('resolved url=%s', resolvedUrl || '');
return resolvedUrl;
}
if (this._blacklist.includes(protocol)) {
throw new ProtocolError(
ProtocolError.ERR_PROTOCOL_BLACKLISTED,
`Blacklisted protocol: \`${protocol}\``
);
}
throw new ProtocolError(
ProtocolError.ERR_PROTOCOL_UNKNOWN,
`Unknown protocol: \`${protocol}\``
);
}
/**
* Asynchronously resolves url with registered protocol handler
* @param {String} url target url
* @returns {Promise<String>} resolved url, redirect location
* @throws {ProtocolError} throws if url contains invalid or unknown protocol
*
* @example
* // create handler
* const handler = new ProtocolHandler();
* handler.protocol('s3://', url => 'https://example.com');
* // resolve url
* handler.resolve('s3://test').then(url => console.log(url));
* //=> https://example.com
* handler.resolve('file:///local/file.txt').then(url => console.log(url));
* //=> file:///local/file.txt
* handler.resolve('dummy://unknown/protocol');
* //=> throws ProtocolError
*/
resolve(url) {
return this._resolve(url).catch(pCatchIf(isBlacklisted, () => url));
}
/**
* Returns [Express](https://expressjs.com) middleware
* @param {String} [param='url'] name of query param containing target url
* @param {ProtocolErrorCallback} [cb] custom error handling callback
* @param {import('@types/express').RequestHandler} Express middleware
*
* @example
* // create handler
* const handler = new ProtocolHandler();
* handler.protocol('s3://', resolve);
* // attach to express app
* app.use(handler.middleware());
*/
middleware(param = 'url', cb) {
return async (req, res, next) => {
const url = decodeURIComponent(req.query[param]);
try {
const redirectUrl = await this._resolve(url, null);
debug('redirect url=%s', redirectUrl || '');
return res.redirect(redirectUrl);
} catch (err) {
if (!(err instanceof ProtocolError)) return next(err);
if (cb) return cb(err, url, res);
if (isBlacklisted(err)) return res.redirect(url);
res.status(400).json({ error: err });
}
};
}
}
/**
* Create new ProtocolHandler instance
* @name module.exports
* @param {ProtocolHandlerOptions} [options={}] protocol handler options
* @returns {ProtocolHandler} instance
*
* @example
* const handler = require('custom-protocol-handler')();
*/
module.exports = options => new ProtocolHandler(options);
module.exports.ProtocolHandler = ProtocolHandler;
module.exports.ProtocolError = ProtocolError;
function getProtocol(str) {
const match = str.trim().match(reProtocol);
return match && match[0];
}
function isBlacklisted(err) {
return err instanceof ProtocolError &&
err.code === ProtocolError.ERR_PROTOCOL_BLACKLISTED;
}
/**
* @typedef {Object} ProtocolHandlerOptions
* @property {Array<String>} [blacklist=[]] array of blacklisted schemes
*/
/**
* Resolver function for specific protocol
* @callback ProtocolCallback
* @param {String} url target url
* @returns {String|Promise<String>} resolved url _redirect location_
*
* @example
* // Resolve gdrive urls
* const { fetchInfo } = require('gdrive-file-info');
*
* async function resolve(url) {
* const itemId = new URL(url).pathname;
* const fileInfo = await fetchInfo(itemId);
* return fileInfo.downloadUrl;
* }
*/
/**
* Custom error calback for Express middleware
* @callback ProtocolErrorCallback
* @param {ProtocolError} err protocol error
* @param {String} url target url
* @param {import('@types/express').Response} res middleware response
*
* @example
* const handler = new ProtocolHandler();
* handler.protocol('s3://', resolve);
* // Act as passthrough proxy for blacklisted protocols
* app.use(handler.middleware('url', (err, url, res) => {
* if (err.code !== ProtocolError.ERR_PROTOCOL_BLACKLISTED) {
* return res.sendStatus(400);
* }
* res.redirect(url);
* }));
*/