Skip to content

Commit

Permalink
fix: Users being able to update datasets across DBs (#17348)
Browse files Browse the repository at this point in the history
* add database id back

* add condition to verify dataset is being changed

* Update superset/datasets/dao.py

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>

Co-authored-by: Beto Dealmeida <roberto@dealmeida.net>
  • Loading branch information
2 people authored and AAfghahi committed Jan 10, 2022
1 parent dfeec21 commit 725d674
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion superset-frontend/src/SqlLab/components/ResultSet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const ResultSetErrorMessage = styled.div`
`;

const updateDataset = async (
dbId: number,
datasetId: number,
sql: string,
columns: Array<Record<string, any>>,
Expand All @@ -159,6 +160,7 @@ const updateDataset = async (
sql,
columns,
owners,
database_id: dbId,
});

const data: JsonResponse = await SupersetClient.put({
Expand Down Expand Up @@ -272,10 +274,11 @@ export default class ResultSet extends React.PureComponent<
};

handleOverwriteDataset = async () => {
const { sql, results } = this.props.query;
const { sql, results, dbId } = this.props.query;
const { datasetToOverwrite } = this.state;

await updateDataset(
dbId,
datasetToOverwrite.datasetId,
sql,
results.selected_columns.map(d => ({ column_name: d.name })),
Expand Down
3 changes: 1 addition & 2 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1734,14 +1734,13 @@ def before_update(

for attr in ["database_id", "schema", "table_name"]:
history = state.get_history(attr, True)

if history.has_changes():
break
else:
return None

if not DatasetDAO.validate_uniqueness(
target.database_id, target.schema, target.table_name
target.database_id, target.schema, target.table_name, target.id
):
raise Exception(get_dataset_exist_error_msg(target.full_name))

Expand Down
12 changes: 11 additions & 1 deletion superset/datasets/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ def validate_table_exists(
return False

@staticmethod
def validate_uniqueness(database_id: int, schema: Optional[str], name: str) -> bool:
def validate_uniqueness(
database_id: int,
schema: Optional[str],
name: str,
dataset_id: Optional[int] = None,
) -> bool:
dataset_query = db.session.query(SqlaTable).filter(
SqlaTable.table_name == name,
SqlaTable.schema == schema,
SqlaTable.database_id == database_id,
)

if dataset_id:
# make sure the dataset found is different from the target (if any)
dataset_query = dataset_query.filter(SqlaTable.id != dataset_id)

return not db.session.query(dataset_query.exists()).scalar()

@staticmethod
Expand Down

0 comments on commit 725d674

Please sign in to comment.