-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from rodolfoviolac/feat/v1.2.0
feat: fixes for null objects and new features
- Loading branch information
Showing
13 changed files
with
3,230 additions
and
2,111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,76 @@ | ||
import {IBlurSettings, TTargetFieldType} from './typings/interfaces'; | ||
import {securityObjectFieldCleaner} from "./utils/objectHandler"; | ||
import {customSensitiveFields} from "./utils/sensitiveFields"; | ||
import {securityStringFieldCleaner} from "./utils/stringHandler"; | ||
import {stringPatterns} from "./utils/stringsPatterns"; | ||
import {EStringPatterns} from "./typings/enums"; | ||
import { IBlurSettings, TTargetFieldType } from './typings/interfaces'; | ||
import securityObjectFieldCleaner from './utils/objectHandler'; | ||
import { customSensitiveFields } from './utils/sensitiveFields'; | ||
import { securityStringFieldCleaner } from './utils/stringHandler'; | ||
import { stringPatterns } from './utils/stringsPatterns'; | ||
import { EStringPatterns } from './typings/enums'; | ||
|
||
const sensitiveFields = require('sensitive-fields'); | ||
|
||
export class Obfuscator { | ||
static readonly EStringLookUpFields = EStringPatterns; | ||
readonly EStringLookUpFields = Obfuscator.EStringLookUpFields; | ||
|
||
blurSettings: IBlurSettings = { | ||
replacerText: "NOT_VISIBLE_SECURITY_REASON", | ||
stringPatterns: [EStringPatterns.CPF, EStringPatterns.CNPJ, EStringPatterns.RG, EStringPatterns.PHONE, EStringPatterns.CREDIT_CARD] | ||
} | ||
|
||
constructor(blurSettings?: IBlurSettings) { | ||
Object.assign(this.blurSettings, blurSettings); | ||
} | ||
|
||
public blur(rawData: TTargetFieldType){ | ||
const rawDataType = typeof rawData | ||
|
||
switch (rawDataType) { | ||
case 'object': | ||
return this.handleObjectData(rawData as object); | ||
case 'string': | ||
return this.handleStringData(rawData as string); | ||
default: | ||
throw new Error('Data type not supported') | ||
} | ||
} | ||
|
||
private handleObjectData(rawData: object): object{ | ||
const lookUpFields = [...customSensitiveFields, ...sensitiveFields, ...this.blurSettings?.additionalObjectKeys || []] | ||
return securityObjectFieldCleaner(rawData, lookUpFields, this.blurSettings.replacerText) | ||
} | ||
|
||
private handleStringData(rawData: string): string{ | ||
const lookUpFieldsPatterns = this.handleStringPatterns(); | ||
return securityStringFieldCleaner(rawData, lookUpFieldsPatterns, this.blurSettings.replacerText); | ||
} | ||
|
||
|
||
private handleStringPatterns(): RegExp[] { | ||
let lookUpFields = [...this.blurSettings.additionalStringPatterns || []]; | ||
for(const pattern of this.blurSettings.stringPatterns || []){ | ||
lookUpFields.push(stringPatterns[pattern]) | ||
} | ||
return lookUpFields; | ||
} | ||
static readonly EStringLookUpFields = EStringPatterns; | ||
|
||
readonly EStringLookUpFields = Obfuscator.EStringLookUpFields; | ||
|
||
blurSettings: IBlurSettings = { | ||
replacerText: 'NOT_VISIBLE_SECURITY_REASON', | ||
stringPatterns: [ | ||
EStringPatterns.CPF, | ||
EStringPatterns.CNPJ, | ||
EStringPatterns.RG, | ||
EStringPatterns.PHONE, | ||
EStringPatterns.CREDIT_CARD, | ||
EStringPatterns.UUID, | ||
EStringPatterns.OBJECT_ID, | ||
], | ||
}; | ||
|
||
constructor(blurSettings?: IBlurSettings) { | ||
Object.assign(this.blurSettings, blurSettings); | ||
} | ||
|
||
public blur(rawData: TTargetFieldType) { | ||
const rawDataType = typeof rawData; | ||
|
||
if (!rawData) throw new Error('Blur data type null or undefined is not supported'); | ||
|
||
switch (rawDataType) { | ||
case 'object': | ||
return this.handleObjectData(rawData as Record<string, unknown>); | ||
case 'string': | ||
return this.handleStringData(rawData as string); | ||
default: | ||
throw new Error(`Blur data type ${rawDataType} not supported`); | ||
} | ||
} | ||
|
||
private handleObjectData(rawData: Record<string, unknown>): Record<string, unknown> { | ||
const lookUpFields = [ | ||
...customSensitiveFields, | ||
...sensitiveFields, | ||
...(this.blurSettings?.additionalObjectKeys || []), | ||
]; | ||
return securityObjectFieldCleaner(rawData, lookUpFields, this.blurSettings.replacerText); | ||
} | ||
|
||
private handleStringData(rawData: string): string { | ||
const lookUpFieldsPatterns = this.handleStringPatterns(); | ||
return securityStringFieldCleaner( | ||
rawData, | ||
lookUpFieldsPatterns, | ||
this.blurSettings.replacerText, | ||
); | ||
} | ||
|
||
private handleStringPatterns(): RegExp[] { | ||
const lookUpFields = [...(this.blurSettings.additionalStringPatterns || [])]; | ||
for (const pattern of this.blurSettings.stringPatterns || []) { | ||
lookUpFields.push(stringPatterns[pattern]); | ||
} | ||
return lookUpFields; | ||
} | ||
} | ||
|
||
module.exports = { | ||
Obfuscator | ||
} | ||
Obfuscator, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
export enum EStringPatterns { | ||
CPF = 'cpf', | ||
CNPJ = 'cnpj', | ||
RG = 'rg', | ||
CEP = 'cep', | ||
PHONE = 'phone', | ||
CREDIT_CARD ='creditCard', | ||
CPF = 'cpf', | ||
CNPJ = 'cnpj', | ||
RG = 'rg', | ||
CEP = 'cep', | ||
PHONE = 'phone', | ||
CREDIT_CARD = 'creditCard', | ||
UUID = 'uuid', | ||
OBJECT_ID = 'objectId', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import {EStringPatterns} from "./enums"; | ||
import { EStringPatterns } from './enums'; | ||
|
||
export interface IBlurSettings { | ||
additionalObjectKeys?: string[]; | ||
additionalStringPatterns? : RegExp[] | ||
stringPatterns? : EStringPatterns[]; | ||
replacerText?: string; | ||
additionalObjectKeys?: string[]; | ||
additionalStringPatterns?: RegExp[]; | ||
stringPatterns?: EStringPatterns[]; | ||
replacerText?: string; | ||
} | ||
|
||
export type TTargetFieldType = string | object; | ||
export type TTargetFieldType = string | Record<string, unknown>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,35 @@ | ||
function removeCircularDependency() { | ||
const seen = new WeakSet(); | ||
return (key: any, value: any) => { | ||
const valueNotSeen = !seen.has(value); | ||
if (typeof value === 'object' && value !== null && valueNotSeen) { | ||
seen.add(value); | ||
} | ||
return value; | ||
}; | ||
const seen = new WeakSet(); | ||
return (key: any, value: any) => { | ||
const valueNotSeen = !seen.has(value); | ||
if (typeof value === 'object' && value !== null && valueNotSeen) { | ||
seen.add(value); | ||
} | ||
return typeof value === 'undefined' ? null : value; | ||
}; | ||
} | ||
|
||
function objectReplacer(objectToReplace: any, lookFor: string[], stringReplacer: string): object { | ||
for (const [key] of Object.entries(objectToReplace)) { | ||
if(lookFor.includes(key)){ | ||
objectToReplace[key] = stringReplacer; | ||
} | ||
if(typeof objectToReplace[key] === 'object'){ | ||
objectReplacer(objectToReplace[key], lookFor, stringReplacer) | ||
} | ||
} | ||
return objectToReplace; | ||
function objectReplacer( | ||
objectToReplace: any, | ||
lookFor: string[], | ||
stringReplacer: string, | ||
): Record<string, unknown> { | ||
Object.keys(objectToReplace).forEach(key => { | ||
if (lookFor.includes(key)) { | ||
objectToReplace[key] = stringReplacer; | ||
} | ||
if (typeof objectToReplace[key] === 'object' && objectToReplace[key] !== null) { | ||
objectReplacer(objectToReplace[key], lookFor, stringReplacer); | ||
} | ||
}); | ||
return objectToReplace; | ||
} | ||
|
||
|
||
export function securityObjectFieldCleaner(objectTarget: any, fieldsToLookFor: string[], stringReplacer: string = ''): object { | ||
const cleanObject = JSON.parse(JSON.stringify(objectTarget, removeCircularDependency())); | ||
return objectReplacer(cleanObject, fieldsToLookFor, stringReplacer); | ||
export default function securityObjectFieldCleaner( | ||
objectTarget: any, | ||
fieldsToLookFor: string[], | ||
stringReplacer = '', | ||
): Record<string, unknown> { | ||
const cleanObject = JSON.parse(JSON.stringify(objectTarget, removeCircularDependency())); | ||
return objectReplacer(cleanObject, fieldsToLookFor, stringReplacer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
export const customSensitiveFields = [ | ||
'token', | ||
'authorization', | ||
'cpf', | ||
'CPF', | ||
'senha', | ||
]; | ||
export const customSensitiveFields = ['token', 'authorization', 'cpf', 'CPF', 'senha']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
export function securityStringFieldCleaner(rawData: string, lookUpFieldsPatterns: RegExp[], stringReplacer: string = ''){ | ||
let filteredString = rawData | ||
for(const pattern of lookUpFieldsPatterns) { | ||
filteredString = filteredString.replace(pattern,stringReplacer); | ||
} | ||
return filteredString; | ||
export function securityStringFieldCleaner( | ||
rawData: string, | ||
lookUpFieldsPatterns: RegExp[], | ||
stringReplacer = '', | ||
) { | ||
let filteredString = rawData; | ||
for (const pattern of lookUpFieldsPatterns) { | ||
filteredString = filteredString.replace(pattern, stringReplacer); | ||
} | ||
return filteredString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
export const stringPatterns = { | ||
cpf: /([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})/igm, | ||
cnpj: /\d{2}\.?\d{3}\.?\d{3}\/?\d{4}\-?\d{2}/igm, | ||
rg: /[0-9]{2,3}\.?[0-9]{2,3}\.?[0-9]{3}\-?[A-Za-z0-9]{1}/igm, | ||
cep: /([0-9]{5})-?([0-9]{3})/igm, | ||
phone: /(\(?\d{2}\)?\s)?(\d{4,5}\-?\d{4})/igm, | ||
creditCard: /\b(3[47]\d{2}([ -]?)(?!(\d)\3{5}|123456|234567|345678)\d{6}\2(?!(\d)\4{4})\d{5}|((4\d|5[1-5]|65)\d{2}|6011)([ -]?)(?!(\d)\8{3}|1234|3456|5678)\d{4}\7(?!(\d)\9{3})\d{4}\7\d{4})\b|(606282\d{10}(\d{3})?)|(3841\d{15})|((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})/igm | ||
} | ||
cpf: /([0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2})|([0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2})/gim, | ||
cnpj: /\d{2}\.?\d{3}\.?\d{3}\/?\d{4}\-?\d{2}/gim, | ||
rg: /[0-9]{2,3}\.?[0-9]{2,3}\.?[0-9]{3}\-?[A-Za-z0-9]{1}/gim, | ||
cep: /([0-9]{5})-?([0-9]{3})/gim, | ||
phone: /(\(?\d{2}\)?\s)?(\d{4,5}\-?\d{4})/gim, | ||
creditCard: /\b(3[47]\d{2}([ -]?)(?!(\d)\3{5}|123456|234567|345678)\d{6}\2(?!(\d)\4{4})\d{5}|((4\d|5[1-5]|65)\d{2}|6011)([ -]?)(?!(\d)\8{3}|1234|3456|5678)\d{4}\7(?!(\d)\9{3})\d{4}\7\d{4})\b|(606282\d{10}(\d{3})?)|(3841\d{15})|((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})/gim, | ||
uuid: /[0-F]{8}-([0-F]{4}-){3}[0-F]{12}/gim, | ||
objectId: /[0-9a-f]{24}/gim, | ||
}; |
Oops, something went wrong.