6

Add support for ActiveStorage expiring URLs by aki77 · Pull Request #42410 · rai...

 3 years ago
source link: https://github.com/rails/rails/pull/42410
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

New issue

Add support for ActiveStorage expiring URLs #42410

Merged

pixeltrix merged 1 commit into

rails:main

from

aki77:activestorage-expiring-url

14 days ago

Merged

Add support for ActiveStorage expiring URLs #42410

pixeltrix merged 1 commit into

rails:main

from

aki77:activestorage-expiring-url

14 days ago

Conversation

Copy link

Contributor

aki77 commented 22 days ago

Summary

Added support for the expires_in option in the ActiveStorage url helper.
This is useful for generating expiring URLs.

Related Issues: #38843, #42059

@@ -1,3 +1,7 @@

* Add support for ActiveStorage expiring URLs.

zzak 22 days ago

Member

Could you also add an example to the docs?

aki77 22 days ago

Author

Contributor

Added examples.

@@ -34,12 +34,12 @@

if model.respond_to?(:signed_id)

route_for(

:rails_service_blob_proxy,

model.signed_id,

model.signed_id(expires_in: options.delete(:expires_in)),

aki77 22 days ago

Author

Contributor

@zzak

I think it would be more useful to make the default value configurable as follows, what do you think about that?

model.signed_id(expires_in: options.delete(:expires_in) { ActiveStorage.rails_urls_expire_in }),

zzak 22 days ago

Member

Probably an application/global setting + an argument that overrides it makes sense. thinking

aki77 21 days ago

Author

Contributor

Thanks.
I tried the application settings.

70a9335

@@ -60,6 +60,7 @@ module ActiveStorage

mattr_accessor :content_types_allowed_inline, default: []

mattr_accessor :service_urls_expire_in, default: 5.minutes

mattr_accessor :rails_urls_expire_in

aki77 21 days ago

Author

Contributor

If you have a better name, please advise!

pixeltrix 17 days ago

Member

Maybe just urls_expire_in? Unsure whether that's too generic but definitely not keen on rails_urls_expire_in

aki77 17 days ago

Author

Contributor

Thanks.
I think urls_expire_in would be a better name.

aki77 17 days ago

Author

Contributor

@pixeltrix

Changed to urls_expire_in.

Copy link

Member

zzak commented 20 days ago

@aki77 Could you rebase the CHANGELOG to a single entry and squash the commits? bow

Copy link

Contributor

Author

aki77 commented 20 days ago

edited

@zzak

I've changed the CHANGELOG and squashed it into one commit.

Copy link

Member

pixeltrix commented 18 days ago

Doesn't this change suffer from the same issues that @jeremy described in #38843 in that the rails urls can expire when inside a cached fragment?

Copy link

Contributor

Author

aki77 commented 18 days ago

edited

@pixeltrix

#38843 changed the whole behavior of ActiveStorage::Blob.signed_id, but this time it will be expired only if the expired_in argument is specified in the url helper.
I don't think anyone would bother to use an expired URL in a cached fragment.

https://github.com/rails/rails/pull/38843/files#diff-3c9b81c772d32ff6805463595e04d8d7e9031e875ebf2f9fb964ca1a78b5e56d

Copy link

Member

pixeltrix commented 17 days ago

@aki77 how did #38843 change the behaviour of signed_id - the pull request was closed without merging. Was a similar pull request merged later?

Copy link

Contributor

Author

aki77 commented 17 days ago

edited

@pixeltrix

The differences between #38843 and this PR are as follows.

38843

  • Breaking the default behavior
  • The signed_id is always set to expire, and there is no way to work around it, which causes problems in the following cases.
    • If the direct upload takes more than 5 minutes
    • When ActiveStorage URLs are used in the fragment cache
  • There is no way to create a persistent url.

This PR

  • The default behavior is not changed in any way.
  • ActiveStorage URLs can be set to expire only in cases where they are needed.
  • It is also possible to set ActiveStorage URLs to expire by default in the app-wide settings, which easily promotes overall security improvement.

My motivation is similar to the following comment.
#38843 (comment)

Copy link

Contributor

Author

aki77 commented 15 days ago

@pixeltrix @zzak Thanks for the review!
What else do I need to do to get this PR merged?

pixeltrix

merged commit 1e1ca11 into rails:main 14 days ago

4 checks passed

Copy link

Member

pixeltrix commented 14 days ago

@aki77 thanks 👍🏻

aki77

deleted the

aki77:activestorage-expiring-url

branch

14 days ago

Copy link

Contributor

Author

aki77 commented 14 days ago

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK