-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[dashboards as code] only validate id on PUT route when creating new dashboard #264161
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
Changes from all commits
bd34df2
61281f6
9ed2c5e
ee92d6a
5670bb0
a2a7cb7
610f5fb
ac91ff9
97fd318
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| */ | ||
|
|
||
| import type { RequestHandlerContext } from '@kbn/core/server'; | ||
| import { asCodeIdSchema } from '@kbn/as-code-shared-schemas'; | ||
| import type { DashboardSavedObjectAttributes } from '../../dashboard_saved_object'; | ||
| import { DASHBOARD_SAVED_OBJECT_TYPE } from '../../../common/constants'; | ||
| import type { DashboardUpdateRequestBody, DashboardUpdateResponseBody } from './types'; | ||
|
|
@@ -29,6 +30,25 @@ export async function update( | |
| isDashboardAppRequest | ||
| ); | ||
|
|
||
| let isCreateRequest = false; | ||
| try { | ||
| await core.savedObjects.client.resolve<DashboardSavedObjectAttributes>( | ||
| DASHBOARD_SAVED_OBJECT_TYPE, | ||
| id | ||
| ); | ||
| } catch (resolveError) { | ||
| if (resolveError.isBoom && resolveError.output.statusCode === 404) { | ||
| isCreateRequest = true; | ||
| } else { | ||
| throw resolveError; | ||
| } | ||
| } | ||
|
|
||
| // Validate id at handler level for create requests | ||
| if (isCreateRequest) { | ||
| asCodeIdSchema.validate(id); | ||
|
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. Moving the object ID validation from the route into the handler shifts this from “fail fast at the edge” to application logic. That means invalid IDs will be rejected slightly later (a bit more server work/latency per bad request), while HTTP traffic/payload size stays the same—though high volumes of invalid/malicious requests could cost a bit more CPU. The upside is the handler can support richer, context-aware checks (e.g., authorization/ownership), not just format validation.
Contributor
Author
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. Really no other way. At the route level, there is no way to know if the PUT request is an update or a create. We can not have the route failing for update requests since this would make it impossible to update old dashboard with ids that are not valid for new schema. Saved object id schema allowed any character that did is allowed in a URL. We want to limit characters to only letters, numbers, hyphens, and underscores |
||
| } | ||
|
|
||
| const savedObject = await core.savedObjects.client.update<DashboardSavedObjectAttributes>( | ||
| DASHBOARD_SAVED_OBJECT_TYPE, | ||
| id, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "attributes": { | ||
| "description": "", | ||
| "hits": 0, | ||
| "kibanaSavedObjectMeta": { | ||
| "searchSourceJSON": "{\"query\":{\"query\":\"\",\"language\":\"lucene\"},\"filter\":[],\"highlightAll\":true,\"version\":true}" | ||
| }, | ||
| "optionsJSON": "{\"darkTheme\":false}", | ||
| "panelsJSON": "{}", | ||
| "refreshInterval": { | ||
| "display": "Off", | ||
| "pause": false, | ||
| "value": 0 | ||
| }, | ||
| "timeFrom": "Wed Sep 16 2015 22:52:17 GMT-0700", | ||
| "timeRestore": true, | ||
| "timeTo": "Fri Sep 18 2015 12:24:38 GMT-0700", | ||
| "title": "Dashboard with invalid as code id", | ||
| "version": 1 | ||
| }, | ||
| "coreMigrationVersion": "7.14.0", | ||
| "id": "(my)dashboard", | ||
| "migrationVersion": { | ||
| "dashboard": "7.11.0" | ||
| }, | ||
| "references": [ | ||
| { | ||
| "id": "dd7caf20-9efd-11e7-acb3-3dab96693fab", | ||
| "name": "1:panel_1", | ||
| "type": "visualization" | ||
| } | ||
| ], | ||
| "type": "dashboard", | ||
| "updated_at": "2017-09-21T18:57:40.826Z", | ||
| "version": "WzExLDJd" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.