locker-mcp
Version:
MCP server for file locking and access control for AI code tools
121 lines • 5.14 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class FileManager {
static addLockComment(filePath, id, state) {
this.validateFilePath(filePath);
if (!fs.existsSync(filePath)) {
throw new Error(`File does not exist: ${filePath}`);
}
const content = fs.readFileSync(filePath, 'utf-8');
if (this.hasLockComment(content)) {
throw new Error(`File already has a lock comment: ${filePath}`);
}
const lockComment = `${this.LOCK_COMMENT_PREFIX} ${id} STATE=${state}`;
const newContent = lockComment + '\n' + content;
fs.writeFileSync(filePath, newContent, 'utf-8');
}
static removeLockComment(filePath) {
this.validateFilePath(filePath);
if (!fs.existsSync(filePath)) {
throw new Error(`File does not exist: ${filePath}`);
}
const content = fs.readFileSync(filePath, 'utf-8');
if (!this.hasLockComment(content)) {
return; // No lock comment to remove
}
const lines = content.split('\n');
const filteredLines = lines.filter(line => !this.LOCK_COMMENT_REGEX.test(line));
const newContent = filteredLines.join('\n');
fs.writeFileSync(filePath, newContent, 'utf-8');
}
static updateLockComment(filePath, id, state) {
this.validateFilePath(filePath);
if (!fs.existsSync(filePath)) {
throw new Error(`File does not exist: ${filePath}`);
}
const content = fs.readFileSync(filePath, 'utf-8');
if (!this.hasLockComment(content)) {
throw new Error(`File does not have a lock comment: ${filePath}`);
}
const lockComment = `${this.LOCK_COMMENT_PREFIX} ${id} STATE=${state}`;
const lines = content.split('\n');
const updatedLines = lines.map(line => this.LOCK_COMMENT_REGEX.test(line) ? lockComment : line);
const newContent = updatedLines.join('\n');
fs.writeFileSync(filePath, newContent, 'utf-8');
}
static hasLockComment(content) {
const lines = content.split('\n');
return lines.some(line => this.LOCK_COMMENT_REGEX.test(line));
}
static extractLockInfo(content) {
const lines = content.split('\n');
const lockLine = lines.find(line => this.LOCK_COMMENT_REGEX.test(line));
if (!lockLine) {
return null;
}
const idMatch = lockLine.match(/LOCKED: ([a-zA-Z0-9-]+)/);
const stateMatch = lockLine.match(/STATE=([\w-]+)/);
if (!idMatch || !stateMatch) {
return null;
}
return {
id: idMatch[1],
state: stateMatch[1]
};
}
static validateFilePath(filePath) {
const normalizedPath = path.normalize(filePath);
// Prevent path traversal attacks
if (normalizedPath.includes('..')) {
throw new Error(`Invalid file path: ${filePath}`);
}
// For testing: allow temp directories
if (normalizedPath.includes('/tmp/') || normalizedPath.includes('\\temp\\') || normalizedPath.includes('/var/folders/')) {
return;
}
// Ensure path is not absolute outside of current working directory (in production)
if (path.isAbsolute(normalizedPath) && !normalizedPath.startsWith(process.cwd())) {
throw new Error(`File path outside project directory: ${filePath}`);
}
}
}
exports.FileManager = FileManager;
FileManager.LOCK_COMMENT_PREFIX = '// LOCKED:';
FileManager.LOCK_COMMENT_REGEX = /^\/\/ LOCKED: [a-zA-Z0-9-]+ STATE=[\w-]+$/;
//# sourceMappingURL=file-manager.js.map