-
Notifications
You must be signed in to change notification settings - Fork 145
fix(api): validate metric alert UUID inputs #8118
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 { | ||||||||||||||||||
|
|
@@ -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, | ||||||||||||||||||
|
|
@@ -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'); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Channels and the saved filter must belong to the same project as the | ||||||||||||||||||
| // target. The DB foreign keys allow cross-project references on their | ||||||||||||||||||
|
|
@@ -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.'); | ||||||||||||||||||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a truthiness check
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -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, | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a truthiness check
if (input.savedFilterId)skips validation ifsavedFilterIdis 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.