Skip to content

Commit

Permalink
feat: add support for .log and .json files
Browse files Browse the repository at this point in the history
  • Loading branch information
Psingle20 committed Oct 26, 2024
1 parent 2e77117 commit c449a9c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 51 deletions.
3 changes: 1 addition & 2 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"


npx --no -- commitlint --edit ${1} && npm run lint
2 changes: 1 addition & 1 deletion src/proxy/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const pushActionChain = [
proc.push.pullRemote,
proc.push.writePack,
proc.push.getDiff,
proc.push.checkSensitiveData, //checkSensitiveData added
proc.push.checkSensitiveData, // checkSensitiveData added
proc.push.clearBareClone,
proc.push.scanDiff,
proc.push.blockForAuth,
Expand Down
43 changes: 24 additions & 19 deletions src/proxy/processors/push-action/checkSensitiveData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const fs = require('fs');
const csv = require('csv-parser');
const XLSX = require('xlsx');
const path = require('path');
const { exec: getDiffExec } = require('./getDiff');

// const { exec: getDiffExec } = require('./getDiff');
// Function to check for sensitive data patterns
const checkForSensitiveData = (cell) => {
const sensitivePatterns = [
Expand All @@ -20,12 +19,10 @@ const checkForSensitiveData = (cell) => {
return false;
});
};

// Function to process CSV files
const processCSV = async (filePath) => {
return new Promise((resolve, reject) => {
let sensitiveDataFound = false;

fs.createReadStream(filePath)
.pipe(csv())
.on('data', (row) => {
Expand All @@ -48,18 +45,15 @@ const processCSV = async (filePath) => {
});
});
};

// Function to process XLSX files
const processXLSX = async (filePath) => {
return new Promise((resolve, reject) => {
let sensitiveDataFound = false;

try {
const workbook = XLSX.readFile(filePath);
const sheetName = workbook.SheetNames[0];
const sheet = workbook.Sheets[sheetName];
const jsonData = XLSX.utils.sheet_to_json(sheet);

jsonData.forEach((row) => {
for (const [key, value] of Object.entries(row)) {
if (checkForSensitiveData(value)) {
Expand All @@ -68,7 +62,6 @@ const processXLSX = async (filePath) => {
}
}
});

if (!sensitiveDataFound) {
console.log('No sensitive data found in XLSX.');
}
Expand All @@ -79,7 +72,23 @@ const processXLSX = async (filePath) => {
}
});
};

// Function to check for sensitive data in .log and .json files
const checkLogJsonFiles = async (filePath) => {
return new Promise((resolve, reject) => {
let sensitiveDataFound = false;
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file ${filePath}: ${err.message}`);
return reject(err);
}
if (checkForSensitiveData(data)) {
console.log(`\x1b[33mSensitive data found in ${filePath}\x1b[0m`);
sensitiveDataFound = true;
}
resolve(sensitiveDataFound);
});
});
};
// Function to parse the file based on its extension
const parseFile = async (filePath) => {
const ext = path.extname(filePath).toLowerCase();
Expand All @@ -89,28 +98,26 @@ const parseFile = async (filePath) => {
return await processCSV(filePath);
case '.xlsx':
return await processXLSX(filePath);
case '.log':
return await checkLogJsonFiles(filePath);
case '.json':
return await checkLogJsonFiles(filePath);
default:
console.log(`Unsupported file type: ${ext} for file: ${filePath}`);
// Skip unsupported file types without logging
return false; // Indicate that no sensitive data was found for unsupported types
}
};

// Async exec function to handle actions
const exec = async (req, action) => {
// getDiffExec(req, action); // Call to getDiffExec if necessary

const diffStep = action.steps.find((s) => s.stepName === 'diff');

if (diffStep && diffStep.content) {
console.log('Diff content:', diffStep.content);

const filePaths = diffStep.content.filePaths || [];

if (filePaths.length > 0) {
// Check for sensitive data in all files
const sensitiveDataFound = await Promise.all(filePaths.map(parseFile));
const anySensitiveDataDetected = sensitiveDataFound.some(found => found); // Check if any file reported sensitive data

if (anySensitiveDataDetected) {
action.pushBlocked = true; // Block the push
action.error = true; // Set error flag
Expand All @@ -123,9 +130,7 @@ const exec = async (req, action) => {
} else {
console.log('No diff content available.');
}

return action; // Returning action for testing purposes
};

exec.displayName = 'logFileChanges.exec';
exports.exec = exec;
exports.exec = exec;
54 changes: 32 additions & 22 deletions test/CheckSensitive.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
const path = require('path');
const { exec } = require('../src/proxy/processors/push-action/checkSensitiveData.js'); // Adjust path as necessary
const sinon = require('sinon');

describe('Sensitive Data Detection', () => {
let logStub;

beforeEach(() => {
logStub = sinon.stub(console, 'log'); // Stub console.log before each test
});

afterEach(() => {
logStub.restore(); // Restore console.log after each test
});

it('should detect sensitive data in CSV file and block execution', async () => {
// Set up the action with the correct file path
const action = {
steps: [{
stepName: 'diff',
Expand All @@ -23,20 +18,12 @@ describe('Sensitive Data Detection', () => {
}
}]
};

// Call exec with necessary parameters
await exec(null, action); // Ensure exec is awaited if it's a promise

// Capture logged messages for debugging after exec execution
await exec(null, action);
const loggedMessages = logStub.getCalls().map(call => call.args[0]);
console.log('Captured log messages for CSV:', loggedMessages);

// Check if the blocking message is logged
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
});

it('should detect sensitive data in XLSX file and block execution', async () => {
// Set up the action with the correct file path for XLSX
const action = {
steps: [{
stepName: 'diff',
Expand All @@ -45,15 +32,38 @@ describe('Sensitive Data Detection', () => {
}
}]
};

// Call exec with necessary parameters
await exec(null, action); // Ensure exec is awaited if it's a promise

// Capture logged messages for debugging after exec execution
await exec(null, action);
const loggedMessages = logStub.getCalls().map(call => call.args[0]);
console.log('Captured log messages for XLSX:', loggedMessages);

// Check if the blocking message is logged
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
});
});
it('should detect sensitive data in a log file and block execution', async () => {
const action = {
steps: [{
stepName: 'diff',
content: {
filePaths: [path.join(__dirname, 'test_data/sensitive_data3.log')] // Ensure this path is correct
}
}]
};
await exec(null, action);
const loggedMessages = logStub.getCalls().map(call => call.args[0]);
console.log('Captured log messages for log file:', loggedMessages);
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
});
it('should detect sensitive data in a JSON file and block execution', async () => {
const action = {
steps: [{
stepName: 'diff',
content: {
filePaths: [path.join(__dirname, 'test_data/sensitive_data4.json')] // Ensure this path is correct
}
}]
};
await exec(null, action);
const loggedMessages = logStub.getCalls().map(call => call.args[0]);
console.log('Captured log messages for JSON file:', loggedMessages);
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
});

});
7 changes: 1 addition & 6 deletions test/CreateExcel.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
const XLSX = require('xlsx');
const fs = require('fs');
const path = require('path');

// Example data with sensitive information
const data = [
{ Name: "John Doe", SSN: "123-45-6789", Email: "[email protected]" },
{ Name: "Jane Smith", SSN: "987-65-4321", Email: "[email protected]" }
];

const worksheet = XLSX.utils.json_to_sheet(data);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "SensitiveData");

// Create the path to the test_data directory
const testDataPath = path.join(__dirname, 'test_data'); // Ensure this points to the correct directory

// Create the test_data directory if it doesn't exist
if (!fs.existsSync(testDataPath)){
fs.mkdirSync(testDataPath, { recursive: true }); // Using recursive to ensure all directories are created
}

// Write the Excel file to the test_data directory
XLSX.writeFile(workbook, path.join(testDataPath, 'sensitive_data2.xlsx'));
XLSX.writeFile(workbook, path.join(testDataPath, 'sensitive_data2.xlsx'));
2 changes: 1 addition & 1 deletion test/chain.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const chai = require('chai');
const sinon = require('sinon');
const { PluginLoader } = require('../src/plugin');
const { checkSensitiveData } = require('../src/proxy/processors/push-action');


chai.should();
const expect = chai.expect;
Expand Down
4 changes: 4 additions & 0 deletions test/test_data/sensitive_data4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username": "johndoe",
"ssn": "123-45-6789"
}

0 comments on commit c449a9c

Please sign in to comment.