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

fix: include organization_id in where clause of update queries #3439

Merged
merged 1 commit into from
Oct 14, 2022

Conversation

dnephin
Copy link
Contributor

@dnephin dnephin commented Oct 13, 2022

If someone is able to guess the ID of a row or the ID is accidentally shared it's possible a non-authorized user could update the row using the ID.

This PR fixes the bug by setting WHERE organization_id = ? so that only a user authorized for that org can perform updates.

Also adds a bunch more godoc for query.go.

Comment on lines +365 to +367
if isOrgMember(model) {
db = ByOrgID(tx.OrganizationID())(db)
}
Copy link
Contributor Author

@dnephin dnephin Oct 13, 2022

Choose a reason for hiding this comment

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

This is the bit we were missing. setOrg makes it look like we're doing the right thing, but that only sets the org_id of the new row, it doesn't force the query to only operate on an ID of the correct org.

I looked back to see if this was accidentally removed at some point, but I didn't find it, so I think it was something we missed while adding org support.

Copy link
Contributor

Choose a reason for hiding this comment

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

right. setting the org is enough for inserts, but not for updates. As is it would effectively allow you to steal records from another org to your org.

internal/server/data/query.go Outdated Show resolved Hide resolved
@dnephin dnephin requested a review from ssoroka October 13, 2022 16:25
So that if an ID is guessed or accidentally shared publicly, only
requests authorized for that org can perform the update.

Also a bunch more godoc for query.go

Also remove unnecessary column aliases from a function
@dnephin dnephin force-pushed the dnephin/data-update-include-org-id branch from 8b45a79 to b22f6dd Compare October 13, 2022 17:17
@@ -103,12 +122,21 @@ func update(tx WriteTxn, item Updatable) error {
query.B(item.Table())
query.B("SET")
query.B(columnsForUpdate(item), item.Values()...)
query.B("WHERE deleted_at is null AND id = ?;", item.Primary())
query.B("WHERE deleted_at is null")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could hiding deleted at on update cause issues with restoring deleted rows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have a way to restore deleted rows through the API, right? If we wanted to do that we'd have to write a migration or do it directly in the database?

My rational for deleted_at is null is that we don't really want someone to be able to updated the deleted_at field of a row that was already deleted.

If we ever support un-delete from the API we will definitely need to change this.

@dnephin dnephin merged commit ec95247 into main Oct 14, 2022
@dnephin dnephin deleted the dnephin/data-update-include-org-id branch October 14, 2022 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants