diff --git a/src/features/device-logs/lib/backends/loki.ts b/src/features/device-logs/lib/backends/loki.ts index f4f9fa5a5..7ad2cfa0b 100644 --- a/src/features/device-logs/lib/backends/loki.ts +++ b/src/features/device-logs/lib/backends/loki.ts @@ -353,25 +353,6 @@ export class LokiBackend implements DeviceLogsBackend { return `{fleet_id="${ctx.appId}"}`; } - private validateLog(log: DeviceLog): asserts log is DeviceLog { - if (typeof log.message !== 'string') { - throw new BadRequestError('DeviceLog message must be string'); - } else if (typeof log.timestamp !== 'number') { - throw new BadRequestError('DeviceLog timestamp must be number'); - } else if (typeof log.isSystem !== 'boolean') { - throw new BadRequestError('DeviceLog isSystem must be boolean'); - } else if (typeof log.isStdErr !== 'boolean') { - throw new BadRequestError('DeviceLog isStdErr must be boolean'); - } else if ( - typeof log.serviceId !== 'number' && - log.serviceId !== undefined - ) { - throw new BadRequestError( - 'DeviceLog serviceId must be number or undefined', - ); - } - } - private fromStreamToDeviceLogs(stream: loki.StreamAdapter): DeviceLog[] { try { return stream.getEntriesList().map((entry) => { @@ -399,7 +380,6 @@ export class LokiBackend implements DeviceLogsBackend { logs: Array, ) { return logs.map((log) => { - this.validateLog(log); const timestamp = new loki.Timestamp(); timestamp.setSeconds(Math.floor(Number(log.nanoTimestamp / 1000000000n))); timestamp.setNanos(Number(log.nanoTimestamp % 1000000000n)); diff --git a/src/features/device-logs/lib/store.ts b/src/features/device-logs/lib/store.ts index 3dec428f4..c186e5496 100644 --- a/src/features/device-logs/lib/store.ts +++ b/src/features/device-logs/lib/store.ts @@ -180,7 +180,12 @@ function handleStreamingWrite( } parser.on('error', close).on('data', (sLog: SupervisorLog) => { - const log = supervisor.convertLog(sLog); + let log; + try { + log = supervisor.convertLog(sLog); + } catch { + return; + } if (!log) { return; } diff --git a/src/features/device-logs/lib/supervisor.ts b/src/features/device-logs/lib/supervisor.ts index ed6cf7d79..f76ccb62a 100644 --- a/src/features/device-logs/lib/supervisor.ts +++ b/src/features/device-logs/lib/supervisor.ts @@ -30,20 +30,36 @@ export class Supervisor { return this.convertLog(log); } - public convertLog(log: SupervisorLog): DeviceLog | undefined { + public convertLog(log: { + [key in keyof SupervisorLog]: unknown; + }): DeviceLog | undefined { // see struct.ts for explanation on this if (log.uuid) { return; } - return { + if (typeof log.message !== 'string') { + throw new errors.BadRequestError('DeviceLog message must be string'); + } + if (typeof log.timestamp !== 'number') { + throw new errors.BadRequestError('DeviceLog timestamp must be number'); + } + const validatedLog: DeviceLog = { nanoTimestamp: getNanoTimestamp(), createdAt: Date.now(), timestamp: log.timestamp, isSystem: log.isSystem === true, isStdErr: log.isStdErr === true, message: log.message, - serviceId: log.serviceId, }; + if ('serviceId' in log) { + if (typeof log.serviceId !== 'number') { + throw new errors.BadRequestError( + 'DeviceLog serviceId must be number or undefined', + ); + } + validatedLog.serviceId = log.serviceId; + } + return validatedLog; } private isOldLog(log: any): log is OldSupervisorLog {