From 3f84816b24b232d8bfde71c4590d439a1f6fa50b Mon Sep 17 00:00:00 2001
From: Effy Elden <git@effy.is>
Date: Sun, 15 Jan 2017 10:27:54 +1100
Subject: [PATCH 1/3] Add nice error page for CSRF errors/cookie issue, and fix
 error page handling altogether

---
 app/views/errors/404.html | 43 +++++++++++++++++++++++++++++++++++++++
 app/views/errors/410.html | 43 +++++++++++++++++++++++++++++++++++++++
 app/views/errors/422.html | 43 +++++++++++++++++++++++++++++++++++++++
 app/views/errors/500.html | 43 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 172 insertions(+)
 create mode 100644 app/views/errors/404.html
 create mode 100644 app/views/errors/410.html
 create mode 100644 app/views/errors/422.html
 create mode 100644 app/views/errors/500.html

diff --git a/app/views/errors/404.html b/app/views/errors/404.html
new file mode 100644
index 000000000..fc75c78be
--- /dev/null
+++ b/app/views/errors/404.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="utf-8">
+  <title>The page you were looking for doesn't exist</title>
+  <meta name="viewport" content="width=device-width,initial-scale=1">
+  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
+  <style>
+    body {
+      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      background: #282c37;
+      color: #9baec8;
+      text-align: center;
+      margin: 0;
+      padding: 20px;
+    }
+
+    .dialog img {
+      display: block;
+      margin: 20px auto;
+      margin-top: 50px;
+      max-width: 600px;
+      width: 100%;
+      height: auto;
+    }
+
+    .dialog h1 {
+      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      font-weight: 400;
+    }
+  </style>
+</head>
+
+<body>
+  <div class="dialog">
+    <img src="/oops.png" alt="Mastodon" />
+
+    <div>
+      <h1>The page you were looking for doesn't exist</h1>
+    </div>
+  </div>
+</body>
+</html>
diff --git a/app/views/errors/410.html b/app/views/errors/410.html
new file mode 100644
index 000000000..e07cbe863
--- /dev/null
+++ b/app/views/errors/410.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="utf-8">
+  <title>The page you were looking for doesn't exist anymore</title>
+  <meta name="viewport" content="width=device-width,initial-scale=1">
+  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
+  <style>
+    body {
+      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      background: #282c37;
+      color: #9baec8;
+      text-align: center;
+      margin: 0;
+      padding: 20px;
+    }
+
+    .dialog img {
+      display: block;
+      margin: 20px auto;
+      margin-top: 50px;
+      max-width: 600px;
+      width: 100%;
+      height: auto;
+    }
+
+    .dialog h1 {
+      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      font-weight: 400;
+    }
+  </style>
+</head>
+
+<body>
+  <div class="dialog">
+    <img src="/oops.png" alt="Mastodon" />
+
+    <div>
+      <h1>The page you were looking for doesn't exist anymore</h1>
+    </div>
+  </div>
+</body>
+</html>
diff --git a/app/views/errors/422.html b/app/views/errors/422.html
new file mode 100644
index 000000000..a214781bd
--- /dev/null
+++ b/app/views/errors/422.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="utf-8">
+  <title>Security verification failed - are you blocking cookies?</title>
+  <meta name="viewport" content="width=device-width,initial-scale=1">
+  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
+  <style>
+    body {
+      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      background: #282c37;
+      color: #9baec8;
+      text-align: center;
+      margin: 0;
+      padding: 20px;
+    }
+
+    .dialog img {
+      display: block;
+      margin: 20px auto;
+      margin-top: 50px;
+      max-width: 600px;
+      width: 100%;
+      height: auto;
+    }
+
+    .dialog h1 {
+      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      font-weight: 400;
+    }
+  </style>
+</head>
+
+<body>
+  <div class="dialog">
+    <img src="/oops.png" alt="Mastodon" />
+
+    <div>
+      <h1>We're sorry, but we were unable to verify your request. Have you tried enabling cookies?</h1>
+    </div>
+  </div>
+</body>
+</html>
diff --git a/app/views/errors/500.html b/app/views/errors/500.html
new file mode 100644
index 000000000..d085d490b
--- /dev/null
+++ b/app/views/errors/500.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="utf-8">
+  <title>We're sorry, but something went wrong</title>
+  <meta name="viewport" content="width=device-width,initial-scale=1">
+  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
+  <style>
+    body {
+      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      background: #282c37;
+      color: #9baec8;
+      text-align: center;
+      margin: 0;
+      padding: 20px;
+    }
+
+    .dialog img {
+      display: block;
+      margin: 20px auto;
+      margin-top: 50px;
+      max-width: 600px;
+      width: 100%;
+      height: auto;
+    }
+
+    .dialog h1 {
+      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+      font-weight: 400;
+    }
+  </style>
+</head>
+
+<body>
+  <div class="dialog">
+    <img src="/oops.png" alt="Mastodon" />
+
+    <div>
+      <h1>We're sorry, but something went wrong.</h1>
+    </div>
+  </div>
+</body>
+</html>

From ed41f9f0b1d49e651c93cc9df90c21daca3f96a5 Mon Sep 17 00:00:00 2001
From: Effy Elden <git@effy.is>
Date: Sun, 15 Jan 2017 10:30:23 +1100
Subject: [PATCH 2/3] Add nice error page for CSRF errors/cookie issue, and fix
 error page handling altogether

---
 app/controllers/application_controller.rb | 10 ++++++
 public/404.html                           | 43 -----------------------
 public/500.html                           | 43 -----------------------
 3 files changed, 10 insertions(+), 86 deletions(-)
 delete mode 100644 public/404.html
 delete mode 100644 public/500.html

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 0a6b50a29..e8cd48b6e 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -11,6 +11,7 @@ class ApplicationController < ActionController::Base
 
   rescue_from ActionController::RoutingError, with: :not_found
   rescue_from ActiveRecord::RecordNotFound, with: :not_found
+  rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
 
   before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
   before_action :set_locale
@@ -50,12 +51,21 @@ class ApplicationController < ActionController::Base
   def not_found
     respond_to do |format|
       format.any  { head 404 }
+      format.html { render "errors/404" }
     end
   end
 
   def gone
     respond_to do |format|
       format.any  { head 410 }
+      format.html { render "errors/410" }
+    end
+  end
+
+  def unprocessable_entity
+    respond_to do |format|
+      format.any  { head 422 }
+      format.html { render "errors/422" }
     end
   end
 
diff --git a/public/404.html b/public/404.html
deleted file mode 100644
index fc75c78be..000000000
--- a/public/404.html
+++ /dev/null
@@ -1,43 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-  <meta charset="utf-8">
-  <title>The page you were looking for doesn't exist</title>
-  <meta name="viewport" content="width=device-width,initial-scale=1">
-  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
-  <style>
-    body {
-      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      background: #282c37;
-      color: #9baec8;
-      text-align: center;
-      margin: 0;
-      padding: 20px;
-    }
-
-    .dialog img {
-      display: block;
-      margin: 20px auto;
-      margin-top: 50px;
-      max-width: 600px;
-      width: 100%;
-      height: auto;
-    }
-
-    .dialog h1 {
-      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      font-weight: 400;
-    }
-  </style>
-</head>
-
-<body>
-  <div class="dialog">
-    <img src="/oops.png" alt="Mastodon" />
-
-    <div>
-      <h1>The page you were looking for doesn't exist</h1>
-    </div>
-  </div>
-</body>
-</html>
diff --git a/public/500.html b/public/500.html
deleted file mode 100644
index d085d490b..000000000
--- a/public/500.html
+++ /dev/null
@@ -1,43 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-  <meta charset="utf-8">
-  <title>We're sorry, but something went wrong</title>
-  <meta name="viewport" content="width=device-width,initial-scale=1">
-  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
-  <style>
-    body {
-      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      background: #282c37;
-      color: #9baec8;
-      text-align: center;
-      margin: 0;
-      padding: 20px;
-    }
-
-    .dialog img {
-      display: block;
-      margin: 20px auto;
-      margin-top: 50px;
-      max-width: 600px;
-      width: 100%;
-      height: auto;
-    }
-
-    .dialog h1 {
-      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      font-weight: 400;
-    }
-  </style>
-</head>
-
-<body>
-  <div class="dialog">
-    <img src="/oops.png" alt="Mastodon" />
-
-    <div>
-      <h1>We're sorry, but something went wrong.</h1>
-    </div>
-  </div>
-</body>
-</html>

From e22a56183a26283941a3260341ef84ce03e8ba44 Mon Sep 17 00:00:00 2001
From: Eugen Rochko <eugen@zeonfederated.com>
Date: Sat, 21 Jan 2017 22:30:47 +0100
Subject: [PATCH 3/3] Improve error page layouting. 500 page has to stay static
 because it's used from nginx when Rails fails.

---
 app/controllers/application_controller.rb |  6 ++--
 app/views/errors/404.html                 | 43 -----------------------
 app/views/errors/404.html.haml            |  5 +++
 app/views/errors/410.html                 | 43 -----------------------
 app/views/errors/410.html.haml            |  5 +++
 app/views/errors/422.html                 | 43 -----------------------
 app/views/errors/422.html.haml            |  5 +++
 app/views/layouts/error.html.haml         | 36 +++++++++++++++++++
 {app/views/errors => public}/500.html     |  0
 9 files changed, 54 insertions(+), 132 deletions(-)
 delete mode 100644 app/views/errors/404.html
 create mode 100644 app/views/errors/404.html.haml
 delete mode 100644 app/views/errors/410.html
 create mode 100644 app/views/errors/410.html.haml
 delete mode 100644 app/views/errors/422.html
 create mode 100644 app/views/errors/422.html.haml
 create mode 100644 app/views/layouts/error.html.haml
 rename {app/views/errors => public}/500.html (100%)

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index e8cd48b6e..e4b6d0faf 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -51,21 +51,21 @@ class ApplicationController < ActionController::Base
   def not_found
     respond_to do |format|
       format.any  { head 404 }
-      format.html { render "errors/404" }
+      format.html { render 'errors/404', layout: 'error' }
     end
   end
 
   def gone
     respond_to do |format|
       format.any  { head 410 }
-      format.html { render "errors/410" }
+      format.html { render 'errors/410', layout: 'error' }
     end
   end
 
   def unprocessable_entity
     respond_to do |format|
       format.any  { head 422 }
-      format.html { render "errors/422" }
+      format.html { render 'errors/422', layout: 'error' }
     end
   end
 
diff --git a/app/views/errors/404.html b/app/views/errors/404.html
deleted file mode 100644
index fc75c78be..000000000
--- a/app/views/errors/404.html
+++ /dev/null
@@ -1,43 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-  <meta charset="utf-8">
-  <title>The page you were looking for doesn't exist</title>
-  <meta name="viewport" content="width=device-width,initial-scale=1">
-  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
-  <style>
-    body {
-      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      background: #282c37;
-      color: #9baec8;
-      text-align: center;
-      margin: 0;
-      padding: 20px;
-    }
-
-    .dialog img {
-      display: block;
-      margin: 20px auto;
-      margin-top: 50px;
-      max-width: 600px;
-      width: 100%;
-      height: auto;
-    }
-
-    .dialog h1 {
-      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      font-weight: 400;
-    }
-  </style>
-</head>
-
-<body>
-  <div class="dialog">
-    <img src="/oops.png" alt="Mastodon" />
-
-    <div>
-      <h1>The page you were looking for doesn't exist</h1>
-    </div>
-  </div>
-</body>
-</html>
diff --git a/app/views/errors/404.html.haml b/app/views/errors/404.html.haml
new file mode 100644
index 000000000..ba1d5f72d
--- /dev/null
+++ b/app/views/errors/404.html.haml
@@ -0,0 +1,5 @@
+- content_for :page_title do
+  The page you were looking for doesn't exist
+
+- content_for :content do
+  The page you were looking for doesn't exist
diff --git a/app/views/errors/410.html b/app/views/errors/410.html
deleted file mode 100644
index e07cbe863..000000000
--- a/app/views/errors/410.html
+++ /dev/null
@@ -1,43 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-  <meta charset="utf-8">
-  <title>The page you were looking for doesn't exist anymore</title>
-  <meta name="viewport" content="width=device-width,initial-scale=1">
-  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
-  <style>
-    body {
-      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      background: #282c37;
-      color: #9baec8;
-      text-align: center;
-      margin: 0;
-      padding: 20px;
-    }
-
-    .dialog img {
-      display: block;
-      margin: 20px auto;
-      margin-top: 50px;
-      max-width: 600px;
-      width: 100%;
-      height: auto;
-    }
-
-    .dialog h1 {
-      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      font-weight: 400;
-    }
-  </style>
-</head>
-
-<body>
-  <div class="dialog">
-    <img src="/oops.png" alt="Mastodon" />
-
-    <div>
-      <h1>The page you were looking for doesn't exist anymore</h1>
-    </div>
-  </div>
-</body>
-</html>
diff --git a/app/views/errors/410.html.haml b/app/views/errors/410.html.haml
new file mode 100644
index 000000000..07cf3742f
--- /dev/null
+++ b/app/views/errors/410.html.haml
@@ -0,0 +1,5 @@
+- content_for :page_title do
+  The page you were looking for doesn't exist anymore
+
+- content_for :content do
+  The page you were looking for doesn't exist anymore
diff --git a/app/views/errors/422.html b/app/views/errors/422.html
deleted file mode 100644
index a214781bd..000000000
--- a/app/views/errors/422.html
+++ /dev/null
@@ -1,43 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-  <meta charset="utf-8">
-  <title>Security verification failed - are you blocking cookies?</title>
-  <meta name="viewport" content="width=device-width,initial-scale=1">
-  <link href="https://fonts.googleapis.com/css?family=Roboto:400" rel="stylesheet">
-  <style>
-    body {
-      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      background: #282c37;
-      color: #9baec8;
-      text-align: center;
-      margin: 0;
-      padding: 20px;
-    }
-
-    .dialog img {
-      display: block;
-      margin: 20px auto;
-      margin-top: 50px;
-      max-width: 600px;
-      width: 100%;
-      height: auto;
-    }
-
-    .dialog h1 {
-      font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
-      font-weight: 400;
-    }
-  </style>
-</head>
-
-<body>
-  <div class="dialog">
-    <img src="/oops.png" alt="Mastodon" />
-
-    <div>
-      <h1>We're sorry, but we were unable to verify your request. Have you tried enabling cookies?</h1>
-    </div>
-  </div>
-</body>
-</html>
diff --git a/app/views/errors/422.html.haml b/app/views/errors/422.html.haml
new file mode 100644
index 000000000..e369cded6
--- /dev/null
+++ b/app/views/errors/422.html.haml
@@ -0,0 +1,5 @@
+- content_for :page_title do
+  Security verification failed
+
+- content_for :content do
+  Security verification failed. Are you blocking cookies?
diff --git a/app/views/layouts/error.html.haml b/app/views/layouts/error.html.haml
new file mode 100644
index 000000000..54563f7d8
--- /dev/null
+++ b/app/views/layouts/error.html.haml
@@ -0,0 +1,36 @@
+!!!
+%html{:lang => "en"}
+  %head
+    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
+    %meta{:charset => "utf-8"}/
+    %title= yield :page_title
+    %meta{:content => "width=device-width,initial-scale=1", :name => "viewport"}/
+    %link{:href => "https://fonts.googleapis.com/css?family=Roboto:400", :rel => "stylesheet"}/
+    :css
+      body {
+        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+        background: #282c37;
+        color: #9baec8;
+        text-align: center;
+        margin: 0;
+        padding: 20px;
+      }
+
+      .dialog img {
+        display: block;
+        margin: 20px auto;
+        margin-top: 50px;
+        max-width: 600px;
+        width: 100%;
+        height: auto;
+      }
+
+      .dialog h1 {
+        font: 20px/28px -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
+        font-weight: 400;
+      }
+  %body
+    .dialog
+      %img{:alt => "Mastodon", :src => "/oops.png"}/
+      %div
+        %h1= yield :content
diff --git a/app/views/errors/500.html b/public/500.html
similarity index 100%
rename from app/views/errors/500.html
rename to public/500.html