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

refactor: improved message obfuscation by randomizing length and handling special characters (#16345) #16584

Merged
merged 1 commit into from
Jan 22, 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
10 changes: 7 additions & 3 deletions src/script/util/StringUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ export const getRandomChar = (): string => {
};

export const obfuscate = (text: string): string => {
/* cspell:disable-next-line */
const alphabet = Array.from('abcdefghijklmnopqrstuvwxyz');
return Array.from(text, char => (/\s/.test(char) ? char : randomElement(alphabet))).join('');
const alphabet = Array.from('abcdefghijklmnopqrstuvwxyz ');

const obfuscatedText = Array.from({length: text.length + Math.floor((1 + Math.random()) * 10)}, () =>
randomElement(alphabet),
).join('');

return obfuscatedText;
};

/**
Expand Down
18 changes: 8 additions & 10 deletions test/unit_tests/util/StringUtilSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ describe('StringUtil', () => {
});

describe('obfuscate', () => {
it("obfuscates a text preserving it's whitespaces", () => {
const text = 'You Are The Sunshine Of My Life';
const obfuscated = obfuscate(text);
const whitespaces = obfuscated.match(/[\n\r\s]+/gi);

expect(obfuscated).not.toBe(text);
expect(whitespaces.length).toBe(6);
it('obfuscates a text returning a text with greater length', () => {
expect(obfuscate('a').length).toBeGreaterThan(1);
expect(obfuscate('ab').length).toBeGreaterThan(2);
expect(
obfuscate(
'Bacon ipsum dolor amet sausage landjaeger ball tip brisket filet mignon, t-bone tenderloin tri-tip beef drumstick fatback burgdoggen ground round meatball. Tri-tip spare ribs ground round bresaola ball tip tail, sirloin chicken doner boudin turkey leberkas bacon alcatra. ',
).length,
).toBeGreaterThan(272);
});

it('obfuscates a text keeping its length', () => {
Expand All @@ -101,23 +102,20 @@ describe('StringUtil', () => {
const obfuscated = obfuscate(text);

expect(obfuscated).not.toBe(text);
expect(obfuscated.length).toBe(text.length);
});

it('obfuscates a text keeping its length (commas)', () => {
const text = ',,,,,,';
const obfuscated = obfuscate(text);

expect(obfuscated).not.toBe(text);
expect(obfuscated.length).toBe(text.length);
});

it('obfuscates a text keeping its length (dots)', () => {
const text = '......';
const obfuscated = obfuscate(text);

expect(obfuscated).not.toBe(text);
expect(obfuscated.length).toBe(text.length);
});
});

Expand Down
Loading