Skip to content

fix: Make g.user attribute access safe for public users#14287

Merged
robdiciuccio merged 1 commit into
apache:masterfrom
preset-io:rd/anonymous-usernames
Apr 26, 2021
Merged

fix: Make g.user attribute access safe for public users#14287
robdiciuccio merged 1 commit into
apache:masterfrom
preset-io:rd/anonymous-usernames

Conversation

@robdiciuccio
Copy link
Copy Markdown
Member

SUMMARY

Make g.user attribute access safe for public users by:

  1. Using the abstract get_id method
  2. Checking for presence of g.user.username before using (this property is not set for public users)

TEST PLAN

Automated tests should pass.

ADDITIONAL INFORMATION

Copy link
Copy Markdown
Member

@dpgaspar dpgaspar left a comment

Choose a reason for hiding this comment

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

nice! but we probably should check if the user is authenticated instead of checking if certain attrs are present, seems cleaner

raise Exception("Database does not support cost estimation")

user_name = g.user.username if g.user else None
user_name = g.user.username if g.user and hasattr(g.user, "username") else None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it probably makes more sense to just check if the user is authenticated, using g.user.is_authenticated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I originally was checking g.user.is_anonymous in these use cases, but opted for the explicit check for username, as this is the value that is being fetched. There may be use cases where username is not present in other states down the road, so thought explicit hasattr would be more future-proof.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ok

Comment thread superset/charts/api.py
if not charts:
return self.response_404()
favorited_chart_ids = ChartDAO.favorited_ids(charts, g.user.id)
favorited_chart_ids = ChartDAO.favorited_ids(charts, g.user.get_id())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@robdiciuccio
Copy link
Copy Markdown
Member Author

/testenv up

@github-actions
Copy link
Copy Markdown
Contributor

@robdiciuccio Ephemeral environment spinning up at http://54.201.39.228:8080. Credentials are admin/admin. Please allow several minutes for bootstrapping and startup.

@robdiciuccio robdiciuccio merged commit 6875a1a into apache:master Apr 26, 2021
@robdiciuccio robdiciuccio deleted the rd/anonymous-usernames branch April 26, 2021 15:50
@github-actions
Copy link
Copy Markdown
Contributor

Ephemeral environment shutdown and build artifacts deleted.

QAlexBall pushed a commit to QAlexBall/superset that referenced this pull request Dec 29, 2021
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.2.0 First shipped in 1.2.0 labels Mar 12, 2024
qfcwell pushed a commit to qfcwell/superset that referenced this pull request May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels preset-io size/M 🚢 1.2.0 First shipped in 1.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to enable query execution in sqllab for Anonymous user(public role) ?

4 participants