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 2455: Check auth for pictrs when instance is private. #2477

Merged

Conversation

sam365724
Copy link
Contributor

@Nutomic
Copy link
Member

Nutomic commented Oct 3, 2022

You need to run cargo +nightly fmt --all. Then you can check the CI for any more problems you might have to fix.

Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

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

This requires 1-2 DB reads for every single picture requested now, reading the site to check if its a private instance, and if it is, reading from local_user to make sure they're actually a user.

That performance hit scares me, because some pages like Home can request hundreds of pictures.

I don't think this is on you to fix tho, its on us to create a in-memory singleton of local site settings, that gets updated on every save. Then we could at least reduce this to one read validating the local user, that should only get run on a private instances.

Also the format check is failing, run cargo +nightly fmt to fix

if site.private_instance {
let jwt = req
.cookie("jwt")
.expect("No auth header for picture access");
Copy link
Member

Choose a reason for hiding this comment

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

No expect, do a .map_err(error::ErrorUnauthorized)

Copy link
Contributor Author

@sam365724 sam365724 Oct 3, 2022

Choose a reason for hiding this comment

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

OK, that should be done in upload() on line 92 as well i think?

.cookie("jwt")
.expect("No auth header for picture access");
if get_local_user_view_from_jwt(jwt.value(), context.pool(), context.secret()).await.is_err() {
return Ok(HttpResponse::Unauthorized().finish());
Copy link
Member

Choose a reason for hiding this comment

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

I think its better to return the Error(error::ErrorUnauthorized)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think in both cases we should return a HttpResponse. I'm not sure how to do that honestly.

Is replacing expect() with .map_err(error::ErrorUnauthorized) enough above?

Copy link
Member

Choose a reason for hiding this comment

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

I spose that's fine, althoug that actix errors are also HttpResponses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dessalines I'm getting errors about types not being correct. I'm really unsure how to do that, and need help on those - sorry not yet familiar with those rust libs.

Copy link
Member

@dessalines dessalines Oct 3, 2022

Choose a reason for hiding this comment

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

There are other examples in this file of .map_err . If you run cargo check, it should tell you the exact error.

@sam365724
Copy link
Contributor Author

This requires 1-2 DB reads for every single picture requested now, reading the site to check if its a private instance, and if it is, reading from local_user to make sure they're actually a user.

That performance hit scares me, because some pages like Home can request hundreds of pictures.

I don't think this is on you to fix tho, its on us to create a in-memory singleton of local site settings, that gets updated on every save. Then we could at least reduce this to one read validating the local user, that should only get run on a private instances.

Also the format check is failing, run cargo +nightly fmt to fix

I agree on the performance concern. I see up to 600 requests per user easily if you have some image intense posts.

Thanks for the review, i did run the formatter now.

@dessalines dessalines force-pushed the check-auth-for-imgs-private-instance branch from 91ed275 to f24678f Compare October 28, 2022 14:43
@dessalines dessalines merged commit 7aa6d6b into LemmyNet:main Oct 28, 2022
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.

3 participants