Skip to content
Closed
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
24 changes: 24 additions & 0 deletions integration-tests/testkit/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,30 @@ export function readTargetInfo(
});
}

export function readMetricAlertRule(
target: GraphQLSchema.TargetReferenceInput,
ruleId: string,
authToken: string,
) {
return execute({
document: graphql(`
query IntegrationTests_ReadMetricAlertRule($target: TargetReferenceInput!, $ruleId: ID!) {
target(reference: $target) {
metricAlertRule(id: $ruleId) {
id
name
}
}
}
`),
authToken,
variables: {
target,
ruleId,
},
});
}

export function createMemberRole(input: CreateMemberRoleInput, authToken: string) {
return execute({
document: graphql(`
Expand Down
55 changes: 55 additions & 0 deletions integration-tests/tests/api/project/metric-alerts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'reflect-metadata';
import {
addMetricAlertRule as rawAddMetricAlertRule,
deleteMetricAlertRules as rawDeleteMetricAlertRules,
readMetricAlertRule,
updateMetricAlertRule as rawUpdateMetricAlertRule,
} from 'testkit/flow';
import {
Expand Down Expand Up @@ -102,6 +103,60 @@ test.concurrent('can create, read, update, and delete a metric alert rule', asyn
expect(deleteResult.ok!.deletedMetricAlertRuleIds).toContain(rule.id);
});

test.concurrent('rejects malformed metric alert UUID inputs before hitting Postgres', async ({
expect,
}) => {
const { createOrg, ownerToken } = await initSeed().createOwner();
const { createProject, organization, setFeatureFlag } = await createOrg();
await setFeatureFlag('metricAlertRules', true);
const { project, target, addMetricAlertRule, updateMetricAlertRule, deleteMetricAlertRules } =
await createProject(ProjectType.Single);

const targetReference = {
bySelector: {
organizationSlug: organization.slug,
projectSlug: project.slug,
targetSlug: target.slug,
},
};

const addResult = await addMetricAlertRule({
target: targetReference,
name: 'Invalid channel id',
type: MetricAlertRuleType.Traffic,
timeWindowMinutes: 5,
thresholdType: MetricAlertRuleThresholdType.FixedValue,
thresholdValue: 100,
direction: MetricAlertRuleDirection.Above,
severity: MetricAlertRuleSeverity.Info,
channelIds: ['i-do-not-exist'],
});
expect(addResult.ok).toBeNull();
expect(addResult.error?.message).toBe('Notification channel ID must be a valid UUID.');

const updateResult = await updateMetricAlertRule({
project: { bySelector: { organizationSlug: organization.slug, projectSlug: project.slug } },
ruleId: 'xxxx-xxxx-xxxx-xxxx',
name: 'Invalid rule id',
});
expect(updateResult.ok).toBeNull();
expect(updateResult.error?.message).toBe('Metric alert rule ID must be a valid UUID.');

const deleteResult = await deleteMetricAlertRules({
project: { bySelector: { organizationSlug: organization.slug, projectSlug: project.slug } },
ruleIds: ['i-do-not-exist'],
});
expect(deleteResult.ok).toBeNull();
expect(deleteResult.error?.message).toBe('Metric alert rule ID must be a valid UUID.');

const readResult = await readMetricAlertRule(
targetReference,
'i-do-not-exist',
ownerToken,
).then(r => r.expectNoGraphQLErrors());
expect(readResult.target?.metricAlertRule).toBeNull();
});

test.concurrent(
'validates that LATENCY type requires metric and non-LATENCY rejects it',
async ({ expect }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
METRIC_ALERT_RULES_PER_TARGET_LIMIT,
} from '../../commerce/constants';
import { OrganizationManager } from '../../organization/providers/organization-manager';
import { isUUID } from '../../shared/is-uuid';
import { Logger } from '../../shared/providers/logger';
import { METRIC_ALERT_RULES_ENABLED } from './metric-alert-rules-flag-token';
import {
Expand Down Expand Up @@ -52,6 +53,18 @@ export class MetricAlertRulesDisabledError extends Error {
}
}

function assertUUID(value: string, label: string): void {
if (!isUUID(value)) {
throw new MetricAlertRuleValidationError(`${label} must be a valid UUID.`);
}
}

function assertUUIDs(values: readonly string[], label: string): void {
for (const value of values) {
assertUUID(value, label);
}
}

@Injectable({
scope: Scope.Operation,
global: true,
Expand Down Expand Up @@ -120,6 +133,10 @@ export class MetricAlertRulesManager {

this.assertTypeMetricPairing(input.type, input.metric);
this.assertTimeWindowInRange(input.timeWindowMinutes);
assertUUIDs(input.channelIds, 'Notification channel ID');
if (input.savedFilterId) {
assertUUID(input.savedFilterId, 'Saved filter ID');
}
Comment on lines +137 to +139

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a truthiness check if (input.savedFilterId) skips validation if savedFilterId is an empty string (""), leading to a database syntax error when Postgres inserts it into a UUID column. Use a strict null/undefined check to ensure all string values are validated.

Suggested change
if (input.savedFilterId) {
assertUUID(input.savedFilterId, 'Saved filter ID');
}
if (input.savedFilterId !== undefined && input.savedFilterId !== null) {
assertUUID(input.savedFilterId, 'Saved filter ID');
}


// Channels and the saved filter must belong to the same project as the
// target. The DB foreign keys allow cross-project references on their
Expand Down Expand Up @@ -195,6 +212,8 @@ export class MetricAlertRulesManager {
params: { organizationId: input.organizationId, projectId: input.projectId },
});

assertUUID(input.ruleId, 'Metric alert rule ID');

const existing = await this.storage.getMetricAlertRule({ id: input.ruleId });
if (!existing || existing.projectId !== input.projectId) {
throw new MetricAlertRuleValidationError('Metric alert rule not found.');
Expand All @@ -211,10 +230,12 @@ export class MetricAlertRulesManager {
}

if (input.channelIds) {
assertUUIDs(input.channelIds, 'Notification channel ID');
await this.assertChannelsBelongToProject(input.channelIds, input.projectId);
}

if (input.savedFilterId) {
assertUUID(input.savedFilterId, 'Saved filter ID');
await this.assertSavedFilterBelongsToProject(input.savedFilterId, input.projectId);
}
Comment on lines 237 to 240

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a truthiness check if (input.savedFilterId) skips validation if savedFilterId is an empty string (""), leading to a database syntax error when Postgres updates the UUID column. Use a strict null/undefined check to ensure all string values are validated.

Suggested change
if (input.savedFilterId) {
assertUUID(input.savedFilterId, 'Saved filter ID');
await this.assertSavedFilterBelongsToProject(input.savedFilterId, input.projectId);
}
if (input.savedFilterId !== undefined && input.savedFilterId !== null) {
assertUUID(input.savedFilterId, 'Saved filter ID');
await this.assertSavedFilterBelongsToProject(input.savedFilterId, input.projectId);
}


Expand Down Expand Up @@ -263,6 +284,8 @@ export class MetricAlertRulesManager {
params: { organizationId: input.organizationId, projectId: input.projectId },
});

assertUUIDs(input.ruleIds, 'Metric alert rule ID');

this.logger.debug(
'Deleting metric alert rules (organization=%s, project=%s, count=%s)',
input.organizationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
MetricAlertRuleState,
MetricAlertStateLogEntry,
} from '../../../shared/entities';
import { isUUID } from '../../shared/is-uuid';
import { METRIC_ALERT_RULES_PER_TARGET_LIMIT } from '../../commerce/constants';

/**
Expand Down Expand Up @@ -230,6 +231,10 @@ export class MetricAlertRulesStorage {
// --- Alert Rule CRUD ---

async getMetricAlertRule(args: { id: string }): Promise<MetricAlertRule | null> {
if (!isUUID(args.id)) {
return null;
}

const result = await this.pool.maybeOne(psql`/* getMetricAlertRule */
SELECT ${METRIC_ALERT_RULE_SELECT}
FROM "metric_alert_rules"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IdTranslator } from '../../../shared/providers/id-translator';
import {
MetricAlertRulesDisabledError,
MetricAlertRulesManager,
MetricAlertRuleValidationError,
} from '../../providers/metric-alert-rules-manager';
import type { MutationResolvers } from './../../../../__generated__/types';

Expand All @@ -23,7 +24,10 @@ export const deleteMetricAlertRules: NonNullable<
});
return { ok: { deletedMetricAlertRuleIds } };
} catch (error) {
if (error instanceof MetricAlertRulesDisabledError) {
if (
error instanceof MetricAlertRulesDisabledError ||
error instanceof MetricAlertRuleValidationError
) {
return { error: { message: error.message } };
}
throw error;
Expand Down