Skip to content

Commit 0405c0d

Browse files
nullvariantclaude
andauthored
fix: resolve SonarCloud Medium severity issues (97 issues) (#154)
- Use node: prefix for built-in modules (fs, path, os, crypto, child_process) - Replace parseInt() with Number.parseInt() for consistency - Add explicit regex grouping with non-capturing groups - Remove nested template literals in documentation.internal.ts - Use optional chaining in flagValidator.ts - Add readonly modifier to FileLogWriter.config Files: 7 source files, 8 test files (15 total) 🖥️ IDE: [Cursor](https://cursor.sh) 🔌 Extension: [Claude Code](https://claude.ai/download) Model-Raw: claude-opus-4-5-20251101 Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 40f4c4d commit 0405c0d

15 files changed

Lines changed: 39 additions & 37 deletions

extensions/git-id-switcher/src/documentation.internal.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,10 @@ export function renderMarkdown(raw: string): string {
236236
const rows = bodyRows.trim().split(/\r?\n/);
237237
const bodyHtml = rows.map((row: string) => {
238238
// Remove leading/trailing | then split, keeping empty cells
239-
const cells = row.replace(/^\||\|$/g, '').split('|').map((c: string) => c.trim());
240-
return `<tr>${cells.map((c: string) => `<td>${c}</td>`).join('')}</tr>`;
239+
// Regex uses non-capturing groups for explicit precedence: (^|) OR (|$)
240+
const cells = row.replace(/(?:^\|)|(?:\|$)/g, '').split('|').map((c: string) => c.trim());
241+
const cellsHtml = cells.map((c: string) => '<td>' + c + '</td>').join('');
242+
return '<tr>' + cellsHtml + '</tr>';
241243
}).join('');
242244
return `<table><thead><tr>${headerHtml}</tr></thead><tbody>${bodyHtml}</tbody></table>`;
243245
}
@@ -279,12 +281,12 @@ export function renderMarkdown(raw: string): string {
279281

280282
// Step 13: Restore inline code
281283
html = html.replace(/%%INLINECODE_(\d+)%%/g, (_match, index: string) => {
282-
return inlineCodes[parseInt(index, 10)];
284+
return inlineCodes[Number.parseInt(index, 10)];
283285
});
284286

285287
// Step 14: Restore code blocks
286288
html = html.replace(/%%CODEBLOCK_(\d+)%%/g, (_match, index: string) => {
287-
return codeBlocks[parseInt(index, 10)];
289+
return codeBlocks[Number.parseInt(index, 10)];
288290
});
289291

290292
// Step 15: Convert double newlines to paragraph breaks

extensions/git-id-switcher/src/documentation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
import * as vscode from 'vscode';
14-
import * as crypto from 'crypto';
14+
import * as crypto from 'node:crypto';
1515

1616
// Import internal functions (testable without VS Code)
1717
import {
@@ -435,7 +435,7 @@ async function fetchDocumentByPath(path: string): Promise<string | null> {
435435
if (response.ok) {
436436
// DoS prevention: Check Content-Length header before reading body
437437
const contentLength = response.headers.get('content-length');
438-
if (contentLength && parseInt(contentLength, 10) > MAX_CONTENT_SIZE) {
438+
if (contentLength && Number.parseInt(contentLength, 10) > MAX_CONTENT_SIZE) {
439439
console.warn('[Git ID Switcher] Documentation too large, rejecting');
440440
return null;
441441
}

extensions/git-id-switcher/src/fileLogWriter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Provides file-based log persistence for audit trails.
66
*/
77

8-
import * as fs from 'fs';
9-
import * as path from 'path';
8+
import * as fs from 'node:fs';
9+
import * as path from 'node:path';
1010
import type { StructuredLog, ILogWriter, FileLogConfig } from './logTypes';
1111
import { isSecurePath } from './pathSecurity';
1212

@@ -19,7 +19,7 @@ import { isSecurePath } from './pathSecurity';
1919
* - Clean up old log files
2020
*/
2121
export class FileLogWriter implements ILogWriter {
22-
private config: FileLogConfig;
22+
private readonly config: FileLogConfig;
2323
private currentFilePath: string = '';
2424
private writeStream: fs.WriteStream | null = null;
2525
private currentFileSize: number = 0;

extensions/git-id-switcher/src/flagValidator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function checkCombinedFlagAllowlist(
108108
): CombinedFlagResult {
109109
// Check if this command has allowed combined patterns
110110
const commandPatterns = ALLOWED_COMBINED_PATTERNS[command];
111-
if (commandPatterns && commandPatterns.includes(flagChars)) {
111+
if (commandPatterns?.includes(flagChars)) {
112112
return { valid: true };
113113
}
114114

extensions/git-id-switcher/src/pathSanitizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Separated from SecurityLogger for Single Responsibility Principle.
88
*/
99

10-
import * as path from 'path';
10+
import * as path from 'node:path';
1111
import { PATH_MAX, MAX_PATTERN_CHECK_LENGTH } from './constants';
1212
import { CONTROL_CHAR_REGEX_ALL } from './validators/common';
1313

extensions/git-id-switcher/src/pathUtils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* Designed to prevent path traversal and symlink-based attacks.
66
*/
77

8-
import * as os from 'os';
9-
import * as path from 'path';
10-
import * as fs from 'fs';
8+
import * as os from 'node:os';
9+
import * as path from 'node:path';
10+
import * as fs from 'node:fs';
1111
import { isSecurePath, SecurePathResult } from './pathSecurity';
1212
import { PATH_MAX } from './constants';
1313
import { CONTROL_CHAR_REGEX_ALL, hasNullByte, hasPathTraversal } from './validators/common';

extensions/git-id-switcher/src/sshAgent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* @see https://owasp.org/www-community/attacks/Command_Injection
99
*/
1010

11-
import * as fs from 'fs/promises';
12-
import * as path from 'path';
11+
import * as fs from 'node:fs/promises';
12+
import * as path from 'node:path';
1313
import * as vscode from 'vscode';
1414
import { Identity, getIdentitiesWithValidation } from './identity';
1515
import { sshAgentExec, sshKeygenExec } from './secureExec';

extensions/git-id-switcher/src/test/e2e/gitConfig.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
import * as assert from 'assert';
2727
import * as vscode from 'vscode';
28-
import * as path from 'path';
29-
import * as fs from 'fs';
30-
import * as os from 'os';
31-
import { execFileSync } from 'child_process';
28+
import * as path from 'node:path';
29+
import * as fs from 'node:fs';
30+
import * as os from 'node:os';
31+
import { execFileSync } from 'node:child_process';
3232

3333
const EXTENSION_ID = 'nullvariant.git-id-switcher';
3434

@@ -358,7 +358,7 @@ describe('Git Config E2E Test Suite', function () {
358358
describe('Repository State', () => {
359359
it('should report repository has at least one commit', () => {
360360
const commitCount = gitSync(['rev-list', '--count', 'HEAD'], tempRepoPath);
361-
const count = parseInt(commitCount, 10);
361+
const count = Number.parseInt(commitCount, 10);
362362
assert.ok(!isNaN(count) && count >= 1, `Repository should have at least one commit, got: ${commitCount}`);
363363
});
364364

extensions/git-id-switcher/src/test/fileLogWriter.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@
6464
*/
6565

6666
import * as assert from 'assert';
67-
import * as fs from 'fs';
68-
import * as path from 'path';
69-
import * as os from 'os';
67+
import * as fs from 'node:fs';
68+
import * as path from 'node:path';
69+
import * as os from 'node:os';
7070
import { FileLogWriter } from '../fileLogWriter';
7171
import { LogLevel, FileLogConfig, StructuredLog } from '../logTypes';
7272

extensions/git-id-switcher/src/test/i18n.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*/
2121

2222
import * as assert from 'assert';
23-
import * as fs from 'fs';
24-
import * as path from 'path';
23+
import * as fs from 'node:fs';
24+
import * as path from 'node:path';
2525

2626
// ============================================================================
2727
// Constants

0 commit comments

Comments
 (0)