Skip to content

Commit

Permalink
Implementing whitelist (#39)
Browse files Browse the repository at this point in the history
Implementing whitelist
  • Loading branch information
hansson authored and stofolus committed Apr 4, 2019
1 parent c955177 commit d1389d5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ $ pnr-scanner .
-p, --pattern <pattern> Glob pattern default is !(node_modules){,/**}
-h, --help output usage information
```

### Whitelist
If your project has any false positives that you want to exclude from the search you can place a .pnr-whitelist file in your search root. This file should have one whitelisted pnr per line.
2 changes: 1 addition & 1 deletion lib/fileMockDataReader.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const FileMockDataReader = require("./fileMockDataReader");

describe("Validator", () => {
describe("FileMockDataReader", () => {
const fileContent = ["Testpersonnummer", "201710022383", "201801202381"].join(
"\r\n"
);
Expand Down
7 changes: 4 additions & 3 deletions lib/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const isTextOrBinary = require(`istextorbinary`);
const Validator = require(`./validator`);
const Pnr = require(`./pnr`);
const FileMockDataReader = require("./fileMockDataReader");
const WhitelistReader = require("./whitelistReader");
const Logger = require(`../lib/logger/logger`);
const logger = new Logger(`finder.js`);

const defaultPattern = `!(node_modules){,/**}`;
const validator = new Validator(FileMockDataReader.fetch());

class Finder {
static findPnrs(searchPath, pattern) {
Expand All @@ -24,18 +24,19 @@ class Finder {
} else {
files.push(searchPath);
}
const validator = new Validator([...[FileMockDataReader.fetch()], ...[WhitelistReader.fetch(searchPath)]]);

return files
.map(file => {
const results = Finder.getPnrsInFile(file);
const results = Finder.getPnrsInFile(file, validator);
return { file, results };
})
.filter(item => {
return item && item.results && item.results.length > 0;
});
}

static getPnrsInFile(file) {
static getPnrsInFile(file, validator) {
const fileBuffer = fs.readFileSync(file);
const results = [];
if (isTextOrBinary.isTextSync(path.basename(file), fileBuffer)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ describe(`findPnrs`, () => {

describe(`getPnrsInFile`, () => {
test(`Should be able to find multiple results`, () => {
const result = Finder.getPnrsInFile("/path/to/file4");
const result = Finder.getPnrsInFile("/path/to/file4", mockValidator);
expect(result.length).toBe(2);
});
test(`Should return an empty result for binary files`, () => {
const result = Finder.getPnrsInFile("/path/to/binary");
const result = Finder.getPnrsInFile("/path/to/binary", mockValidator);
expect(result.length).toBe(0);
});
test(`Should not return mocked pnrs`, () => {
const result = Finder.getPnrsInFile("/path/to/file5");
const result = Finder.getPnrsInFile("/path/to/file5", mockValidator);
expect(result.length).toBe(0);
});
});
Expand Down
18 changes: 18 additions & 0 deletions lib/whitelistReader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require(`fs`);
const path = require(`path`);
const Pnr = require(`./pnr`);

class WhitelistReader {
static fetch(rootPath) {
const whiteListPath = path.join(rootPath, `.pnr-whitelist`);
if (fs.existsSync(whiteListPath)) {
return fs.readFileSync(whiteListPath, `utf8`)
.replace(/\r/g, ``)
.split(`\n`)
.filter(line => Pnr.isValidPnr(line));
}
return [];
}
}

module.exports = WhitelistReader;
25 changes: 25 additions & 0 deletions lib/whitelistReader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fileContent = ["Testpersonnummer", "201710022383", "201801202381"].join(
"\r\n"
);
const mockFs = {
existsSync: jest.fn(file => true),
readFileSync: jest.fn(data => fileContent)
};
jest.mock("fs", () => mockFs);

const WhitelistReader = require("./whitelistReader");

describe("WhitelistReader", () => {
describe("fetch()", () => {
test("returns a list of pnrs", () => {
expect(WhitelistReader.fetch("nicePath")).toEqual(
expect.arrayContaining(["201801202381", "201710022383"])
);
});
test("can filter out non-pnr lines from mock data", () => {
expect(WhitelistReader.fetch("nicePath")).not.toContain(
"Testpersonnummer"
);
});
});
});

0 comments on commit d1389d5

Please sign in to comment.