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

Anonymization in/out #76

Merged
merged 28 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7aa6a2d
Delete compose-dev.yaml
AJun01 Jun 27, 2024
4d31657
feat(anonymization): improved anonymization feature
AJun01 Jun 29, 2024
ead1c9c
feat(anonymization): utilizing anonymization feature
AJun01 Jul 3, 2024
8ca9be9
fixed testing issue
AJun01 Jul 3, 2024
c49ffa7
feat(anonymization): improved testing
AJun01 Jul 4, 2024
c709f23
file rename: renamed simple-request(anonymized) to simple-request_ano…
AJun01 Jul 6, 2024
bd09f7e
merging branch 'develop-yujun' into AJun01/fix-issue-71 to fix the is…
AJun01 Jul 8, 2024
cc8fc07
Address PR review comments
AJun01 Jul 20, 2024
c38f915
Rename anonymized_Form-test.js to anonymized-Form-test.js
AJun01 Jul 20, 2024
7f66f82
Rename anonymized_JSON-test.js to anonymized-JSON-test.js
AJun01 Jul 20, 2024
0647219
Deleted gh.args(true) statement
AJun01 Jul 20, 2024
045b37f
Merge branch 'AJun01/fix-issue-71' of https://github.com/AJun01/devel…
AJun01 Jul 20, 2024
f21db70
feat(anonmization) refactored main.js and anonymizer.js
AJun01 Jul 25, 2024
90b6726
Address PR review comments
AJun01 Jul 28, 2024
7c52f85
minor fix
AJun01 Jul 29, 2024
96aa252
Rename anonymized-Form-test.js to anonymized-form-test.js
AJun01 Aug 2, 2024
c7d16f4
Rename anonymized-JSON-test.js to anonymized-json-test.js
AJun01 Aug 2, 2024
db957a0
minor fix: addressed comments and feedbacks
AJun01 Aug 2, 2024
bdaeb31
Merge branch 'AJun01/fix-issue-71' of https://github.com/AJun01/devel…
AJun01 Aug 2, 2024
029150e
removed console logs
AJun01 Aug 12, 2024
a933a97
fixed running error
AJun01 Aug 13, 2024
822239d
addressing the running error for simple-request-anonymized.js
AJun01 Aug 14, 2024
9d251fd
Added anonymization feature run test to README.md for Flexbench
AJun01 Aug 14, 2024
c31074b
fixed addressing running error
AJun01 Aug 14, 2024
11bae7c
improved anonymizeRequest in anonymizer
AJun01 Aug 14, 2024
d882d80
addresed minor issues
AJun01 Aug 17, 2024
234b4b9
addressed format and removed commented codes
AJun01 Aug 19, 2024
7c854da
addressed testing issues
AJun01 Aug 20, 2024
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
109 changes: 109 additions & 0 deletions lib/anonymizer.js
iskitsas marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const multiparty = require('multiparty');

const emailRegex = /([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/g;
const fullNameRegex = /\b([A-Z][a-z]+ [A-Z][a-z]+)\b/g;
const singleNameRegex = /\b([A-Z][a-z]+)\b/g;
const addressRegex = /\b(\d{1,5}\s\w+(?:\s\w+)*(?:\s(Street|St|Avenue|Ave|Boulevard|Blvd|Road|Rd|Lane|Ln|Drive|Dr|Court|Ct|Circle|Cir))?)\b/g;
const phoneRegex = /(\+?\d{1,4}[\s-]?)?(\(?\d{3}\)?[\s-]?)?[\d\s-]{7,10}/g;
const urlRegex = /https?:\/\/[^\s/$.?#].[^\s]*/g;
const stateRegex = /\b[A-Z]{2}\b/g;
iskitsas marked this conversation as resolved.
Show resolved Hide resolved
const zipRegex = /\b\d{5}(?:-\d{4})?\b/g;
iskitsas marked this conversation as resolved.
Show resolved Hide resolved
const passwordRegex = /(?<=name="password"[\s\S]*?\r\n\r\n)([A-Za-z0-9]+[A-Za-z0-9]*|[A-Za-z0-9]*[A-Za-z0-9]+)/g;

//default
let anonymizationEnabled = true;
mariosk marked this conversation as resolved.
Show resolved Hide resolved

//Text
function anonymizeText(text) {
if (typeof text !== 'string') {
return text;
}
text = text.replace(emailRegex, '[email protected]');
text = text.replace(phoneRegex, '000-000-0000');
text = text.replace(urlRegex, 'https://anonymized-url.com');
text = text.replace(fullNameRegex, 'Anonymized Name');
text = text.replace(singleNameRegex, (match, p1, offset, string) => {
if (fullNameRegex.test(string.substring(offset - p1.length, offset + p1.length))) {
return match;
}
return 'Anonymized Name';
});
text = text.replace(passwordRegex, 'anonymized_password');
text = text.replace(/\bAnonymized Name Anonymized Name\b/g, 'Anonymized Name');
return text;
}

//Object
function anonymizeObject(obj) {
if (!anonymizationEnabled) {
return obj;
}

for (let key in obj) {
mariosk marked this conversation as resolved.
Show resolved Hide resolved
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
anonymizeObject(obj[key]);
} else if (Array.isArray(obj[key])) {
obj[key] = obj[key].map(item => anonymizeText(item));
} else {
if (key === 'street' || key === 'city' || key === 'country') {
mariosk marked this conversation as resolved.
Show resolved Hide resolved
obj[key] = 'Anonymized Address';
} else if (key === 'state') {
obj[key] = 'XX';
} else if (key === 'zip') {
obj[key] = '00000';
} else {
obj[key] = anonymizeText(obj[key]);
}
}
}
}
return obj;
}

//Form
function anonymizeFormData(req, callback) {
if (!anonymizationEnabled) {
return callback(null, req);
}

const form = new multiparty.Form();
const fields = {};
const files = {};

form.on('field', (name, value) => {
if (name === 'password') {
fields[name] = 'anonymized_password';
} else {
fields[name] = anonymizeText(value);
}
});

form.on('file', (name, file) => {
files[name] = file;
});

form.on('close', () => {
callback(null, { fields, files });
});

form.on('error', (err) => {
callback(err, null);
});

form.parse(req);
}

//flag setter
function setAnonymization(enabled) {
anonymizationEnabled = enabled;
}


module.exports = {
anonymizeObject,
anonymizeFormData,
anonymizeText,
setAnonymization
};

40 changes: 33 additions & 7 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var http = require('http'),
gh = require('./lib-helper.js'),
proxy = require('./ThrottleProxy/throttleServer.js'),
config = require('./config.js');
anonymizer = require('../lib/anonymizer.js');



Expand Down Expand Up @@ -56,6 +57,9 @@ var timers=[];
var minDelay;//ms
var maxDelay;//ms
var isRandomDelay=false;

var anonymizationEnabled = gh.args(true);
mariosk marked this conversation as resolved.
Show resolved Hide resolved

/**
* END of Properties section
* *************************
Expand All @@ -76,6 +80,11 @@ var EVENT={
error:'error'
}

function anonymizeMode(flag) {
iskitsas marked this conversation as resolved.
Show resolved Hide resolved
anonymizationEnabled = flag;
mariosk marked this conversation as resolved.
Show resolved Hide resolved
anonymizer.setAnonymization(flag);
}

function incrCounter(id){
if(countObj[id]!=null){
countObj[id]++;
Expand Down Expand Up @@ -408,6 +417,11 @@ function request(options,cb){
}else{
_options=options;
}

if (_options.body && anonymizationEnabled) {
_options.body = anonymizer.anonymizeText(_options.body);
}

let protocol;
if (options.port === "443" && !isThrottled()) {
_options['agent'] = gh.keepAliveAgentHttps;
Expand All @@ -417,19 +431,30 @@ function request(options,cb){
_options['agent'] = gh.keepAliveAgentHttp;
protocol = http;
}
var req = protocol.request(_options, function (response) {
response.on('end', function () {
var req = protocol.request(_options, function(response) {
let responseBody = '';

response.on('data', function(chunk) {
responseBody += chunk;
});

response.on('end', function() {
incrCounter(response.statusCode);
if (anonymizationEnabled) {
responseBody = anonymizer.anonymizeText(responseBody);
}
if (cb) {
cb(response, responseBody);
mariosk marked this conversation as resolved.
Show resolved Hide resolved
}
});

if(cb){
cb(response);
}
response.resume();
});
if(options.body){
req.write(options.body)

if (options.body) {
req.write(options.body);
mariosk marked this conversation as resolved.
Show resolved Hide resolved
}

req.end();
return req;
}
Expand Down Expand Up @@ -549,6 +574,7 @@ exports.randomDelayBetweenRequests = function(value){randomDelayBetweenRequests=
exports.throttleRequests_bps = function(value){throttleRequests_bps=value};
exports.isPipelinedRequest = function(value){isPipelinedRequest=value};
exports.debugMode = function(value){debugMode=value; gh.consoleDebug(value); gh.shouldDebug(value)};
exports.anonymizeMode = anonymizeMode;
exports.request=request;
exports.multiRequest=multiRequest;
exports.isThrottled=isThrottled;
Expand Down
Loading