-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix code scanning alert no. 8: Uncontrolled data used in path expression #15
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -199,10 +199,16 @@ | |||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||
// Middleware to serve any .yml files in USER_DATA_DIR with optional protection | ||||||||||||||||||||||||||||||||||||||
.get('/*.yml', protectConfig, (req, res) => { | ||||||||||||||||||||||||||||||||||||||
const ymlFile = req.path.split('/').pop(); | ||||||||||||||||||||||||||||||||||||||
res.sendFile(path.join(__dirname, process.env.USER_DATA_DIR || 'user-data', ymlFile)); | ||||||||||||||||||||||||||||||||||||||
const userDataDir = path.resolve(__dirname, process.env.USER_DATA_DIR || 'user-data'); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Extract repeated USER_DATA_DIR logic into a constant The
|
||||||||||||||||||||||||||||||||||||||
const resolvedPath = path.resolve(userDataDir, ymlFile); | ||||||||||||||||||||||||||||||||||||||
if (!resolvedPath.startsWith(userDataDir)) { | ||||||||||||||||||||||||||||||||||||||
res.status(403).send('Forbidden'); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨 suggestion (security): Enhance error handling with logging for security monitoring While sending a 403 status is appropriate, consider adding logging for these access attempts. This can help with security monitoring and detecting potential attacks.
|
||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
res.sendFile(resolvedPath); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling and security controls for file sending The current implementation lacks error handling and additional security controls for file sending. Apply these security improvements: -res.sendFile(resolvedPath);
+res.sendFile(resolvedPath, {
+ headers: {
+ 'Content-Type': 'application/x-yaml',
+ 'X-Content-Type-Options': 'nosniff'
+ },
+ maxAge: '1h',
+ lastModified: true
+}, (err) => {
+ if (err) {
+ console.error(`[Error] Failed to send file ${ymlFile}:`, err);
+ if (err.code === 'ENOENT') {
+ res.status(404).send('File not found');
+ } else {
+ res.status(500).send('Internal server error');
+ }
+ }
+}); 📝 Committable suggestion
Suggested change
Comment on lines
+204
to
+210
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Based on the analysis of the results, I can now provide a final response: Path traversal protection needed for static file serving middleware The codebase has multiple file access patterns that require attention:
Other file operations in the codebase are either:
🔗 Analysis chainVerify similar file handling patterns across the codebase Let's verify if there are other endpoints that might need similar path traversal protection. Let me analyze the findings and generate additional verification scripts to better understand the file access patterns and security measures. This will help us understand:
🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Find other potential file access patterns that might need similar protection
# Look for file system operations
echo "Searching for file system operations..."
rg -n "(?:readFile|writeFile|readdir|sendFile|createReadStream)" --type js
# Look for path manipulation
echo "\nSearching for path manipulation..."
rg -n "(?:path\.join|path\.resolve|req\.path)" --type js
# Look for static file serving
echo "\nSearching for static file serving..."
rg -n "(?:express\.static|res\.sendFile)" --type js
Length of output: 2413 Script: #!/bin/bash
# Let's examine the context around file operations to understand security measures
# Check file access patterns in server.js
echo "Examining server.js file operations context..."
rg -B 5 -A 5 "(?:readFile|writeFile|sendFile)" server.js
# Check static file serving middleware setup
echo "\nExamining static file serving setup..."
rg -B 3 -A 3 "express\.static" server.js
# Check if there are any other routes handling files
echo "\nChecking for route handlers with file operations..."
ast-grep --pattern 'app.$_($_, function ($_, $_) {
$$$
res.sendFile($_);
$$$
}'
Length of output: 2105 |
||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||
Check failure Code scanning / CodeQL Missing rate limiting High
This route handler performs
a file system access Error loading related location Loading |
||||||||||||||||||||||||||||||||||||||
// Serves up static files | ||||||||||||||||||||||||||||||||||||||
.use(express.static(path.join(__dirname, process.env.USER_DATA_DIR || 'user-data'))) | ||||||||||||||||||||||||||||||||||||||
.use(express.static(path.join(__dirname, 'dist'))) | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enhance input validation for yml file paths
The current implementation extracts the filename without proper sanitization, which could potentially allow malicious filenames. Consider adding these security measures:
Apply this improvement:
📝 Committable suggestion