Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing double-hashing for TikTok Conversions. #2033

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { formatEmails, formatPhones } from '../formatter'

describe('Tiktok Conversions Formatter', () => {
describe('formatEmails', () => {
it('should hash and encode email addresses', () => {
const emails = ['[email protected]', '[email protected]']
const result = formatEmails(emails)
expect(result).toEqual([
'67e28cdcc3e845d3a4da05ca9fe5ddb7320a83b4cc2167f0555a3b04f63511e3',
'2d2fb2388f17f86affee71d632978425a3037fa8eed5b3f2baaa458c80d495ed'
])
})

it('should not hash and encode already hashed email addresses', () => {
const emails = [
'67e28cdcc3e845d3a4da05ca9fe5ddb7320a83b4cc2167f0555a3b04f63511e3',
'2d2fb2388f17f86affee71d632978425a3037fa8eed5b3f2baaa458c80d495ed'
]
const result = formatEmails(emails)
expect(result).toEqual(emails)
})
})

describe('formatPhones', () => {
it('should hash and encode phone numbers', () => {
const phones = ['12345678901234', '98765432109876']
const result = formatPhones(phones)
expect(result).toEqual([
'00a63bd0437d6ca0fa7b95c00ab2e7c020faa71440e5246750d6b517689e6777',
'ce7b12d132a021393e793d21d6e8e673ac06042922b73cc10f2d7db597657a4a'
])
})

it('should not hash and encode already hashed phone numbers', () => {
const phones = [
'c17c025fb9ed44eae8a9d5c9df0312af5c6161bd79bd669692364fc5ecaf108a',
'5a1b78d720b151af8e69fde486784c1c279996813d20d23badbcce1e1037ee91'
]
const result = formatPhones(phones)
expect(result).toEqual(phones)
})
})
})
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { createHash } from 'crypto'

const isHashedInformation = (information: string): boolean => new RegExp(/[0-9abcdef]{64}/gi).test(information)

/**
* Convert emails to lower case, and hash in SHA256.
*/
export const formatEmails = (email_addresses: string[] | undefined): string[] => {
const result: string[] = []
if (email_addresses) {
email_addresses.forEach((email: string) => {
result.push(hashAndEncode(email.toLowerCase()))
let resolvedEmail
if (isHashedInformation(email)) {
resolvedEmail = email
} else {
resolvedEmail = hashAndEncode(email.toLowerCase())
}

result.push(resolvedEmail)
})
}
return result
Expand All @@ -23,6 +32,11 @@ export const formatPhones = (phone_numbers: string[] | undefined): string[] => {
if (!phone_numbers) return result

phone_numbers.forEach((phone: string) => {
if (isHashedInformation(phone)) {
result.push(phone)
return
}

const validatedPhone = phone.match(/[0-9]{0,14}/g)
if (validatedPhone === null) {
throw new Error(`${phone} is not a valid E.164 phone number.`)
Expand Down
Loading