1
+ const URL = require ( 'url' ) ;
2
+
1
3
function incorrectParams ( error ) {
2
4
return {
3
5
statusCode : 501 , // oembed status // 422, // Unprocessable Entity
@@ -8,19 +10,23 @@ function incorrectParams(error) {
8
10
9
11
function getHostname ( event , context ) {
10
12
if ( event . headers . host ) {
11
- return `http ://${ event . headers . host } ` ;
13
+ return `https ://${ event . headers . host } ` ;
12
14
}
13
15
14
16
const { netlify } = context . clientContext . custom || { } ;
15
17
16
18
return JSON . parse ( Buffer . from ( netlify , 'base64' ) . toString ( 'utf-8' ) ) . site_url ;
17
19
}
18
20
21
+ const allowedPathsRegexp = new RegExp ( / ^ \/ ( g i s t | e m b e d ) \/ .* / ) ;
22
+
19
23
function handler ( event , context , callback ) {
20
24
const host = getHostname ( event , context ) ;
25
+
21
26
const params = event . queryStringParameters ;
27
+ const { format, referrer, maxwidth = 900 , maxheight = 450 } = params ;
22
28
23
- if ( params . format === 'xml ') {
29
+ if ( format && format !== 'json ') {
24
30
return callback (
25
31
null ,
26
32
incorrectParams ( 'unsupported format, only json is supported' ) ,
@@ -34,7 +40,22 @@ function handler(event, context, callback) {
34
40
) ;
35
41
}
36
42
37
- const { url, referrer, maxwidth = 900 , maxheight = 450 } = params ;
43
+ const { hostname, pathname } = URL . parse ( params . url ) ;
44
+
45
+ // verify if the url is supported, basically we only allow localhost if we're
46
+ // running at localhost, and testing-playground.com as host. And either no
47
+ // path or /gist and /embed paths.
48
+ if (
49
+ ( ! host . includes ( hostname ) && hostname !== 'testing-playground.com' ) ||
50
+ ( pathname && ! allowedPathsRegexp . test ( pathname ) )
51
+ ) {
52
+ return callback ( null , incorrectParams ( 'unsupported url provided :/' ) ) ;
53
+ }
54
+
55
+ // map /gist urls to /embed
56
+ const url = pathname . startsWith ( '/gist/' )
57
+ ? params . url . replace ( '/gist/' , '/embed/' )
58
+ : params . url ;
38
59
39
60
callback ( null , {
40
61
statusCode : 200 ,
0 commit comments