Skip to content

Commit

Permalink
Add safeguards milliseconds timestamps (#184)
Browse files Browse the repository at this point in the history
Avoid mistakenly send seconds timestamps to the audit logs command.
  • Loading branch information
Sgravis authored Sep 1, 2023
2 parents c564454 + 410e396 commit db56d71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/command-handlers/teamLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import { getTeamDeviceCredentials, jsonToCsv, epochTimestampToIso } from '../uti
import { GenericLog } from '../types/logs';

export const runTeamLogs = async (options: {
start: string;
end: string;
start: number;
end: number;
type: string;
category: string;
csv: boolean;
humanReadable: boolean;
}) => {
const teamDeviceCredentials = getTeamDeviceCredentials();

const { start, type, category } = options;
const end = options.end === 'now' ? Date.now().toString() : options.end;
const { start, end, type, category } = options;

let logs = await getAuditLogs({
teamDeviceCredentials,
startDateRangeUnix: parseInt(start),
endDateRangeUnix: parseInt(end),
startDateRangeUnix: start,
endDateRangeUnix: end,
logType: type,
category,
});
Expand Down
11 changes: 8 additions & 3 deletions src/commands/team/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Command } from 'commander';
import { teamCredentialsCommands } from './credentials';
import { CouldNotFindTeamCredentialsError } from '../../errors';
import { runTeamLogs, runTeamMembers, runTeamReport } from '../../command-handlers';
import { customParseInt, getTeamDeviceCredentials } from '../../utils';
import { customParseInt, customParseTimestampMilliseconds, getTeamDeviceCredentials } from '../../utils';

export const teamCommands = (params: { program: Command }) => {
const { program } = params;
Expand Down Expand Up @@ -37,8 +37,13 @@ export const teamCommands = (params: { program: Command }) => {
.command('logs')
.alias('l')
.description('List audit logs')
.option('--start <start>', 'start timestamp in ms', '0')
.option('--end <end>', 'end timestamp in ms (use "now" to get the current timestamp)', 'now')
.option('--start <start>', 'Start timestamp in ms', customParseTimestampMilliseconds, '0')
.option(
'--end <end>',
'End timestamp in ms (use "now" to get the current timestamp)',
customParseTimestampMilliseconds,
'now'
)
.option('--type <type>', 'log type')
.option('--category <category>', 'log category')
.option('--csv', 'Output in CSV format')
Expand Down
20 changes: 20 additions & 0 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ export const customParseInt = (value: string, _dummyPrevious: unknown) => {
return parsedValue;
};

export const customParseTimestampMilliseconds = (value: string, _dummyPrevious: unknown) => {
if (value === 'now') {
return Date.now();
}
if (value === '0') {
return 0;
}

// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new commander.InvalidArgumentError('Not a number.');
}

if (parsedValue < 999999999999 || parsedValue > 100000000000000) {
throw new commander.InvalidArgumentError('Timestamp must be in milliseconds.');
}
return parsedValue;
};

/** Remove underscores and capitalize string */
export const removeUnderscoresAndCapitalize = (string: string): string => {
return string
Expand Down

0 comments on commit db56d71

Please sign in to comment.