From afbede2e9446aa921c9e370c731752c9b31ef270 Mon Sep 17 00:00:00 2001
From: Richard Petersen <richard.petersen@open-xchange.com>
Date: Fri, 2 Sep 2022 17:05:33 +0200
Subject: [PATCH] Fixup for fastify: Only consider the path when serving files,
 not url params

---
 package.json               |  1 +
 src/create-app.js          |  2 ++
 src/plugins/serve-files.js |  2 +-
 yarn.lock                  | 10 +++++++++-
 4 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/package.json b/package.json
index e6bd3b8..f6e031a 100644
--- a/package.json
+++ b/package.json
@@ -21,6 +21,7 @@
     "@fastify/formbody": "^7.1.0",
     "@fastify/helmet": "^9.1.0",
     "@fastify/swagger": "^7.4.1",
+    "@fastify/url-data": "^5.1.0",
     "@open-xchange/logging": "^0.0.11",
     "bull": "^4.8.5",
     "dotenv-defaults": "^5.0.2",
diff --git a/src/create-app.js b/src/create-app.js
index b65a6d1..7881857 100644
--- a/src/create-app.js
+++ b/src/create-app.js
@@ -1,5 +1,6 @@
 import fastify from 'fastify'
 import formBodyPlugin from '@fastify/formbody'
+import urlDataPlugin from '@fastify/url-data'
 import fastifySwagger from '@fastify/swagger'
 import fastifyMetrics from 'fastify-metrics'
 import helmet from '@fastify/helmet'
@@ -22,6 +23,7 @@ export async function createApp (basePath) {
     connectionTimeout: 30000
   })
   await app.register(formBodyPlugin)
+  await app.register(urlDataPlugin)
   await app.register(helmet, {
     contentSecurityPolicy: false,
     crossOriginEmbedderPolicy: false,
diff --git a/src/plugins/serve-files.js b/src/plugins/serve-files.js
index f6b9cb9..4e64051 100644
--- a/src/plugins/serve-files.js
+++ b/src/plugins/serve-files.js
@@ -6,7 +6,7 @@ export default async function serveFilePlugin (fastify, options) {
   fastify.get('*', async (req, reply) => {
     try {
       const version = reply.version
-      const url = req.url.substr((options.prefix || '/').length - 1)
+      const url = req.urlData('path').substr((options.prefix || '/').length - 1)
       const path = url === '/' ? '/index.html' : url
       const { body, headers } = await getFile({ version, path })
 
diff --git a/yarn.lock b/yarn.lock
index 8c8cd2d..df294c7 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -98,6 +98,14 @@
     openapi-types "^11.0.0"
     rfdc "^1.3.0"
 
+"@fastify/url-data@^5.1.0":
+  version "5.1.0"
+  resolved "https://registry.yarnpkg.com/@fastify/url-data/-/url-data-5.1.0.tgz#1674b2f93ef877dde79db4f7ccc97eeff15c1d79"
+  integrity sha512-n8mz0FiXOOxFJP5QqoMTmfQ9Ejt56K+o+NzJVoDq6LWRZaA1cn0gfR8u0njuiliBTSgbrGovxz6oOQIOQfmW3w==
+  dependencies:
+    fastify-plugin "^4.0.0"
+    uri-js "^4.2.1"
+
 "@humanwhocodes/config-array@^0.10.4":
   version "0.10.4"
   resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.10.4.tgz#01e7366e57d2ad104feea63e72248f22015c520c"
@@ -3319,7 +3327,7 @@ undefsafe@^2.0.5:
   resolved "https://registry.yarnpkg.com/undefsafe/-/undefsafe-2.0.5.tgz#38733b9327bdcd226db889fb723a6efd162e6e2c"
   integrity sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==
 
-uri-js@^4.2.2:
+uri-js@^4.2.1, uri-js@^4.2.2:
   version "4.4.1"
   resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"
   integrity sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==
-- 
GitLab