~jan0sch/smederee

Showing details for patch 9e506252708e08521f491871be0c718a0ddc3a74.
2023-08-03 (Thu), 10:48 AM - Jens Grassel - 9e506252708e08521f491871be0c718a0ddc3a74

HTML: Extend MetaTags model by referrer policies

Summary of changes
2 files modified with 52 lines added and 2 lines removed
  • modules/html-utils/src/main/scala/de/smederee/html/MetaTags.scala with 50 added and 1 removed lines
  • modules/hub/src/main/twirl/de/smederee/hub/views/meta.scala.html with 2 added and 1 removed lines
diff -rN -u old-smederee/modules/html-utils/src/main/scala/de/smederee/html/MetaTags.scala new-smederee/modules/html-utils/src/main/scala/de/smederee/html/MetaTags.scala
--- old-smederee/modules/html-utils/src/main/scala/de/smederee/html/MetaTags.scala	2025-01-15 18:41:34.353141176 +0000
+++ new-smederee/modules/html-utils/src/main/scala/de/smederee/html/MetaTags.scala	2025-01-15 18:41:34.357141184 +0000
@@ -17,6 +17,50 @@
 
 package de.smederee.html
 
+enum MetaReferrerPolicy(val tag: String) {
+
+  /** The Referer header will be omitted: sent requests do not include any referrer information.
+    */
+  case NoReferrer extends MetaReferrerPolicy("no-referrer")
+
+  /** Send the origin, path, and querystring in Referer when the protocol security level stays the same or improves
+    * (HTTP -> HTTP, HTTP -> HTTPS, HTTPS -> HTTPS). Don't send the Referer header for requests to less secure
+    * destinations (HTTPS -> HTTP, HTTPS -> file).
+    */
+  case NoReferrerWhenDowngrade extends MetaReferrerPolicy("no-referrer-when-downgrade")
+
+  /** Send only the origin in the Referer header. For example, a document at https://example.com/page.html will send the
+    * referrer https://example.com/.
+    */
+  case Origin extends MetaReferrerPolicy("origin")
+
+  /** When performing a same-origin request to the same protocol level (HTTP -> HTTP, HTTPS -> HTTPS), send the origin,
+    * path, and query string. Send only the origin for cross origin requests and requests to less secure destinations
+    * (HTTPS -> HTTP).
+    */
+  case OriginWhenCrossOrigin extends MetaReferrerPolicy("origin-when-cross-origin")
+
+  /** Send the origin, path, and query string for same-origin requests. Don't send the Referer header for cross-origin
+    * requests.
+    */
+  case SameOrigin extends MetaReferrerPolicy("same-origin")
+
+  /** Send only the origin when the protocol security level stays the same (HTTPS -> HTTPS). Don't send the Referer
+    * header to less secure destinations (HTTPS -> HTTP).
+    */
+  case StrictOrigin extends MetaReferrerPolicy("strict-origin")
+
+  /** Send the origin, path, and querystring when performing a same-origin request. For cross-origin requests send the
+    * origin (only) when the protocol security level stays same (HTTPS -> HTTPS). Don't send the Referer header to less
+    * secure destinations (HTTPS -> HTTP).
+    */
+  case StrictOriginWhenCrossOrigin extends MetaReferrerPolicy("strict-origin-when-cross-origin")
+
+  /** Send the origin, path, and query string when performing any request, regardless of security.
+    */
+  case UnsafeUrl extends MetaReferrerPolicy("unsafe-url")
+}
+
 enum MetaRobotsDirective(val tag: String) {
 
   /** Equivalent to index, follow. Used by Google
@@ -129,12 +173,16 @@
   *   An optional description for the related meta tag which should not be too long (max. 160/200 characters).
   * @param keywords
   *   A list of keywords which can be empty and should not be too long.
+  * @param referrer
+  *   The Referrer-Policy HTTP header controls how much referrer information (sent with the Referer header) should be
+  *   included with requests.
   * @param robots
   *   A set of directives for the meta tag "robots" to affect search engine / crawler indexing behaviour.
   */
 final case class MetaTags(
     description: Option[MetaDescription],
     keywords: MetaKeyWords,
+    referrer: Option[MetaReferrerPolicy],
     robots: Set[MetaRobotsDirective]
 )
 
@@ -149,6 +197,7 @@
     MetaTags(
       description = None,
       keywords = MetaKeyWords.empty,
+      referrer = None,
       robots = Set(MetaRobotsDirective.NoFollow, MetaRobotsDirective.NoIndex)
     )
 
@@ -157,5 +206,5 @@
     * @return
     *   An instance of meta tags containing no values, resulting in no tags being rendered.
     */
-  def empty: MetaTags = MetaTags(description = None, keywords = MetaKeyWords.empty, robots = Set.empty)
+  def empty: MetaTags = MetaTags(description = None, keywords = MetaKeyWords.empty, referrer = None, robots = Set.empty)
 }
diff -rN -u old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/meta.scala.html new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/meta.scala.html
--- old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/meta.scala.html	2025-01-15 18:41:34.353141176 +0000
+++ new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/meta.scala.html	2025-01-15 18:41:34.357141184 +0000
@@ -1,3 +1,4 @@
 @(tags: MetaTags)
-@if(tags.description.nonEmpty){<meta name="description" content="@{tags.description}">}
+@for(description <- tags.description){<meta name="description" content="@description" />}
 @if(tags.keywords.nonEmpty){<meta name="keywords" content="@{tags.keywords.mkString}">}
+@for(referrer <- tags.referrer){<meta name="referrer" content="@{referrer.tag}" />}