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

Prevent creating saved objects with empty IDs #120693

Conversation

jportner
Copy link
Contributor

@jportner jportner commented Dec 7, 2021

Resolves #118957.

The SavedObjectsRepository no longer allows consumers to create saved objects with empty IDs.

Scenarios:

  • bulkCreate w/ id: undefined -> generate a random object ID
  • bulkCreate w/ id: '' -> generate a random object ID
  • create w/ id: undefined -> generate a random object ID
  • create w/ id: '' -> generate a random object ID
  • update w/ id: undefined -> throw 400 Bad Request
  • update w/ id: '' -> throw 400 Bad Request
  • incrementCounter w/ id: undefined -> throw 400 Bad Request
  • incrementCounter w/ id: '' -> throw 400 Bad Request

Notes:

  • I did not change bulkCreate, it always behaved this way
  • I changed create to match the behavior of bulkCreate
  • I changed update and incrementCounter because these can use the upsert option to create new saved objects
  • I did not change bulkUpdate because it doesn't support the upsert option
    • I can change this to match the new behavior of update, I'm on the fence about it

Further validation of saved object IDs should be added in a follow-on issue (#105039). This issue is minimal in scope and is intended to prevent users from accidentally creating saved objects that can't be deserialized.

`create` now coalesces an empty ID into a randomly generated one.
`update` and `incrementCounter` now throw a Bad Request error when an
empty ID is used.
@jportner jportner added v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v8.1.0 v7.16.1 labels Dec 7, 2021
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@jportner jportner marked this pull request as ready for review December 7, 2021 23:26
@jportner jportner requested a review from a team as a code owner December 7, 2021 23:26
@jportner jportner requested a review from pgayvallet December 7, 2021 23:27
Comment on lines +1234 to +1236
if (!id) {
throw SavedObjectsErrorHelpers.createBadRequestError('id cannot be empty'); // prevent potentially upserting a saved object with an empty ID
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If the intent of the PR is to avoid creating objects with empty ids, should we only throw if options.upsert is set, both for update and incrementCounter? OTOH it doesn't make sense to try to update an object with empty id, but in that case, maybe we should ensure this for all methods?

But the PR does what it's supposed to do, protect against introducing corrupted docs, so it's fine with me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I wanted to minimize the changes but at least keep the responses consistent within the update method. I think we should probably ensure this for all methods (along with other ID validation) but I'd prefer to leave it for the follow-on issue 👍

@jportner jportner added auto-backport Deprecated - use backport:version if exact versions are needed v7.17.0 labels Dec 9, 2021
@jportner jportner merged commit 0457cd1 into elastic:main Dec 9, 2021
@jportner jportner deleted the issue-118957-fix-saved-object-create-empty-id branch December 9, 2021 14:33
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Dec 9, 2021
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Dec 9, 2021
@kibanamachine
Copy link
Contributor

💔 Backport failed

Status Branch Result
8.0
7.16
7.17 The branch "7.17" is invalid or doesn't exist

Successful backport PRs will be merged automatically after passing CI.

To backport manually run:
node scripts/backport --pr 120693

@jportner jportner removed the v7.17.0 label Dec 9, 2021
kibanamachine added a commit that referenced this pull request Dec 9, 2021
Co-authored-by: Joe Portner <5295965+jportner@users.noreply.github.com>
kibanamachine added a commit that referenced this pull request Dec 9, 2021
Co-authored-by: Joe Portner <5295965+jportner@users.noreply.github.com>
TinLe pushed a commit to TinLe/kibana that referenced this pull request Dec 22, 2021
@jportner
Copy link
Contributor Author

jportner commented Jan 18, 2022

This was originally supposed to make it into the 7.16.1 release but I think the backport merged a bit too late on Dec 9 and didn't make the cut: https://github.com/elastic/kibana/commits/v7.16.1

I'm changing the label to reflect that this actually shipped with 7.16.2: https://github.com/elastic/kibana/commits/v7.16.2?after=9b678a13a6a3f45286f1d21856a7536a9297f42f+34&branch=v7.16.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed release_note:skip Skip the PR/issue when compiling release notes v7.16.2 v8.0.0 v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kibana allows users to create a saved object with an empty ID
4 participants