Skip to content

Commit

Permalink
fix: add string limit to some very long debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
j-luong committed Jan 23, 2025
1 parent 2aa94d1 commit 0318b71
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/cli/commands/test/iac/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { getErrorStringCode } from './local-execution/error-utils';
import { getRepositoryRootForPath } from '../../../../lib/iac/git';
import { getInfo } from '../../../../lib/project-metadata/target-builders/git';
import { buildMeta, GitRepository, GitRepositoryFinder } from './meta';
import { MAX_STRING_LENGTH } from '../../../../lib/constants';

const debug = debugLib('snyk-iac');

Expand Down Expand Up @@ -181,7 +182,7 @@ function formatTestError(error) {

function safeStringify(obj: unknown): string {
try {
return JSON.stringify(obj);
return JSON.stringify(obj).slice(0, MAX_STRING_LENGTH);
} catch (e) {
if (e instanceof Error) {
return `Error stringifying object: ${e.message}`;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export const CALL_PATH_LEADING_ELEMENTS = 2;

// Number of function names to show in the end of an abbreviated call path
export const CALL_PATH_TRAILING_ELEMENTS = 2;

// Upper limit of string length that should be allowed for output to stdrr || stdout.
// Use when outputting strings of unknown length. e.g. response payloads
export const MAX_STRING_LENGTH = 50000;
11 changes: 10 additions & 1 deletion src/lib/plugins/sast/utils/testEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { emitter as codeEmitter } from '@snyk/code-client';
import { spinner } from '../../../spinner';
import * as debugLib from 'debug';
import { MAX_STRING_LENGTH } from '../../../constants';

export function analysisProgressUpdate(): void {
let currentMessage = '';
Expand Down Expand Up @@ -32,6 +33,14 @@ export function analysisProgressUpdate(): void {
});
codeEmitter.on('apiRequestLog', (data) => {
const debug = debugLib('snyk-code');
debug('---> API request log ', data);
if (data.length > MAX_STRING_LENGTH) {
// limit the string length as truncation doesn't always happen, causing the CLI to end unexpectedly
debug(
'---> API request log ',
data.slice(0, MAX_STRING_LENGTH) + '...(log line truncated)',
);
} else {
debug('---> API request log ', data);
}
});
}
9 changes: 8 additions & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as http from 'http';
import { jsonStringifyLargeObject } from '../json';
import { MissingApiTokenError } from '../errors';
import { headerSnykAuthFailed } from './constants';
import { MAX_STRING_LENGTH } from '../constants';

const debug = debugModule('snyk:req');
const snykDebug = debugModule('snyk');
Expand Down Expand Up @@ -87,7 +88,13 @@ function setupRequest(payload: Payload) {
}

try {
debug('request payload: ', jsonStringifyLargeObject(payload));
const payloadStr = jsonStringifyLargeObject(payload);
debug(
'request payload: ',
payloadStr.length > MAX_STRING_LENGTH
? payloadStr + '...(log line truncated)'
: payloadStr,
);
} catch (e) {
debug('request payload is too big to log', e);
}
Expand Down

0 comments on commit 0318b71

Please sign in to comment.