Skip to content

Commit 04bf351

Browse files
committed
Santization Logic Changes
1 parent c7babde commit 04bf351

1 file changed

Lines changed: 12 additions & 85 deletions

File tree

src/sso/sso.service.ts

Lines changed: 12 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,6 @@ export class SsoService {
311311

312312
//Map User to Role and Tenant
313313

314-
// Sanitize userId for use in custom fields
315-
const sanitizedNewtonUserId = this.sanitizeUUID(newtonResponse.userId) || createdUser.userId;
316-
317314
// Update custom fields if newtonData is available
318315
if (newtonResponse.newtonData && Object.keys(newtonResponse.newtonData).length > 0) {
319316
for (const [fieldLabel, fieldValue] of Object.entries(newtonResponse.newtonData)) {
@@ -328,8 +325,8 @@ export class SsoService {
328325
{
329326
tenantId: ssoRequestDto.tenantId,
330327
contextType: "USER",
331-
createdBy: sanitizedNewtonUserId,
332-
updatedBy: sanitizedNewtonUserId
328+
createdBy: createdUser.userId,
329+
updatedBy: createdUser.userId
333330
}
334331
);
335332
}
@@ -364,45 +361,6 @@ export class SsoService {
364361
}
365362
}
366363

367-
/**
368-
* Sanitize and validate UUID by removing invalid characters
369-
* @param uuid - UUID string that may contain invalid characters
370-
* @returns Sanitized UUID string or null if invalid
371-
*/
372-
private sanitizeUUID(uuid: string | undefined | null): string | null {
373-
if (!uuid || typeof uuid !== 'string') {
374-
return null;
375-
}
376-
377-
// Remove any trailing/leading whitespace and invalid characters
378-
// UUID format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
379-
// Remove any characters that are not valid UUID characters (hex digits and hyphens)
380-
let sanitized = uuid.trim();
381-
382-
// Remove any trailing special characters (like asterisks, spaces, etc.)
383-
sanitized = sanitized.replace(/[^a-fA-F0-9-]+$/, '');
384-
385-
// Remove any leading special characters
386-
sanitized = sanitized.replace(/^[^a-fA-F0-9]+/, '');
387-
388-
// Remove any invalid characters within the UUID (keep only hex digits and hyphens)
389-
sanitized = sanitized.replace(/[^a-fA-F0-9-]/g, '');
390-
391-
// Validate UUID format: should be 8-4-4-4-12 hex digits separated by hyphens
392-
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
393-
394-
if (uuidRegex.test(sanitized)) {
395-
return sanitized;
396-
}
397-
398-
this.logger.warn(
399-
`Invalid UUID format after sanitization: ${uuid} -> ${sanitized}`,
400-
'SSO_SERVICE'
401-
);
402-
403-
return null;
404-
}
405-
406364
/**
407365
* Map Newton API response to UserCreateDto with name parsing
408366
* @param newtonResponse - Newton API response
@@ -423,31 +381,22 @@ export class SsoService {
423381
EMAIL: newtonResponse.email || ''
424382
};
425383

426-
// Sanitize userId from Newton response or userData
427-
const rawUserId = newtonResponse.userId || userData.USER_ID;
428-
const sanitizedUserId = this.sanitizeUUID(rawUserId);
384+
// Get userId from Newton response or userData
385+
const userId = newtonResponse.userId || userData.USER_ID;
429386

430-
if (!sanitizedUserId) {
387+
if (!userId) {
431388
this.logger.error(
432-
`Invalid userId received from Newton API: ${rawUserId}. Cannot create user.`,
389+
`No userId received from Newton API. Cannot create user.`,
433390
'SSO_SERVICE'
434391
);
435392
throw new HttpException(
436-
`Invalid user ID format received from authentication service: ${rawUserId}`,
393+
`User ID not found in authentication response`,
437394
HttpStatus.BAD_REQUEST
438395
);
439396
}
440397

441-
// Log if sanitization changed the value
442-
if (rawUserId !== sanitizedUserId) {
443-
this.logger.warn(
444-
`UserId sanitized: "${rawUserId}" -> "${sanitizedUserId}"`,
445-
'SSO_SERVICE'
446-
);
447-
}
448-
449398
return {
450-
userId: sanitizedUserId,
399+
userId: userId,
451400
username: newtonResponse.email || userData.EMAIL,
452401
firstName: firstName,
453402
middleName: undefined,
@@ -463,8 +412,8 @@ export class SsoService {
463412
password: undefined, // No password for SSO users
464413
createdAt: new Date().toISOString(),
465414
updatedAt: new Date().toISOString(),
466-
createdBy: sanitizedUserId, // Use the sanitized Newton user ID as creator
467-
updatedBy: sanitizedUserId, // Use the sanitized Newton user ID as updater
415+
createdBy: userId, // Use the Newton user ID as creator
416+
updatedBy: userId, // Use the Newton user ID as updater
468417
customFields: [],
469418
automaticMember: undefined,
470419
tenantCohortRoleMapping: [{
@@ -599,38 +548,16 @@ export class SsoService {
599548

600549

601550
// Use decoded userId if available, otherwise fallback to newtonResponse.userId or ssoRequestDto.userId
602-
const rawUserId = newtonResponse.userId || ssoRequestDto.userId;
551+
const userId = newtonResponse.userId || ssoRequestDto.userId;
603552

604-
if (!rawUserId) {
553+
if (!userId) {
605554
this.logger.error('No userId available for existing user', 'SSO_SERVICE');
606555
throw new HttpException(
607556
'Unable to determine user ID for existing user',
608557
HttpStatus.BAD_REQUEST
609558
);
610559
}
611560

612-
// Sanitize userId to remove any invalid characters
613-
const userId = this.sanitizeUUID(rawUserId);
614-
615-
if (!userId) {
616-
this.logger.error(
617-
`Invalid userId format for existing user: ${rawUserId}`,
618-
'SSO_SERVICE'
619-
);
620-
throw new HttpException(
621-
`Invalid user ID format: ${rawUserId}`,
622-
HttpStatus.BAD_REQUEST
623-
);
624-
}
625-
626-
// Log if sanitization changed the value
627-
if (rawUserId !== userId) {
628-
this.logger.warn(
629-
`UserId sanitized for existing user: "${rawUserId}" -> "${userId}"`,
630-
'SSO_SERVICE'
631-
);
632-
}
633-
634561
this.logger.log(
635562
`Processing existing user: ${userId}`,
636563
'SSO_SERVICE'

0 commit comments

Comments
 (0)