CVE-2021-43856: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') (CWE-79)
value: Wiki.js is a wiki app built on Node.js. Wiki.js 2.5.263 and earlier is vulnerable to stored cross-site scripting through non-image file uploads for file types that can be viewed directly inline in the browser. By creating a malicious file which can execute inline JS when viewed in the browser (e.g. XML files), a malicious Wiki.js user may stage a stored cross-site scripting attack. This allows the attacker to execute malicious JavaScript when the file is viewed directly by other users. The file must be opened directly by the user and will not trigger directly in a normal Wiki.js page. A patch in version 2.5.264 fixes this vulnerability by adding an optional (enabled by default) force download flag to all non-image file types, preventing the file from being viewed inline in the browser. As a workaround, disable file upload for all non-trusted users. --- Thanks to @Haxatron for reporting this vulnerability. Initially reported via https://huntr.dev/bounties/266bff09-00d9-43ca-a4bb-bb540642811f/
CWE description: The product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users.
CVSS v3 base score: 5.4
Published: 2021-12-27T18:15Z
Repository: https://github.com/Requarks/wiki
Language: JavaScript
Files changed: client/components/admin/admin-security.vue, server/app/data.yml, server/graph/resolvers/site.js, server/graph/schemas/site.graphql, server/helpers/asset.js, server/models/assets.js
Commit date: 2021-12-24 20:18:12 -0500
Commit message: fix: force download of unsafe extensions

Diff: diff --git a/client/components/admin/admin-security.vue b/client/components/admin/admin-security.vue
index ce9c25838..7a8d305b2 100644
--- a/client/components/admin/admin-security.vue
+++ b/client/components/admin/admin-security.vue
@@ -149,10 +149,19 @@
                     color='primary'
                     v-model='config.uploadScanSVG'
                     persistent-hint
                     hint='Should SVG uploads be scanned for vulnerabilities and stripped of any potentially unsafe content.'
                     )
+                  v-divider.mt-3
+                  v-switch(
+                    inset
+                    label='Force Download of Unsafe Extensions'
+                    color='primary'
+                    v-model='config.uploadForceDownload'
+                    persistent-hint
+                    hint='Should non-image files be forced as downloads when accessed directly. This prevents potential XSS attacks via unsafe file extensions uploads.'
+                    )
 
               v-card.mt-3.animated.fadeInUp.wait-p2s
                 v-toolbar(flat, color='primary', dark, dense)
                   .subtitle-1 {{$t('admin:security.login')}}
                 //- v-card-info(color='blue')
@@ -250,10 +259,11 @@ export default {
     return {
       config: {
         uploadMaxFileSize: 0,
         uploadMaxFiles: 0,
         uploadScanSVG: true,
+        uploadForceDownload: true,
         securityOpenRedirect: true,
         securityIframe: true,
         securityReferrerPolicy: true,
         securityTrustProxy: true,
         securitySRI: true,
@@ -295,10 +305,11 @@ export default {
               $authJwtExpiration: String
               $authJwtRenewablePeriod: String
               $uploadMaxFileSize: Int
               $uploadMaxFiles: Int
               $uploadScanSVG: Boolean
+              $uploadForceDownload: Boolean
               $securityOpenRedirect: Boolean
               $securityIframe: Boolean
               $securityReferrerPolicy: Boolean
               $securityTrustProxy: Boolean
               $securitySRI: Boolean
@@ -317,10 +328,11 @@ export default {
                   authJwtExpiration: $authJwtExpiration,
                   authJwtRenewablePeriod: $authJwtRenewablePeriod,
                   uploadMaxFileSize: $uploadMaxFileSize,
                   uploadMaxFiles: $uploadMaxFiles,
                   uploadScanSVG: $uploadScanSVG
+                  uploadForceDownload: $uploadForceDownload,
                   securityOpenRedirect: $securityOpenRedirect,
                   securityIframe: $securityIframe,
                   securityReferrerPolicy: $securityReferrerPolicy,
                   securityTrustProxy: $securityTrustProxy,
                   securitySRI: $securitySRI,
@@ -348,10 +360,11 @@ export default {
             authJwtExpiration: _.get(this.config, 'authJwtExpiration', ''),
             authJwtRenewablePeriod: _.get(this.config, 'authJwtRenewablePeriod', ''),
             uploadMaxFileSize: _.toSafeInteger(_.get(this.config, 'uploadMaxFileSize', 0)),
             uploadMaxFiles: _.toSafeInteger(_.get(this.config, 'uploadMaxFiles', 0)),
             uploadScanSVG: _.get(this.config, 'uploadScanSVG', false),
+            uploadForceDownload: _.get(this.config, 'uploadForceDownload', false),
             securityOpenRedirect: _.get(this.config, 'securityOpenRedirect', false),
             securityIframe: _.get(this.config, 'securityIframe', false),
             securityReferrerPolicy: _.get(this.config, 'securityReferrerPolicy', false),
             securityTrustProxy: _.get(this.config, 'securityTrustProxy', false),
             securitySRI: _.get(this.config, 'securitySRI', false),
@@ -400,10 +413,11 @@ export default {
               authJwtExpiration
               authJwtRenewablePeriod
               uploadMaxFileSize
               uploadMaxFiles
               uploadScanSVG
+              uploadForceDownload
               securityOpenRedirect
               securityIframe
               securityReferrerPolicy
               securityTrustProxy
               securitySRI
diff --git a/server/app/data.yml b/server/app/data.yml
index cb9f2bf6c..0e5e45e21 100644
--- a/server/app/data.yml
+++ b/server/app/data.yml
@@ -79,10 +79,11 @@ defaults:
       sslRedir: false
     uploads:
       maxFileSize: 5242880
       maxFiles: 10
       scanSVG: true
+      forceDownload: true
     flags:
       ldapdebug: false
       sqllog: false
     # System defaults
     channel: STABLE
diff --git a/server/graph/resolvers/site.js b/server/graph/resolvers/site.js
index 161719fbf..c325706c9 100644
--- a/server/graph/resolvers/site.js
+++ b/server/graph/resolvers/site.js
@@ -28,11 +28,12 @@ module.exports = {
         authJwtAudience: WIKI.config.auth.audience,
         authJwtExpiration: WIKI.config.auth.tokenExpiration,
         authJwtRenewablePeriod: WIKI.config.auth.tokenRenewal,
         uploadMaxFileSize: WIKI.config.uploads.maxFileSize,
         uploadMaxFiles: WIKI.config.uploads.maxFiles,
-        uploadScanSVG: WIKI.config.uploads.scanSVG
+        uploadScanSVG: WIKI.config.uploads.scanSVG,
+        uploadForceDownload: WIKI.config.uploads.forceDownload
       }
     }
   },
   SiteMutation: {
     async updateConfig(obj, args, context) {
@@ -97,11 +98,12 @@ module.exports = {
         }
 
         WIKI.config.uploads = {
           maxFileSize: _.get(args, 'uploadMaxFileSize', WIKI.config.uploads.maxFileSize),
           maxFiles: _.get(args, 'uploadMaxFiles', WIKI.config.uploads.maxFiles),
-          scanSVG: _.get(args, 'uploadScanSVG', WIKI.config.uploads.scanSVG)
+          scanSVG: _.get(args, 'uploadScanSVG', WIKI.config.uploads.scanSVG),
+          forceDownload: _.get(args, 'uploadForceDownload', WIKI.config.uploads.forceDownload)
         }
 
         await WIKI.configSvc.saveToDb(['host', 'title', 'company', 'contentLicense', 'seo', 'logoUrl', 'auth', 'features', 'security', 'uploads'])
 
         if (WIKI.config.security.securityTrustProxy) {
diff --git a/server/graph/schemas/site.graphql b/server/graph/schemas/site.graphql
index 65875ac56..5544fffe8 100644
--- a/server/graph/schemas/site.graphql
+++ b/server/graph/schemas/site.graphql
@@ -53,10 +53,11 @@ type SiteMutation {
     securityCSP: Boolean
     securityCSPDirectives: String
     uploadMaxFileSize: Int
     uploadMaxFiles: Int
     uploadScanSVG: Boolean
+    uploadForceDownload: Boolean
 
   ): DefaultResponse @auth(requires: ["manage:system"])
 }
 
 # -----------------------------------------------
@@ -93,6 +94,7 @@ type SiteConfig {
   securityCSP: Boolean
   securityCSPDirectives: String
   uploadMaxFileSize: Int
   uploadMaxFiles: Int
   uploadScanSVG: Boolean
+  uploadForceDownload: Boolean
 }
diff --git a/server/helpers/asset.js b/server/helpers/asset.js
index 56f76e0d9..95b202db4 100644
--- a/server/helpers/asset.js
+++ b/server/helpers/asset.js
@@ -1,10 +1,15 @@
 const crypto = require('crypto')
+const path = require('path')
 
 module.exports = {
   /**
    * Generate unique hash from page
    */
   generateHash(assetPath) {
     return crypto.createHash('sha1').update(assetPath).digest('hex')
+  },
+
+  getPathInfo(assetPath) {
+    return path.parse(assetPath.toLowerCase())
   }
 }
diff --git a/server/models/assets.js b/server/models/assets.js
index 9d0a79b1c..94cb4a0e8 100644
--- a/server/models/assets.js
+++ b/server/models/assets.js
@@ -166,12 +166,19 @@ module.exports = class Asset extends Model {
     }
   }
 
   static async getAsset(assetPath, res) {
     try {
+      const fileInfo = assetHelper.getPathInfo(assetPath)
       const fileHash = assetHelper.generateHash(assetPath)
       const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${fileHash}.dat`)
+
+      // Force unsafe extensions to download
+      if (WIKI.config.uploads.forceDownload && !['.png', '.apng', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg'].includes(fileInfo.ext)) {
+        res.set('Content-disposition', 'attachment; filename=' + fileInfo.base)
+      }
+
       if (await WIKI.models.assets.getAssetFromCache(assetPath, cachePath, res)) {
         return
       }
       if (await WIKI.models.assets.getAssetFromStorage(assetPath, res)) {
         return

Vulnerable code:         uploadScanSVG: WIKI.config.uploads.scanSVG
          scanSVG: _.get(args, 'uploadScanSVG', WIKI.config.uploads.scanSVG)
Fixed code:                   v-divider.mt-3
                  v-switch(
                    inset
                    label='Force Download of Unsafe Extensions'
                    color='primary'
                    v-model='config.uploadForceDownload'
                    persistent-hint
                    hint='Should non-image files be forced as downloads when accessed directly. This prevents potential XSS attacks via unsafe file extensions uploads.'
                    )
        uploadForceDownload: true,
              $uploadForceDownload: Boolean
                  uploadForceDownload: $uploadForceDownload,
            uploadForceDownload: _.get(this.config, 'uploadForceDownload', false),
              uploadForceDownload
      forceDownload: true
        uploadScanSVG: WIKI.config.uploads.scanSVG,
        uploadForceDownload: WIKI.config.uploads.forceDownload
          scanSVG: _.get(args, 'uploadScanSVG', WIKI.config.uploads.scanSVG),
          forceDownload: _.get(args, 'uploadForceDownload', WIKI.config.uploads.forceDownload)
    uploadForceDownload: Boolean
  uploadForceDownload: Boolean
const path = require('path')
  },

  getPathInfo(assetPath) {
    return path.parse(assetPath.toLowerCase())
      const fileInfo = assetHelper.getPathInfo(assetPath)

      // Force unsafe extensions to download
      if (WIKI.config.uploads.forceDownload && !['.png', '.apng', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg'].includes(fileInfo.ext)) {
        res.set('Content-disposition', 'attachment; filename=' + fileInfo.base)
      }