Skip to content

Commit 76e671d

Browse files
Merge pull request #11 from socketlabs/hotfix/SS-1041
Enforce serverId as interger type
2 parents 7cd82b5 + 7f88c5c commit 76e671d

File tree

1 file changed

+45
-48
lines changed

1 file changed

+45
-48
lines changed

src/core/sendValidator.js

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const {ToBulkRecipient, ToEmailAddress, ToCustomHeader} = require('../helpers/helpersClasses');
3+
const { ToBulkRecipient, ToEmailAddress, ToCustomHeader } = require('../helpers/helpersClasses');
44

55
const sendResponse = require('../sendResponse');
66
const addressResult = require('../addressResult');
@@ -12,17 +12,16 @@ const MaximumRecipientsPerMessage = 50;
1212
* Used by the SocketLabsClient to conduct basic validation on the message before sending to the Injection API.
1313
*/
1414
class SendValidator {
15-
15+
1616
/**
1717
* Validate the ServerId and Api Key pair prior before sending to the Injection API.
1818
* @param {number} serverId - Your SocketLabs ServerId number.
1919
* @param {string} apiKey - Your SocketLabs Injection API key.
2020
* @returns A SendResponse with the validation results
2121
*/
22-
validateCredentials(serverId, apiKey)
23-
{
22+
validateCredentials(serverId, apiKey) {
2423
var result = new sendResponse();
25-
24+
2625
//String given
2726
if (typeof apiKey !== 'string') {
2827
result.setResult(sendResultEnum.AuthenticationValidationFailed);
@@ -33,17 +32,17 @@ class SendValidator {
3332
result.setResult(sendResultEnum.AuthenticationValidationFailed);
3433
return result;
3534
}
36-
35+
3736
//int given
38-
if (typeof serverId !== 'number') {
37+
if (Number.isInteger(serverId)) {
3938
result.setResult(sendResultEnum.AuthenticationValidationFailed);
4039
return result;
4140
}
4241

4342
if (serverId <= 0) {
4443
result.setResult(sendResultEnum.AuthenticationValidationFailed);
4544
return result;
46-
}
45+
}
4746

4847
result.setResult(sendResultEnum.Success);
4948
return result;
@@ -70,7 +69,7 @@ class SendValidator {
7069
result.setResult(sendResultEnum.EmailAddressValidationMissingFrom);
7170
return result;
7271
}
73-
if (!from.isValid()){
72+
if (!from.isValid()) {
7473
result.setResult(sendResultEnum.EmailAddressValidationInvalidFrom);
7574
return result;
7675
}
@@ -95,7 +94,7 @@ class SendValidator {
9594
}
9695
}
9796

98-
// Check email addresses
97+
// Check email addresses
9998
return this.validateEmailAddresses(message.to, message.cc, message.bcc);
10099

101100
}
@@ -121,7 +120,7 @@ class SendValidator {
121120
result.setResult(sendResultEnum.EmailAddressValidationMissingFrom);
122121
return result;
123122
}
124-
if (!from.isValid()){
123+
if (!from.isValid()) {
125124
result.setResult(sendResultEnum.EmailAddressValidationInvalidFrom);
126125
return result;
127126
}
@@ -151,7 +150,7 @@ class SendValidator {
151150

152151
}
153152

154-
153+
155154
/**
156155
* Check if the fromAddress is not empty
157156
* @param {emailAddress} fromAddress - the from email address
@@ -161,12 +160,12 @@ class SendValidator {
161160
if (typeof value === 'undefined' || !value) {
162161
return false;
163162
}
164-
if (value && value === '') {
163+
if (value && value === '') {
165164
return false;
166165
}
167-
return true;
166+
return true;
168167
}
169-
168+
170169
/**
171170
* Check if the fromAddress is not empty
172171
* @param {emailAddress} fromAddress - the from email address
@@ -176,7 +175,7 @@ class SendValidator {
176175
if (typeof value === 'undefined' || !value) {
177176
return false;
178177
}
179-
if (value && value.emailAddress === ''){
178+
if (value && value.emailAddress === '') {
180179
return false;
181180
}
182181
return true;
@@ -189,16 +188,16 @@ class SendValidator {
189188
*/
190189
isValidReplyTo(value) {
191190
if (typeof value === 'undefined' || !value) {
192-
return true; // undefined is allowed
191+
return true; // undefined is allowed
193192
}
194193
var replyTo = ToEmailAddress.convert(value);
195-
if (replyTo && replyTo.emailAddress === '' && replyTo.friendlyName === ''){
194+
if (replyTo && replyTo.emailAddress === '' && replyTo.friendlyName === '') {
196195
return true;
197196
}
198-
return replyTo.isValid();
197+
return replyTo.isValid();
199198
}
200199

201-
200+
202201
/**
203202
* Check if the message has a Message Body
204203
* If an Api Template is specified it will override the HtmlBody, and/or the textBody.
@@ -209,11 +208,11 @@ class SendValidator {
209208
* @returns {bool} the validation result
210209
*/
211210
hasMessageBody(apiTemplate, htmlBody, textBody) {
212-
211+
213212
if ((typeof apiTemplate !== 'undefined' || apiTemplate) &&
214-
(typeof apiTemplate === 'number') &&
213+
(typeof apiTemplate === 'number') &&
215214
(apiTemplate > 0)) {
216-
return true;
215+
return true;
217216
}
218217

219218
return (this.hasValidString(htmlBody) || this.hasValidString(textBody));
@@ -224,10 +223,10 @@ class SendValidator {
224223
* @param {customHeader[]} value - array of customHeader
225224
* @returns {bool} the validation result
226225
*/
227-
hasValidCustomHeaders(value) {
226+
hasValidCustomHeaders(value) {
228227
if (typeof value === 'undefined' || !value) {
229-
return true;
230-
}
228+
return true;
229+
}
231230
if (!Array.isArray(value)) {
232231
return false;
233232
}
@@ -238,7 +237,7 @@ class SendValidator {
238237
try {
239238
value.forEach(element => {
240239
if (!ToCustomHeader.convert(element).isValid())
241-
return false;
240+
return false;
242241
});
243242
} catch (error) {
244243
return false;
@@ -276,7 +275,7 @@ class SendValidator {
276275
result.addressResults = invalidRec;
277276
return result;
278277
}
279-
278+
280279
result.setResult(sendResultEnum.Success);
281280
return result;
282281

@@ -292,7 +291,7 @@ class SendValidator {
292291

293292
var fullRecipientCount = 0;
294293
if ((typeof to !== 'undefined' || to) && (Array.isArray(to))) {
295-
fullRecipientCount = to.length;
294+
fullRecipientCount = to.length;
296295
}
297296
if (fullRecipientCount <= 0) {
298297
result.setResult(sendResultEnum.RecipientValidationNoneInMessage);
@@ -302,14 +301,14 @@ class SendValidator {
302301
result.setResult(sendResultEnum.RecipientValidationMaxExceeded);
303302
return result;
304303
}
305-
304+
306305
var invalidRec = this.hasInvalidBulkRecipients(to);
307-
if (invalidRec != null && invalidRec.length > 0) {
306+
if (invalidRec != null && invalidRec.length > 0) {
308307
result.setResult(sendResultEnum.RecipientValidationInvalidRecipients);
309308
result.addressResults = invalidRec;
310309
return result;
311310
}
312-
311+
313312
result.setResult(sendResultEnum.Success);
314313
return result;
315314
}
@@ -334,7 +333,7 @@ class SendValidator {
334333
if ((typeof bcc !== 'undefined' || bcc) && (Array.isArray(bcc))) {
335334
recipientCount += bcc.length;
336335
}
337-
return recipientCount;
336+
return recipientCount;
338337
}
339338

340339
/**
@@ -348,18 +347,18 @@ class SendValidator {
348347
var invalid = [];
349348
if ((typeof to !== 'undefined' || to) && (Array.isArray(to))) {
350349
this.findInvalidEmailAddresses(to).forEach(e => {
351-
invalid.push(e)
352-
});
350+
invalid.push(e)
351+
});
353352
}
354353
if ((typeof cc !== 'undefined' || cc) && (Array.isArray(cc))) {
355354
this.findInvalidEmailAddresses(cc).forEach(e => {
356-
invalid.push(e)
357-
});
355+
invalid.push(e)
356+
});
358357
}
359358
if ((typeof bcc !== 'undefined' || bcc) && (Array.isArray(bcc))) {
360359
this.findInvalidEmailAddresses(bcc).forEach(e => {
361-
invalid.push(e)
362-
});
360+
invalid.push(e)
361+
});
363362
}
364363
return invalid;
365364
}
@@ -386,20 +385,19 @@ class SendValidator {
386385
try {
387386
value.forEach(element => {
388387
var e = ToEmailAddress.convert(element);
389-
if (!e.isValid())
390-
{
388+
if (!e.isValid()) {
391389
invalid.push(new addressResult(
392390
{
393391
accepted: false,
394392
emailAddress: e.emailAddress,
395393
errorCode: "InvalidAddress"
396394
})
397395
);
398-
}
396+
}
399397
});
400398
} catch (error) {
401399
return invalid;
402-
}
400+
}
403401
}
404402
return invalid;
405403
}
@@ -415,20 +413,19 @@ class SendValidator {
415413
try {
416414
value.forEach(element => {
417415
var e = ToBulkRecipient.convert(element);
418-
if (!e.isValid())
419-
{
420-
invalid.push(new addressResult(
416+
if (!e.isValid()) {
417+
invalid.push(new addressResult(
421418
{
422419
accepted: false,
423420
emailAddress: e.emailAddress,
424421
errorCode: "InvalidAddress"
425422
})
426423
);
427-
}
424+
}
428425
});
429426
} catch (error) {
430427
return invalid;
431-
}
428+
}
432429
}
433430
return invalid;
434431
}

0 commit comments

Comments
 (0)