Unpoly 2: Don't try to download files through AJAX requests

Posted Over 2 years ago by Jakob Scholz.

Rails has the handy controller method send_file which lets us download files easily. We can decide whether the file...

You should probably load your JavaScript with <script defer>

Posted Over 2 years ago by Henning Koch.

It is generally discouraged to load your JavaScript by a tag in the : The reason is that a tag will pause the DOM parser until the script has loaded and executed. This will delay the browser's first contentful paint. A much better default is to load your scripts with a tag: A deferred script has many...

Unobtrusive JavaScript helper to progressively enhance HTML

Posted Over 2 years ago by Henning Koch.

The attached compiler() function below applies JavaScript behavior to matching HTML elements as they enter the DOM. This works like...

Using attribute event handlers with a strict Content Security Policy (CSP)

Posted Over 2 years ago by Henning Koch.

Given you have a strict CSP that only allows elements from your own domain: Content-Security-Policy: script-src 'self' This will block JavaScript handlers inlined as attribute into your HTML elements. Clicking on the following link will only log an error with a strict CSP: click me click me Solution 1: Move the handler into your JavaScript The recommended solution is to move the handler from the HTML to the allowed JavaScript file that we loaded via . In the example above we could invent a new [data-alert] attribute with the alert message: click me Then our JavaScript intercepts clicks on elements with that attribute: document.addEventListener('click', function(event) { let link = event.target.closest('[data-alert]') if (link) { let message = link.dataset.alert alert(message) event.preventDefault() } }) Solution 2: Allow that one handler in your CSP Some browsers allow the CSP directive script-src-attr. This lets you allow the hashes of actual JavaScript code. The SHA256 hash of alert('hello') is vIsp2avtxDy0157AryO+jEJVpLdmka7PI7o7C4q5ABE= (in Base64). We can allow this one event handlers like this: Content-Security-Policy: script-src 'self'; script-src-attr 'unsafe-hashes' 'sha256-vIsp2avtxDy0157AryO+jEJVpLdmka7PI7o7C4q5ABE=' Note the sha256- prefix. This event handler now works when clicked: click me But any other script will still be blocked: click me Dealing with legacy browsers Currently (November 2023) about 75% of browsers support script-src-attr. Here is a forward-looking compromise that many users use with new CSP features: Have a liberal CSP with old directives supported by all browsers Make your CSP stricter with new, more specific directives for browsers that support it The CSP spec supports that approach in that using newer, more specific directives disable older, more general features. In our case this means: For old browsers, allow all inline scripts For new browsers, disallow inline scripts but allow inline handlers with given hashes Here is a CSP directive that works like this: Content-Security-Policy: script-src 'self' 'unsafe-inline'; script-src-elem 'self'; script-src-attr 'unsafe-hashes' 'sha256-vIsp2avtxDy0157AryO+jEJVpLdmka7PI7o7C4q5ABE=' Old browsers will only use script-src. New browsers will use script-src-elem (for tags) and script-src-attr (for inline event handlers), which override the more liberal rules from script-src. Solution 3: Don't use a strict CSP You can have a secure web application without a strict CSP. A Content Security Policy should never be your first line of defense against Cross-Site-Scripting (XSS). Your primary tool should be to escape and sanitize user input. Many template engines make it easy to do the right thing by escaping dynamic content by default. A CSP is a second security net. It's there when you slip up and somehow, somewhere don't sanitize user input. Depending on your project, you may decide to prioritize developer convenience over a strict CSP.

Triggering JavaScript when an element is clicked

Posted Over 2 years ago.

Often people need links which are not linked directly, but should trigger execution of JavaScript. ❌ Bad workarounds

Spreewald development steps

Posted Over 2 years ago by Emanuel.

Our gem spreewald supports a few helpers for development. In case you notice errors in your Cucumber tests, you might...

Modern HTTP Status codes for redirecting

Posted Almost 3 years ago.
en.wikipedia.org

Formerly 301 (Moved Permanently) and 302 (Found) were used for redirecting. Browsers did implement them in different ways, so since...

RubyMine / IntelliJ: How to increase UI and fonts for presentations

Posted Almost 3 years ago by Arne Hartherz.

When giving a presentation where you do some coding, the font size you usually use is probably a bit too...

Accessing JavaScript objects from Capybara/Selenium

Posted Almost 3 years ago by Dominic Beger.

When testing JavaScript functionality in Selenium (E2E), you may need to access a class or function inside of a evaluate...

Marko Denic's list of TIL for HTML

Posted Almost 3 years ago by Emanuel.
markodenic.com

Table of content for the linked article: 1. The `loading=lazy` attribute 2. Email, call, and SMS links

Regular Expressions: Space Separators

Posted Almost 3 years ago by Bruno Sedler.

Matching the "space" character class For matching whitespaces in a regular expression, the most common and best-known shorthand expression...

When does Webpacker compile?

Posted Almost 3 years ago by Henning Koch.

Webpack builds can take a long time, so we only want to compile when needed. This card shows what will...

Delivering Carrierwave attachments to authorized users only

Posted About 3 years ago by Dominic Beger.

To attach files to your records, you will need a new database column representing the filename of the file...

The Ruby Object Model

Posted About 3 years ago by Dominic Beger.

In Ruby (almost) everything is an Object. While this enables a lot of powerful features, this concept might be confusing...

RSpec: How to test the content of a flash message in a request spec

Posted About 3 years ago by Emanuel.

The ActionDispatch module of Rails gives you the helper method flash to access the flash messages in a response.

CarrierWave: Default Configuration and Suggested Changes

Posted About 3 years ago by Dominik Schöler.

CarrierWave comes with a set of default configuration options which make sense in most cases. However, you should review these...

Some tips for upgrading Bootstrap from 3 to 4

Posted About 3 years ago by Florian Leinsinger.
getbootstrap.com

Recently I made an upgrade from Bootstrap 3 to Bootstrap 4 in a bigger project. Here are some tips how...

ActiveType #change_association: Define { autosave: true } on parent models

Posted About 3 years ago by Jakob Scholz.

Consider the following models and form models: class Parent < ApplicationRecord has_many :children, class_name: 'Child', foreign_key: 'parent_id...

WYSIWYG with Action Text

Posted About 3 years ago by Niklas Hasselmeyer.

Rails 6 includes a WYSIWYG editor, Action Text. It works out of the box quite well, but chances are that...

Rails: How to use custom flash types in controllers

Posted About 3 years ago by Emanuel.

Rails supports alert and notice as default flash types. This allows you to use these keys as options in e.g...

Git: Parsing large diffs as a human

Posted Over 3 years ago by Michael Leimstädtner.

I just finished migrating a project from the Asset Pipeline to Webpacker, this is what my diff to master looks...

Using the Truemail gem to validate e-mail addresses

Posted Over 3 years ago by Arne Hartherz.

The Truemail gem (not to be confused with truemail.io) allows validating email addresses, e.g. when users enter them into a...

How to build a fully custom TinyMCE 5 dialog

Posted Over 3 years ago.

TinyMCE is a WYSIWYG editor which is quite customizable. Add a custom button to the tinyMCE toolbar and tell tinyMCE...

Using the alt attribute and the figcaption element in HTML

Posted Over 3 years ago by Emanuel.
thoughtbot.com

The linked blog post contains some background information about the alt attribute and the figure / figcaption element. It is interesting...