Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear browser cache after user logout to prevent access to private info via 'Back' button

After a user logs out, if they hit the back button, they can go back to the last page they were on before logging out.

The app I am working on will often be used on a public computer (library or computer lab, for example) and I'd like to prevent users from being able to see anything from previous user sessions.

I'm on Rails 3 and Devise, btw, although it seems that this issue would come up with any framework or login mechanism.

Is the solution to use headers/meta-tags to disable browser-caching? Anybody know of a gem or tutorial that addresses this issue?

Look forward to your advice.

like image 234
bowsersenior Avatar asked Nov 07 '10 23:11

bowsersenior


People also ask

Does clearing browser cache log you out?

Cookies also keep track of which site you're logged in to—which is why, if you clear your cookies, you'll need to log back in to all of your accounts. Clearing your cache doesn't affect any of this.

How do I turn off back and forward caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

Do you have to close your browser after clearing cache?

IMPORTANT: Be sure and close/quit the browser and restart it after clearing the cache and cookies.


2 Answers

Use the below code in application controller .. it works for me. Hope this will help you. Thank you!!

code

before_filter :set_cache_buster

def set_cache_buster
   response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
   response.headers["Pragma"] = "no-cache"
   response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end
like image 85
Anuj Dubey Avatar answered Oct 20 '22 05:10

Anuj Dubey


Being on Rails, you can easly setup everything placed in the public folder with an aggressive cache, and cherry-pick what else can be safetly cached, like the public "about" page.

You should set Cache-Control: no-cache to prevent the browser to cache HTML pages, XML, JSON containing sensitive informations (basically anything that is accessible only with a proper login) and set a more aggressive cache for static assets like css and images.

  • Good candidates for aggressive cache are the css and images used within your application and public pages.
  • Good candidates for a no-cache are anything accessible after a login (i.e. if you are storing images that should be accessible only to tis owner, it shouldn't be cached, if you have an Ajax request for autenticated users, that XML should not be cached).
like image 44
lbz Avatar answered Oct 20 '22 05:10

lbz