Skip to content

Commit 4c2cbbb

Browse files
authored
Merge pull request #66 from Chrilleweb/cmn/dev
dotenv-diff-ignore
2 parents 204287d + 21dd025 commit 4c2cbbb

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ This project follows [Keep a Changelog](https://keepachangelog.com/) and [Semant
1515
## [2.2.7] - 2025-09-27
1616
### Added
1717
- Added warning on .env not ignored by .gitignore on default.
18+
- added `dotenv-diff-ignore` comment to ignore lines from secret detection.
1819

1920
### Fixed
2021
- Fixed `--strict` error output to console when no warnings are found.
2122

2223
### Changed
2324
- No breaking changes.
25+
- Updated dependencies to latest versions.
2426

2527
## [2.2.6] - 2025-09-25
2628
### Added

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ You can use the `--strict` flag to treat all warnings as errors. This is useful
8282
dotenv-diff --strict
8383
```
8484

85+
## ignore specific warnings
86+
87+
You can use the `dotenv-diff-ignore` comment to ignore specific lines from secret detection. For example:
88+
89+
```js
90+
const secret ="https://thisurlshouldbeignored.com"; // dotenv-diff-ignore
91+
const ignoredEntropy = "AIzaSyA-1234567890abcdefgHIJKLMNOpqrstuv" // dotenv-diff-ignore;
92+
```
93+
94+
This will prevent `dotenv-diff` from flagging the line as a potential secret.
95+
8596
## Show unused variables
8697

8798
As default, `dotenv-diff` will list variables that are defined in `.env` but never used in your codebase.

src/core/secretDetectors.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ const HARMLESS_URLS = [
4141
/xmlns=["']http:\/\/www\.w3\.org\/2000\/svg["']/i, // SVG namespace
4242
];
4343

44+
/**
45+
* Checks if a line has an ignore comment
46+
* @param line - The line to check
47+
* @returns True if the line should be ignored
48+
*/
49+
function hasIgnoreComment(line: string): boolean {
50+
return (
51+
/\/\/\s*dotenv-diff-ignore/.test(line) ||
52+
/\/\*\s*dotenv-diff-ignore\s*\*\//.test(line)
53+
);
54+
}
55+
4456
/**
4557
* Checks if a string looks like a harmless literal.
4658
* @param s - The string to check.
@@ -133,6 +145,9 @@ export function detectSecretsInSource(
133145
// Skip comments
134146
if (/^\s*\/\//.test(line)) continue;
135147

148+
// Check if line has ignore comment
149+
if (hasIgnoreComment(line)) continue;
150+
136151
// Check for HTTPS URLs
137152
HTTPS_PATTERN.lastIndex = 0;
138153
let httpsMatch: RegExpExecArray | null;

test/e2e/cli.secrets.e2e.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,44 @@ describe('secrets detection (default scan mode)', () => {
310310
expect(res.status).toBe(0);
311311
expect(res.stdout).not.toContain('Potential secrets detected in codebase:');
312312
});
313+
it('should ignore warnings with dotenv-diff-ignore comments', () => {
314+
const cwd = tmpDir();
315+
316+
fs.writeFileSync(path.join(cwd, '.env'), 'DUMMY=\n');
317+
fs.mkdirSync(path.join(cwd, 'src'), { recursive: true });
318+
fs.writeFileSync(
319+
path.join(cwd, 'src', 'index.ts'),
320+
`
321+
// These should be flagged normally
322+
const service1 = 'https://shouldwarn.com';
323+
const secret1 = "sk_live_abcdefghijklmnopqrstuvwx";
324+
325+
// These should be ignored with comments
326+
const service2 = 'https://exdfdfdfdfdfe.com'; // dotenv-diff-ignore
327+
const service3 = "https://ignored.com/api" /* dotenv-diff-ignore */;
328+
const secret2 = "sk_live_ignoredtoken123"; // dotenv-diff-ignore
329+
const apiKey = 'AKIA1234567890IGNORE' /* dotenv-diff-ignore */;
330+
331+
// Also test high entropy strings
332+
const ignoredEntropy = "highEntropyButIgnored987654321fedcba"; // dotenv-diff-ignore
333+
334+
console.log(service1, service2, service3, secret1, secret2, apiKey, ignoredEntropy);
335+
`.trimStart(),
336+
);
337+
338+
const res = runCli(cwd, []);
339+
expect(res.status).toBe(0);
340+
expect(res.stdout).toContain('Potential secrets detected in codebase:');
341+
342+
// Should warn about the non-ignored ones
343+
expect(res.stdout).toContain('shouldwarn.com');
344+
expect(res.stdout).toContain('sk_live_abcdefghijklmnopqrstuvwx');
345+
346+
// Should NOT warn about the ignored ones
347+
expect(res.stdout).not.toContain('exdfdfdfdfdfe.com');
348+
expect(res.stdout).not.toContain('ignored.com');
349+
expect(res.stdout).not.toContain('sk_live_ignoredtoken123');
350+
expect(res.stdout).not.toContain('AKIA1234567890IGNORE');
351+
expect(res.stdout).not.toContain('highEntropyButIgnored987654321fedcba');
352+
});
313353
});

0 commit comments

Comments
 (0)