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

[8.15] [Security Solution] [Attack discovery] Updates Gemini connector to ignore unknown properties in responses (#192915) #193221

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 11 additions & 8 deletions x-pack/plugins/stack_connectors/common/gemini/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ export const RunActionParamsSchema = schema.object({
raw: schema.maybe(schema.boolean()),
});

export const RunApiResponseSchema = schema.object({
candidates: schema.any(),
usageMetadata: schema.object({
promptTokenCount: schema.number(),
candidatesTokenCount: schema.number(),
totalTokenCount: schema.number(),
}),
});
export const RunApiResponseSchema = schema.object(
{
candidates: schema.any(),
usageMetadata: schema.object({
promptTokenCount: schema.number(),
candidatesTokenCount: schema.number(),
totalTokenCount: schema.number(),
}),
},
{ unknowns: 'ignore' } // unknown keys will NOT fail validation, but will be removed
);

export const RunActionResponseSchema = schema.object(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,35 @@ describe('GeminiConnector', () => {

expect(response).toEqual(connectorResponse);
});

describe('RunApiResponseSchema', () => {
it('successfully validates a response that only has known properties', () => {
const onlyKnownProperties = {
...defaultResponse.data,
};

expect(RunApiResponseSchema.validate(onlyKnownProperties)).toEqual(onlyKnownProperties);
});

it('fails validation when the response does NOT conform to the schema', () => {
const missingRequiredFields = {
// missing candidates and usageMetadata
};

expect(() => RunApiResponseSchema.validate(missingRequiredFields)).toThrowError();
});

it('removes unknown properties, but does NOT fail validation when they are present', () => {
const hasUnknownProperties = {
...defaultResponse.data,
modelVersion: '1.0.0', // <-- an unknown property
};

expect(RunApiResponseSchema.validate(hasUnknownProperties)).toEqual({
...defaultResponse.data,
});
});
});
});

describe('invokeAI', () => {
Expand Down