Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent parsing CLI errors without metadata #2518

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions arduino-ide-extension/src/node/service-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ProgrammerIsRequiredForUploadError } from './cli-protocol/cc/arduino/cl
type ProtoError = typeof ProgrammerIsRequiredForUploadError;
const protoErrorsMap = new Map<string, ProtoError>([
[
'type.googleapis.com/cc.arduino.cli.commands.v1.ProgrammerIsRequiredForUploadError',
'cc.arduino.cli.commands.v1.ProgrammerIsRequiredForUploadError',
ProgrammerIsRequiredForUploadError,
],
// handle other cli defined errors here
Expand All @@ -22,30 +22,33 @@ export namespace ServiceError {
return arg instanceof Error && isStatusObject(arg);
}

export function isInstanceOf(arg: unknown, type: unknown): boolean {
export function isInstanceOf<ProtoError>(
arg: unknown,
type: new (...args: unknown[]) => ProtoError
): arg is ProtoError {
if (!isStatusObject(arg)) {
return false;
}

const bin = arg.metadata.get('grpc-status-details-bin')[0];
try {
const bin = arg.metadata.get('grpc-status-details-bin')[0];
const uint8Array =
typeof bin === 'string'
? stringToUint8Array(bin)
: new Uint8Array(bin.buffer, bin.byteOffset, bin.byteLength);

const uint8Array =
typeof bin === 'string'
? stringToUint8Array(bin)
: new Uint8Array(bin.buffer, bin.byteOffset, bin.byteLength);
const errors = Status.deserializeBinary(uint8Array)
.getDetailsList()
.map((details) => {
const typeName = details.getTypeName();
const ErrorType = protoErrorsMap.get(typeName);
return ErrorType?.deserializeBinary(details.getValue_asU8());
});

const errors = Status.deserializeBinary(uint8Array)
.getDetailsList()
.map((details) => {
const typeUrl = details.getTypeUrl();
const ErrorType = protoErrorsMap.get(typeUrl);
return ErrorType?.deserializeBinary(details.getValue_asU8());
});

return !!errors.find((error) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return error && error instanceof <any>type;
});
return !!errors.find((error) => error && error instanceof type);
} catch {
return false;
}
}

function isStatusObject(arg: unknown): arg is StatusObject {
Expand Down
Loading