refactor: fix file condition checks

This commit is contained in:
wancat committed 2024-02-19 18:35:39 +08:00
1 parent 64215d3ef1
commit 4ab3096fa5
5 files changed
+16 -20

No files matched your search

+2 -2
View File
@@ -2,8 +2,8 @@ import { Schema } from 'mongoose'
export interface IFileCondition { export interface IFileCondition {
path: string path: string
type?: string type: string
exists?: boolean exists: boolean
} }
export interface ICondition { export interface ICondition {
+1
View File
@@ -5,6 +5,7 @@ export { Quest, IQuest } from './quest.js'
export { Session, ISession } from './session.js' export { Session, ISession } from './session.js'
export { Command, ICommand } from './command.js' export { Command, ICommand } from './command.js'
export { UserProfile, IUserProfile } from './userProfile.js' export { UserProfile, IUserProfile } from './userProfile.js'
export { IFileCondition, ICondition } from './condition.js'
export default async function connectDB(uri: string) { export default async function connectDB(uri: string) {
const db = await mongoose.connect(uri) const db = await mongoose.connect(uri)
+1 -1
View File
@@ -8,6 +8,6 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"watch": "tsc -w" "dev": "tsc -w"
} }
} }
+9 -15
View File
@@ -1,23 +1,17 @@
const fileTypes = { import type { IFileCondition } from '@linux-odyssey/models'
file: '-f',
folder: '-d',
link: '-L',
socket: '-S',
unknown: '-e',
}
export type FileType = keyof typeof fileTypes const fileTypes = new Map([
['file', '-f'],
export type File = { ['folder', '-d'],
path: string ['link', '-L'],
type: FileType ['socket', '-S'],
} ])
function sanitizePath(path: string): string { function sanitizePath(path: string): string {
return path.replace(/[^a-zA-Z0-9._/-]/g, '') return path.replace(/[^a-zA-Z0-9._/-]/g, '')
} }
export function buildFileCheckCmd(file: File): string[] { export function buildFileCheckCmd(file: IFileCondition): string[] {
const { path, type } = file const { path, type } = file
return ['test', fileTypes[type] || '-e', sanitizePath(path)] return ['test', fileTypes.get(type) || '-e', sanitizePath(path)]
} }
@@ -1,13 +1,14 @@
import { execFile } from 'child_process' import { execFile } from 'child_process'
import { buildFileCheckCmd } from '@linux-odyssey/utils' import { buildFileCheckCmd } from '@linux-odyssey/utils'
import type { IFileCondition } from '@linux-odyssey/models'
function sanitizeId(id) { function sanitizeId(id: string) {
return id.replace(/[^a-zA-Z0-9]/g, '') return id.replace(/[^a-zA-Z0-9]/g, '')
} }
// eslint-disable-next-line import/prefer-default-export // eslint-disable-next-line import/prefer-default-export
export function checkFile(id, file) { export function checkFile(id: string, file: IFileCondition) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const cmd = buildFileCheckCmd(file) const cmd = buildFileCheckCmd(file)
// WARNING: This is vulnerable to command injection // WARNING: This is vulnerable to command injection