CVE-2020-7763 (published 2020-11-05T14:15Z)

This affects the package phantom-html-to-pdf before 0.6.1.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/pofider/phantom-html-to-pdf
commit_details: repo_url: https://github.com/pofider/phantom-html-to-pdf
author: pofider
author_date: 2020-11-02 15:51:26+01:00
msg: fix access to files without protocol
repository: repo_url: https://github.com/pofider/phantom-html-to-pdf
repo_name: pofider/phantom-html-to-pdf
description: Highly scalable html to pdf conversion using phantom workers
repo_language: JavaScript
owner: pofider
file_changes: filename: conversionScriptPart.js
change_type: ModificationType.MODIFY
diff: @@ -19,19 +19,19 @@ page.onResourceRequested = function (request, networkRequest) {
     console.log('Request ' + request.url);
     if (request.url.lastIndexOf(body.url, 0) === 0) {
         return;
-    }
-
-    //potentially dangerous request
-    if (request.url.lastIndexOf("file:///", 0) === 0 && !body.allowLocalFilesAccess) {
-        networkRequest.abort();
-        return;
-    }
+    }   
 
     //to support cdn like format //cdn.jquery...
     if (request.url.lastIndexOf("file://", 0) === 0 && request.url.lastIndexOf("file:///", 0) !== 0) {
         networkRequest.changeUrl(request.url.replace("file://", "http://"));
     }
 
+     //potentially dangerous request
+     if (request.url.lastIndexOf("http://", 0) !== 0 && request.url.lastIndexOf("https://", 0) && !body.allowLocalFilesAccess) {
+        networkRequest.abort();
+        return;
+    }
+
     if (body.waitForJS && request.url.lastIndexOf("http://intruct-javascript-ending", 0) === 0) {
         pageJSisDone = true;
     }

programming_language: JavaScript
filename: test.js
change_type: ModificationType.MODIFY
diff: @@ -1,6 +1,6 @@
-var should = require("should"),
+var should = require("should"),   
     path = require("path"),
-    fs = require("fs"),
+    fs = require("fs"),    
     phantomjs = require("phantomjs"),
     phantomjs2 = require("phantomjs-prebuilt")
     tmpDir = path.join(__dirname, "temp"),
@@ -95,21 +95,7 @@ describe("phantom html to pdf", function () {
                     done();
                 });
             });
-        });
-
-        it('should create a pdf file ignoring ssl errors', function(done) {
-            conversion({
-                url: 'https://sygris.com'
-            }, function(err, res) {
-                if (err) {
-                    return done(err);
-                }
-
-                res.numberOfPages.should.be.eql(1);
-                res.stream.should.have.property("readable");
-                done();
-            });
-        });
+        });       
 
         it('should wait for page js execution', function(done) {
             conversion({
@@ -201,7 +187,7 @@ describe("phantom html to pdf", function () {
                     return done(err);
                 }
 
-                JSON.stringify(res.logs).should.containEql('foo');
+                ;
                 done();
             });
         });
@@ -261,6 +247,35 @@ describe("phantom html to pdf", function () {
                 done();
             })
         });
+
+        it('should reject local files', function(done) {
+            conversion({
+                html: `<script>
+                document.write(window.location='${__filename.replace(/\\/g, '/')}')                
+              </script>`
+            }, function(err, res) {
+                if (err) {
+                    return done(err);
+                }
+                JSON.stringify(res.logs).should.containEql('Unable to load resource')
+                done()
+            });
+        });
+
+        it('should allow local files when allowLocalFilesAccess', function(done) {
+            conversion({
+                allowLocalFilesAccess: true,
+                html: `<script>
+                document.write(window.location='${__filename.replace(/\\/g, '/')}')                
+              </script>`
+            }, function(err, res) {
+                if (err) {
+                    return done(err);
+                }
+                JSON.stringify(res.logs).should.not.containEql('Unable to load resource')
+                done()
+            });
+        });
     }
 
     rmDir = function (dirPath) {

programming_language: JavaScript