-
Notifications
You must be signed in to change notification settings - Fork 8
/
checkRFC4566.js
368 lines (343 loc) · 13.4 KB
/
checkRFC4566.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
/* Copyright 2018 Streampunk Media Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const badEndings = /[^\r]\n|\r[^\n]/;
const hasNoNewlineAtEnd = /[^\r\n]$/;
const blankLines = /\r\n?\r|\n\r?\n/;
const linePattern = /^([a-z]=\S.*|s= )$/;
const letterCheck = /^[vosiuepcbzkatrm].*$/;
// const spaceCheck = /\s\s/;
const mustHaves = [ 'v', 'o', 's', 't' ];
const followedBy = {
session : {
'v': { 'o': 'session' },
'o': { 's': 'session' },
's': { 'i': 'session', 'u': 'session', 'e': 'session', 'p': 'session',
'c': 'session', 'b': 'session', 't': 'time' },
'i': { 'i': 'session', 'u': 'session', 'e': 'session', 'p': 'session',
'c': 'session', 'b': 'session', 't': 'time' },
'u': { 'u': 'session', 'e': 'session', 'p': 'session',
'c': 'session', 'b': 'session', 't': 'time' },
'e': { 'e': 'session', 'p': 'session',
'c': 'session', 'b': 'session', 't': 'time' },
'p': { 'p': 'session',
'c': 'session', 'b': 'session', 't': 'time' },
'c': { 'c': 'session', 'b': 'session', 't': 'time' },
'b': { 'b': 'session', 't': 'time' },
'z': { 'z': 'session', 'k' : 'session',
'a': 'session', 'm': 'media' },
'k': { 'k' : 'session',
'a': 'session', 'm': 'media' },
'a': { 'a': 'session', 'm': 'media' },
},
time : {
't': { 'r': 'time', 't': 'time', 'z': 'session', 'k' : 'session',
'a': 'session', 'm': 'media' },
'r': { 'r': 'time', 't': 'time', 'z': 'session', 'k' : 'session',
'a': 'session', 'm': 'media' }
},
media : {
'm': { 'i': 'media', 'c': 'media', 'b': 'media', 'k': 'media', 'a': 'media',
'm': 'media' },
'i': { 'i': 'media', 'c': 'media', 'b': 'media', 'k': 'media', 'a': 'media',
'm' : 'media' },
'c': { 'c': 'media', 'b': 'media', 'k': 'media', 'a': 'media',
'm' : 'media' },
'b': { 'b': 'media', 'k': 'media', 'a': 'media',
'm' : 'media' },
'k': { 'k': 'media', 'a': 'media',
'm' : 'media' },
'a': { 'a': 'media', 'm' : 'media' }
}
};
const oPattern = /^o=(\S+)\s+(\d+)\s+(\d+)\s+IN\s+(IP[4|6])\s+(\S+)$/;
const cPattern = /^c=IN\s+(IP[46])\s+([^\s/]+)(\/\d+)?(\/[1-9]\d*)?$/;
const ip4Pattern = /^([1-9]\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/;
// Tne following is a very basic test of IPv6 addresses
const ip6Pattern = /^[0-9a-f]*:[0-9a-f]*(:[0-9a-f]+)*:[0-9a-f]+$/;
const multiPattern = /^((22[4-9]|23[0-9])(\.(\d\d?\d?)){3})|(ff[0-7][123458e]::[^\s]+)$/;
// Section 5 Test 1 - check if line endings are all CRLF
const test50_1 = (sdp, params) => {
let errors = [];
if (params.checkEndings && badEndings.test(sdp)) {
errors.push(new Error('SDP file contains record ending characters CR (0x0d) and/or LF (0x0a) separately from the expected CRLF pattern, as per RFC 4566 Section 5.'));
}
return errors;
};
// Section 5 Test 1.1 - check last record is terminated with a newline and there are no blank lines
const test50_1_1 = sdp => {
let errors = [];
if (hasNoNewlineAtEnd.test(sdp)) {
errors.push(new Error('SDP file does not contain a newline character at the end of the last record, as per RFC 4566 Section 5.'));
}
if (blankLines.test(sdp)) {
errors.push(new Error('Blank lines are not permitted, as per RFC 4566 Section 5.'));
}
return errors;
};
const splitLines = sdp => sdp.match(/[^\r\n]+/g);
// Section 5 Test 2 - check each line is an acceptable format
const test_50_2 = lines => {
let errors = [];
for ( let x = 0 ; x < lines.length ; x++ ) {
if (!linePattern.test(lines[x])) {
errors.push(new Error(
`Line ${x + 1}: Every line of an SDP file must be of the form '<type>=<value>' with <type> being one character and no whitespace either side of the equals, as per RFC 4566 Section 5.`));
}
}
return errors;
};
// Section 5 Test 3 - check the only letters in use as types are defined in RFC 4566
const test_50_3 = lines => {
let errors = [];
for ( let x = 0 ; x < lines.length ; x++ ) {
if (!letterCheck.test(lines[x])) {
errors.push(new Error(
`Line ${x + 1}: Every line of an SDP file must start with a permitted type letter or the line must be ignored, as per RFC 4566 Section 5.`));
}
}
return errors;
};
// Section 5 Test 4 - Check for mandatory types
const test_50_4 = lines => {
let errors = [];
let types = lines.map(x => x.slice(0, 1));
for ( let x of mustHaves ) {
if (types.indexOf(x) < 0) {
errors.push(new Error(`An SDP file must have at least one record of type ${x} as per RFC 4566 Section 5.1.`));
}
}
return errors;
};
// Section 5 Test 5 - Check the order of the parameters
const test_50_5 = lines => {
let errors = [];
let types = lines.map(x => x.slice(0, 1));
let state = 'session';
let nextState = {};
for ( let x = 1 ; x < types.length ; x++ ) {
let movingOn = followedBy[state][types[x - 1]];
nextState = movingOn ? movingOn : nextState;
if (Object.keys(nextState).indexOf(types[x]) < 0) {
errors.push(new Error(`Line ${x + 1}: SDP type '${types[x - 1]}' cannot be followed by type '${types[x]}', as per the fixed order of RFC 4566 Section 5.`));
} else {
state = nextState[types[x]];
}
}
return errors;
};
// Section 5 Test 6 - check no values contain the Nul character
const test_50_6 = lines => {
let errors = [];
for ( let x = 0 ; x < lines.length ; x++ ) {
if (lines[x].slice(2).indexOf('\u0000') >= 0) {
errors.push(new Error(`Line ${x + 1}: Value contains illegal Nul (0x00) character not permitted by RFC 4566 Section 5.`));
}
}
return errors;
};
// Section 5.1 Test 1 - first line must be v=0
const test_51_1 = lines => {
let errors = [];
if (!lines[0].startsWith('v=0')) {
errors.push(new Error('Line 1: The first line must be \'v=0\' as per RFC 4566 Section 5.1.'));
}
return errors;
};
// Section 5.2 Test 1 - Origin name matches acceptable patterns
const test_52_1 = (sdp, params) => {
let errors = [];
let lines = splitLines(sdp);
for ( let x = 0 ; x < lines.length ; x++ ) {
if (lines[x].startsWith('o=')) {
let oMatch = lines[x].match(oPattern);
if (!oMatch) {
errors.push(new Error(`Line ${x + 1}: Origin field ("o=") is not an acceptable pattern, as per RFC 4566 Section 5.2.`));
continue;
}
if (params.useIP4 === true && oMatch[4] === 'IP6') {
errors.push(new Error(`Line ${x + 1}: Origin field specified an address type of 'IP6' when 'IP4' is requested by configuration.`));
}
if (params.useIP6 === true && oMatch[4] === 'IP4') {
errors.push(new Error(`Line ${x + 1}: Origin field specified an address type of 'IP4' when 'IP6' is requested by configuration.`));
}
if (multiPattern.test(oMatch[5])) {
errors.push(new Error(`Line ${x + 1}: Origin field address of the machine is a multicast address when it must be a unicast address.`));
}
}
}
return errors;
};
// Section 5.2 Test 2 - Check that the unicast address meets test configuration
const test_52_2 = (sdp, params) => {
if (params.useIP4 === false && params.useIP6 === false) {
return [];
}
let errors = [];
let lines = splitLines(sdp);
for ( let x = 0 ; x < lines.length ; x++ ) {
if (lines[x].startsWith('o=')) {
let oMatch = lines[x].match(oPattern);
if (!oMatch) {
continue;
}
if (params.useIP4 && !ip4Pattern.test(oMatch[5])) {
errors.push(new Error(`Line ${x + 1}: Origin field address of the machine should use IPv4 as requested by configuration.`));
}
if (params.useIP6 && !ip6Pattern.test(oMatch[5])) {
errors.push(new Error(`Line ${x + 1}: Origin field address of the machine should use IPv6 as requested by configuration.`));
}
}
}
return errors;
};
// Section 5.7 Test 1 - Must have c either at session level or for each streams
const test_57_1 = sdp => {
let errors = [];
let lines = splitLines(sdp);
let streamCount = 0;
let cFound = false;
for ( let x = 0 ; x < lines.length ; x++ ) {
if (lines[x].startsWith('m=')) {
if (cFound === false && streamCount > 0) {
errors.push(new Error(`Line ${x + 1}: For stream ${streamCount}, no connection data ("c=") field was found, as per RFC 4566 Section 5.7.`));
}
cFound = false;
streamCount++;
continue;
}
if (lines[x].startsWith('c=')) {
cFound = true;
if (streamCount === 0) {
break;
}
}
}
if (cFound === false) {
errors.push(new Error(`Line ${lines.length}: For ${streamCount ? 'stream ' + streamCount : 'all streams'}, no connection data ("c=") field was found, as per RFC 4566 Section 5.7.`));
}
return errors;
};
// Section 5.7 Test 2 - Must be IN IP4 or IN IP6, and if multicast must have valid TTL
const test_57_2 = (sdp, params) => {
let errors = [];
let lines = splitLines(sdp);
for ( let x = 0 ; x < lines.length ; x++ ) {
if (lines[x].startsWith('c=')) {
let cMatch = lines[x].match(cPattern);
if (!cMatch) {
errors.push(new Error(`Line ${x + 1}: Connection data field does not match an acceptable pattern, as per RFC 4566 Section 5.7 and ST 2110-10 Section 6.1.`));
continue;
}
if (params.useIP4 === true && cMatch[1] !== 'IP4') {
errors.push(new Error(`Line ${x + 1}: Configuration requests IPv4 but connection data field address type is '${cMatch[1]}'.`));
}
if (params.useIP6 === true && cMatch[1] !== 'IP6') {
errors.push(new Error(`Line ${x + 1}: Configuration requests IPv6 but connection data field address type is '${cMatch[1]}'.`));
}
let addrMatch = cMatch[2].match(ip4Pattern);
if (addrMatch) {
let ip0 = +addrMatch[1];
if (ip0 >= 224 && ip0 <= 239) { // Entering IPv4 Multicast land
if (typeof addrMatch[2] === 'undefined') {
errors.push(new Error(`Line ${x + 1}: Connection data fields using multicast addresses must have a TTL field, as per RFC 4566 Section 5.7.`));
} else {
let ttl = +addrMatch[2];
if (ttl < 0 || ttl > 255) {
errors.push(new Error(`Line ${x + 1}: Multicast TTL value must be in the range 0-255 and '${ttl}' provided, as per RFC 4566 Section 5.7.`));
}
// TODO check length of TTL string ... 0001 should not be acceptable
}
}
}
}
}
return errors;
};
// Section 5.7 Test 3 - Check is valid address type
const test_57_3 = (sdp, params) => {
let errors = [];
let lines = splitLines(sdp);
for ( let x = 0 ; x < lines.length ; x++ ) {
if (lines[x].startsWith('c=')) {
let addrMatch = lines[x].match(cPattern);
if (!addrMatch) {
continue;
}
if (params.useIP4 === true) {
if (!ip4Pattern.test(addrMatch[2])) {
errors.push(new Error(`Line ${x + 1}: IPv4 addresses requested by configuration and connection data field address '${addrMatch[2]}' is not.`));
}
}
if (params.useIP6 === true) {
if (!ip6Pattern.test(addrMatch[2])) {
errors.push(new Error(`Line ${x + 1}: IPv6 addresses requested by configuration and connection data field address '${addrMatch[2]}' is not.`));
}
}
if (params.multicast === true) {
if (!multiPattern.test(addrMatch[2])) {
errors.push(new Error(`Line ${x + 1}: Multicast connections requested by configuration and connection data field address '${addrMatch[2]}' is unicast.`));
}
}
if (params.unicast === true) {
if (multiPattern.test(addrMatch[2])) {
errors.push(new Error(`Line ${x + 1}: Unicast connections requested by configuration and connection data field address '${addrMatch[2]} is multicast.`));
}
}
}
}
return errors;
};
// TODO Future work - test ability to join multicast group or DNS lookup address
const section_50 = (sdp, params) => {
let endingTests = [ test50_1, test50_1_1 ];
let lines = splitLines(sdp);
let mainTests = [ test_50_2, test_50_3, test_50_4, test_50_5, test_50_6 ];
return [...endingTests.flatMap(t => t(sdp, params)), ...mainTests.flatMap(t => t(lines, params))];
};
const section_51 = (sdp, params) => {
let lines = splitLines(sdp);
return test_51_1(lines, params);
};
const section_52 = (sdp, params) => {
let tests = [ test_52_1, test_52_2 ];
return tests.flatMap(t => t(sdp, params));
};
const section_57 = (sdp, params) => {
let tests = [ test_57_1, test_57_2, test_57_3 ];
return tests.flatMap(s => s(sdp, params));
};
// Test if SDP file is missing media descriptions
const no_media = sdp => {
let lines = splitLines(sdp.trim());
let hasMedia = false;
for ( let x = 0 ; x < lines.length ; x++ ) {
if (lines[x].startsWith('m=')) {
hasMedia = true;
break;
}
}
return hasMedia ? [] : [ new Error('SDP file does not include any "m=" media attributes.') ];
};
const allSections = (sdp, params) => {
let sections = [ section_50, section_51, section_52, section_57 ];
if (params.noMedia) {
sections.push(no_media);
}
return sections.flatMap(s => s(sdp, params));
};
module.exports = {
allSections,
section_50,
section_51,
section_52,
section_57
};