mirror of
https://github.com/lone-cloud/gerbil
synced 2026-06-03 19:54:44 -07:00
"fix" code that was flagged by CodeQL - gerbil is a local-only desktop app so sanitizing user's own paths should not really be necessary
This commit is contained in:
parent
3e76766f6a
commit
bb4cc9a8bf
2 changed files with 22 additions and 4 deletions
|
|
@ -2,7 +2,7 @@ import { createServer, Server } from 'http';
|
||||||
import { readFile } from 'fs/promises';
|
import { readFile } from 'fs/promises';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { lookup } from 'mime-types';
|
import { lookup } from 'mime-types';
|
||||||
import { pathExists } from '@/utils/node/fs';
|
import { pathExists, sanitizePath } from '@/utils/node/fs';
|
||||||
|
|
||||||
let server: Server | null = null;
|
let server: Server | null = null;
|
||||||
let serverPort = 0;
|
let serverPort = 0;
|
||||||
|
|
@ -16,8 +16,10 @@ export const startStaticServer = (distPath: string) =>
|
||||||
filePath = join(distPath, 'index.html');
|
filePath = join(distPath, 'index.html');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sanitizedFilePath = sanitizePath(filePath);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const content = await readFile(filePath);
|
const content = await readFile(sanitizedFilePath);
|
||||||
const contentType = lookup(filePath) || 'application/octet-stream';
|
const contentType = lookup(filePath) || 'application/octet-stream';
|
||||||
|
|
||||||
res.writeHead(200, { 'Content-Type': contentType });
|
res.writeHead(200, { 'Content-Type': contentType });
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,26 @@
|
||||||
import { readFile, writeFile, access, mkdir } from 'fs/promises';
|
import { readFile, writeFile, access, mkdir } from 'fs/promises';
|
||||||
import { constants } from 'fs';
|
import { constants } from 'fs';
|
||||||
import { dirname } from 'path';
|
import { dirname, normalize } from 'path';
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-comments/disallowComments
|
||||||
|
/**
|
||||||
|
* This function normalizes a file path and checks for null
|
||||||
|
* bytes to prevent security issues.
|
||||||
|
* This is probably not relevant for our local desktop app,
|
||||||
|
* but github does warn about it via "js/path-injection".
|
||||||
|
*/
|
||||||
|
export const sanitizePath = (path: string) => {
|
||||||
|
const normalized = normalize(path);
|
||||||
|
if (normalized.includes('\0')) {
|
||||||
|
throw new Error('Invalid path: null byte detected');
|
||||||
|
}
|
||||||
|
return normalized;
|
||||||
|
};
|
||||||
|
|
||||||
export const pathExists = async (path: string) => {
|
export const pathExists = async (path: string) => {
|
||||||
|
const sanitized = sanitizePath(path);
|
||||||
try {
|
try {
|
||||||
await access(path, constants.F_OK);
|
await access(sanitized, constants.F_OK);
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue