Force rack-offline (or Rails) to update caching manifest - ruby-on-rails

I use Rails 4 with rack-offline to cache my pages. I also want to cache (dynamically generated) html pages for offline browsing. Problem is, with the way HTML5 works, they stay stale until the application manifest is updated.
The docs say:
In production, it generates a SHA hash once based on the contents of
all the assets in the manifest. This means that the cache manifest will
not be considered stale unless the underlying assets change.
Is there any way I can trigger a new hash generation when one of my html pages change? This would, for example, after a database update.

Probably not, because AppCache is meant for static resources. Best to use a static HTML page, and use JavaScript to load in dynamic content.
Check out appcachefacts.info for more information.

Related

HTML5 Application Cache need explanation

Can someone show a complete example of application cache with html, css, js, appcache file including CACHE, NETWORK and FALLBACK section. Also updating the manifest. Where should the coding be written?
http://www.html5rocks.com/en/tutorials/appcache/beginner/#toc-updating-cache
As per updating cache from the above link, where should the coding been written?
The code for updating the manifest is written by your sever somewhere.
Either in PHP or Node.js you must write and serve this file with the correct mime type as specified in the link you posted.
You can auto generate this from the css and js files on your server. Don't include html files unless they are dynamic pages.
The first line in the file must be CACHE MANIFEST
Now it assumes your are putting things into the CACHE section, which is where you need to include all the paths to your css and js that you want the user to be able to use offline.
To create a NETWORK section, simply print out the word on it's own line.
Under this section you should include pages that should only be used online.
Under the FALLBACK section include a page to show if there is no offline version available.
This is a brief explanation but you should be able to easily find a tutorial that will help you auto generate this file.
For more details about the cache manifest itself:
http://diveintohtml5.info/offline.html
Offers the best explanation IMHO.

How to force a browser to download page content resources?

Too frequently I see issues where customers/clients have to refresh the webpage, typically after clearing their history/cache, only to find out that the webpage is perfectly fine.
Is there any way to add a version or just force the browser to download all resources, such as scripts, images, ...etc?
One common way to do this is to append a nonce or version number to the URL of these resources:
myStylesheet.css?ver=1.1
logo.png?ver=13.1
When you change the file, you update the version number and any cached resource gets updated.
You can also ensure you are using the same version number (or whatever) for every time you make any changes to the codebase.

Rails passing additional params in some tag helpers

Currently image_tag("file.jpg") produces normal image html tag, BUT src="file.jpg**?7485793246**" What are those numbers anyway and how to disable them?
Those are refered to as Asset Timestamps they can be used by the server to cache files. For example lets say you have a file called file.jpg on your server, you can set up your server to tell browsers like firefox to cache the file.jpg so the next time that browser visits your web-page it loads faster because file.jpg was already in memory.
The problem comes when you upload a new file.jpg because even though the image is different, your old users who have the image cached will still see the cached image, that is where asset timestamps come into play. Those numbers represent a timestamp of when the file was updated, so if you replace file.jpg?123456789 with file.jpg?987654321 then the user's browser will not use the cached version.
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html
long story short, it is only there to help you, and doesn't affect the way the file behaves at all. Users can still download the file and operating systems will see it as being a valid .jpg.
thats your development mode making sure that nothing gets cached so that if you change the image it actually gets to the browser. the production version won't have it.
Solved, add this to environment.rb => ENV['RAILS_ASSET_ID'] = ''
Conclusion: yes, its a good rails stuff, but when you deal with ie6 and PNG images it can break your script. So be careful.

Secure File Upload in Ruby On Rails

I built a photo gallery which uses Paperclip and validates the content-type using validates_attachment_content_type.
The application runs on a shared host with Passenger.
Is it possible to bypass the validation and run malicious scripts from the public/pictures directory? If so, is there anything that I can do to avoid evil scripts from running or from being uploaded?
Is it possible to bypass the validation and run malicious scripts from the public/pictures directory?
Yes. You can have a perfectly valid renderable image file that also contains HTML with script injection. Thanks for the bogus content-sniffing, IE, you have ruined everything.
See http://webblaze.cs.berkeley.edu/2009/content-sniffing/ for a summary.
If so, is there anything that I can do to avoid evil scripts from running or from being uploaded?
Not really. In theory you can check the first 256 bytes for HTML tags, but then you have to know the exact details of what browsers content-sniff for, and keeping that comprehensive and up-to-date is a non-starter.
If you are processing the images and re-saving them yourself that can protect you. Otherwise, do one or both of:
only serve user-uploaded files from a different hostname, so they don't have access to the cookie/auth details that would allow an injected script to XSS into your site. (but look out for non-XSS attacks like general JavaScript/plugin exploits)
serve user-uploaded files through a server-side script that includes the 'Content-Disposition: attachment' header, so browsers don't attempt to view the page inline. (but look out of old versions of Flash ignoring it for Flash files) This approach also means you don't have to store files on your server filesystem under the filename the user submits, which saves you some heavy and difficult-to-get-right filename validation work.

Question marks after images and js/css files in rails. Why?

Does anyone know why there are question marks (with a number) after the images and css files (when looking at the html code)? And how can I turn them off?
From Rails API documentation:
By default, Rails will append all
asset paths with that asset‘s
timestamp. This allows you to set a
cache-expiration date for the asset
far into the future, but still be able
to instantly invalidate it by simply
updating the file (and hence updating
the timestamp, which then updates the
URL as the timestamp is part of that,
which in turn busts the cache).
Hope it helps.
It is to be able to cache the file on the client and still making sure the client receive the newest version when there is a change. So each file modification results in a new timestamp which the client will do a new request to the server to receive the modified file.
If you do not want to use (though I cannot see why - it is a good thing) simple do not use the rails helpers for including javascripts or stylesheets. Just include the normal HTML tags: link and script.

Resources