-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidator.js
More file actions
242 lines (201 loc) · 8.42 KB
/
validator.js
File metadata and controls
242 lines (201 loc) · 8.42 KB
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
var B = require('bluebird');
var superagent = require('superagent');
var childProcess = require('child_process');
var fs = require('fs');
var path = require('path');
// Override the default behavior of superagent, which encodes to UTF-8.
var _parse = function(res, done) {
res.text = '';
res.setEncoding('binary');
res.on('data', function(chunk) { res.text += chunk; });
res.on('end', done);
};
function _verifyJsonFormat(aasa) {
var applinks = aasa.applinks;
if (!applinks) {
return false;
}
var details = applinks.details;
if (!details) {
return false;
}
// Domains are an array: [ { appID: '01234567890.com.foo.FooApp', paths: [ '*' ] } ]
if (details instanceof Array) {
for (var i = 0; i < details.length; i++) {
var domain = details[i];
if (!(typeof domain.appID === 'string' && domain.paths instanceof Array)) {
return false;
}
}
}
// Domains are an object: { '01234567890.com.foo.FooApp': { paths: [ '*' ] } }
else {
for (var domain in details) {
if (!(details[domain].paths instanceof Array)) {
return false;
}
}
}
return true;
}
function _verifyBundleIdentifierIsPresent(aasa, bundleIdentifier, teamIdentifier) {
var regexString = bundleIdentifier.replace(/\./g, '\\.') + '$';
if (teamIdentifier) {
regexString = teamIdentifier + '\\.' + regexString;
}
var identifierRegex = new RegExp(regexString);
var details = aasa.applinks.details;
// Domains are an array: [ { appID: '01234567890.com.foo.FooApp', paths: [ '*' ] } ]
if (details instanceof Array) {
for (var i = 0; i < details.length; i++) {
var domain = details[i];
if (identifierRegex.test(domain.appID) && domain.paths instanceof Array) {
return true;
}
}
}
// Domains are an object: { '01234567890.com.foo.FooApp': { paths: [ '*' ] } }
else {
for (var domain in details) {
if (identifierRegex.test(domain) && details[domain].paths instanceof Array) {
return true;
}
}
}
return false;
}
function _evaluateAASA(content, bundleIdentifier, teamIdentifier, encrypted) {
return new B(function(resolve, reject) {
try {
var domainAASAValue = JSON.parse(content);
// Make sure format is good.
var jsonValidationResult = _verifyJsonFormat(domainAASAValue);
// Only check bundle identifier if json is good and a bundle identifier to test against is present
var bundleIdentifierResult;
if (jsonValidationResult && bundleIdentifier) {
bundleIdentifierResult =_verifyBundleIdentifierIsPresent(domainAASAValue, bundleIdentifier, teamIdentifier);
}
resolve({ encrypted: encrypted, aasa: domainAASAValue, jsonValid: jsonValidationResult, bundleIdentifierFound: bundleIdentifierResult });
}
catch (e) {
reject(e);
}
});
}
function _validate(fileUrl, bundleIdentifier, teamIdentifier) {
return new B(function(resolve, reject) {
var errorObj = { };
superagent
.get(fileUrl)
.timeout(3000)
.buffer()
.parse(_parse)
.end(function(err, res) {
if (err && !res) {
// Unable to resolve DNS name
if (err.code == 'ENOTFOUND') {
errorObj.badDns = true;
}
// Doesn't support HTTPS
else if (err.code == 'ECONNREFUSED' || /Hostname\/IP doesn't match certificate's altnames/.test(err.message)) {
errorObj.badDns = false;
errorObj.httpsFailure = true;
}
else {
errorObj.errorOutOfScope = true;
}
reject(errorObj);
}
else {
errorObj.badDns = false;
errorObj.httpsFailure = false;
var isValidMimeType = res.headers['content-type'] !== undefined &&
(res.headers['content-type'].indexOf('application/pkcs7-mime') > -1 ||
res.headers['content-type'].indexOf('application/octet-stream') > -1 ||
res.headers['content-type'].indexOf('application/json') > -1 ||
res.headers['content-type'].indexOf('text/json') > -1 ||
res.headers['content-type'].indexOf('text/plain') > -1 );
// Bad server response
if (res.status >= 400) {
errorObj.serverError = true;
// Check here for alternate URL
reject(errorObj);
}
else if (!isValidMimeType) {
errorObj.serverError = false;
errorObj.badContentType = true;
reject(errorObj);
}
else {
errorObj.serverError = false;
errorObj.badContentType = false;
_evaluateAASA(res.text, bundleIdentifier, teamIdentifier, false)
.then(resolve) // Not encrypted, send it back
.catch(function() { // Nope, encrypted. Go through the rest of the process
return _parseEncryptedContentForJSON(res.text, bundleIdentifier, teamIdentifier)
})
.then(resolve)
.catch(function(err) {
errorObj.invalidJson = err.invalidJson;
reject(errorObj);
});
}
}
});
});
}
function _performRegexSearch(reg_ex_pattern, content_to_search){
var re = reg_ex_pattern;
var str = content_to_search;
var regex_result;
if ((regex_result = re.exec(str)) !== null) {
if (regex_result.index === re.lastIndex) {
re.lastIndex++;
}
}
return regex_result;
}
function _parseEncryptedContentForJSON(content,bundleIdentifier,teamIdentifier){
return new B(function(resolve,reject){
try {
var applinks = _performRegexSearch(/{{1}\s*"{1}applinks"{1}/, content);
if(applinks && applinks.index){
content = content.substr(applinks.index, content.length-1);
}
var webcredentials = _performRegexSearch(/{{1}\s*"{1}webcredentials"{1}/, content);
if(webcredentials && webcredentials.index){
content = content.substr(webcredentials.index, content.length-1);
}
var activityContinuation = _performRegexSearch(/{{1}\s*"{1}activitycontinuation"{1}/, content);
if(activityContinuation && activityContinuation.index){
content = content.substr(activityContinuation.index, content.length-1);
}
if(!applinks && !webcredentials && !activityContinuation){
reject({ invalidJson: true });
return;
}
var open_braces = 0, close_braces = 0;
for(var i = 0; i < content.length; i++){
if(content.charAt(i) == '{'){
open_braces = open_braces + 1;
}
if(content.charAt(i) == '}'){
close_braces = close_braces + 1;
}
if(open_braces == close_braces){
var aasa_string = content.substr(0, i+1);
return _evaluateAASA(aasa_string, bundleIdentifier, teamIdentifier, true)
.then(resolve)
.catch(function() {
reject({ invalidJson: true });
return;
});
}
}
}catch(err){
reject({ invalidJson: true });
return;
}
});
}
module.exports.validate = _validate;