add some fancy magic :kekw:

This commit is contained in:
2025-10-13 18:21:23 +02:00
parent 2e59c1f5e7
commit 22badaf535
15 changed files with 1328 additions and 786 deletions

View File

@@ -1,118 +1,120 @@
var logger = require('winston');
var KeyGenerator = require('./key_generator.js');
const KeyGenerator = require('./key_generator.js');
// handles creating new and requesting existing documents
var DocumentHandler = function(options) {
if (!options) {
options = {};
}
this.store = options.store;
this.maxLength = options.maxLength || 50000;
this.keyLength = options.keyLength || 10;
this.createKey = options.createKey || '';
this.keyGenerator = new KeyGenerator();
class DocumentHandler {
constructor(options) {
if (!options) {
options = {};
}
this.store = options.store;
this.logger = options.logger;
this.maxLength = options.maxLength || 50000;
this.keyLength = options.keyLength || 10;
this.createKey = options.createKey || '';
this.keyGenerator = new KeyGenerator();
if (this.createKey !== '') {
logger.info("Creation-Key:", this.createKey);
}
};
if (this.createKey !== '') {
this.logger.info("Creation-Key is configured.");
}
}
// handles existing documents
DocumentHandler.prototype.handleGet = function(key, res) {
this.store.get(key, function(ret) {
if (ret) {
logger.verbose('Open paste:', key);
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify({key: key, data: ret.replace(/\t/g, ' ')}));
} else {
logger.verbose('Paste not found:', key);
res.writeHead(404, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Paste not found.'}));
}
});
};
// handles existing documents
handleGet(key, res) {
this.store.get(key, (ret) => {
if (ret) {
this.logger.verbose(`Open paste: ${key}`);
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ key: key, data: ret.replace(/\t/g, ' ') }));
} else {
this.logger.verbose(`Paste not found: ${key}`);
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Paste not found.' }));
}
});
}
// handles exisiting documents (raw)
DocumentHandler.prototype.handleRawGet = function(key, res) {
this.store.get(key, function(ret) {
if (ret) {
logger.verbose('Open paste:', key);
res.writeHead(200, {'content-type': 'text/plain'});
res.end(ret);
} else {
logger.verbose('Paste not found:', key);
res.writeHead(404, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Paste not found.'}));
}
});
};
// handles existing documents (raw)
handleRawGet(key, res) {
this.store.get(key, (ret) => {
if (ret) {
this.logger.verbose(`Open raw paste: ${key}`);
res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' });
res.end(ret);
} else {
this.logger.verbose(`Paste not found: ${key}`);
res.writeHead(404, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Paste not found.' }));
}
});
}
// handles creating new documents
DocumentHandler.prototype.handlePost = function(req, res) {
var _this = this;
var buffer = '';
var cancelled = false;
req.on('data', function(data) {
if (cancelled) return;
buffer += data.toString();
if (_this.maxLength && buffer.length > _this.maxLength) {
cancelled = true;
logger.warn('Paste exeeds maximum length.');
res.writeHead(400, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Paste exceeds maximum length.'}));
}
});
req.on('end', function() {
if (cancelled) return;
// handles creating new documents
handlePost(req, res) {
let buffer = '';
let cancelled = false;
if (_this.createKey !== '') {
if (!buffer.startsWith(_this.createKey)) {
logger.warn('Error adding new paste: wrong key');
res.writeHead(400, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Error adding new paste: wrong key'}));
return;
}
buffer = buffer.substring(_this.createKey.length);
}
req.on('data', (data) => {
if (cancelled) return;
buffer += data.toString();
if (this.maxLength && buffer.length > this.maxLength) {
cancelled = true;
this.logger.warn('Paste exceeds maximum length.');
res.writeHead(400, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Paste exceeds maximum length.' }));
}
});
_this.chooseKey(function(key) {
_this.store.set(key, buffer, function(success) {
if (success) {
logger.verbose('New paste:', key);
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify({key: key}));
} else {
logger.warn('Error adding new paste.');
res.writeHead(500, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Error adding new paste.'}));
}
});
});
});
req.on('error', function(error) {
logger.error('Connection error: ' + error.message);
res.writeHead(500, {'content-type': 'application/json'});
res.end(JSON.stringify({message: 'Connection error.'}));
});
};
req.on('end', () => {
if (cancelled) return;
// creates new keys until one is not taken
DocumentHandler.prototype.chooseKey = function(callback) {
var key = this.acceptableKey();
var _this = this;
this.store.get(key, function(success) {
if (success) {
_this.chooseKey(callback);
} else {
callback(key);
}
});
};
if (this.createKey !== '') {
if (!buffer.startsWith(this.createKey)) {
this.logger.warn('Error adding new paste: wrong key');
res.writeHead(403, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Error adding new paste: wrong key' }));
return;
}
buffer = buffer.substring(this.createKey.length);
}
// creates a new key using the key-generator
DocumentHandler.prototype.acceptableKey = function() {
return this.keyGenerator.createKey(this.keyLength);
};
this.chooseKey((key) => {
this.store.set(key, buffer, (success) => {
if (success) {
this.logger.verbose(`New paste: ${key}`);
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ key: key }));
} else {
this.logger.warn('Error adding new paste.');
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Error adding new paste.' }));
}
});
});
});
module.exports = DocumentHandler;
req.on('error', (error) => {
this.logger.error(`Connection error: ${error.message}`);
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify({ message: 'Connection error.' }));
});
}
// creates new keys until one is not taken
chooseKey(callback) {
const key = this.acceptableKey();
this.store.get(key, (success) => {
if (success) {
this.chooseKey(callback);
} else {
callback(key);
}
});
}
// creates a new key using the key-generator
acceptableKey() {
return this.keyGenerator.createKey(this.keyLength);
}
}
module.exports = DocumentHandler;

View File

@@ -1,38 +1,75 @@
var fs = require('fs');
var crypto = require('crypto');
const fs = require('fs');
const path = require('path');
var logger = require('winston');
// A simple key regex to prevent path traversal
const validKeyRegex = /^[a-zA-Z0-9]+$/;
// handles saving and retrieving all documents
var FileDocumentStore = function(options) {
this.basePath = options.path || './data';
logger.info('Path to data: ' + this.basePath);
};
class FileDocumentStore {
constructor(options) {
this.basePath = options.path || './data';
this.logger = options.logger;
this.logger.info('Path to data: ' + this.basePath);
// Create directory if it does not exist
if (!fs.existsSync(this.basePath)) {
fs.mkdirSync(this.basePath, { recursive: true, mode: '700' });
}
}
// saves a new file to the filesystem
FileDocumentStore.prototype.set = function(key, data, callback) {
var _this = this;
fs.mkdir(this.basePath, '700', function(err) {
fs.writeFile(_this.basePath + '/' + key, data, 'utf8', function(err) {
if (err) {
callback(false);
} else {
callback(true);
}
});
});
};
// saves a new file to the filesystem
set(key, data, callback) {
if (!validKeyRegex.test(key)) {
this.logger.warn(`Invalid key provided for set: ${key}`);
return callback(false);
}
// gets an exisiting file from the filesystem
FileDocumentStore.prototype.get = function(key, callback) {
var _this = this;
fs.readFile(this.basePath + '/' + key, 'utf8', function(err, data) {
if (err) {
callback(false);
} else {
callback(data);
}
});
};
const resolvedBasePath = path.resolve(this.basePath);
const filePath = path.resolve(resolvedBasePath, key);
module.exports = FileDocumentStore;
// Security check to ensure we are not writing outside of the data directory
if (path.dirname(filePath) !== resolvedBasePath) {
this.logger.error(`Path traversal attempt detected for key: ${key}`);
return callback(false);
}
fs.writeFile(filePath, data, 'utf8', (err) => {
if (err) {
this.logger.error('Error writing file:', err);
callback(false);
} else {
callback(true);
}
});
}
// gets an existing file from the filesystem
get(key, callback) {
if (!validKeyRegex.test(key)) {
this.logger.warn(`Invalid key provided for get: ${key}`);
return callback(false);
}
const resolvedBasePath = path.resolve(this.basePath);
const filePath = path.resolve(resolvedBasePath, key);
// Security check to ensure we are not reading outside of the data directory
if (path.dirname(filePath) !== resolvedBasePath) {
this.logger.error(`Path traversal attempt detected for key: ${key}`);
return callback(false);
}
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
// Don't log error if file just doesn't exist
if (err.code !== 'ENOENT') {
this.logger.error('Error reading file:', err);
}
callback(false);
} else {
callback(data);
}
});
}
}
module.exports = FileDocumentStore;

View File

@@ -1,15 +1,18 @@
var KeyGenerator = function() {
this.keyspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
};
const crypto = require('crypto');
KeyGenerator.prototype.createKey = function(keyLength) {
var key = '';
var index;
for (var i = 0; i < keyLength; i++) {
index = Math.floor(Math.random() * this.keyspace.length);
key += this.keyspace.charAt(index);
}
return key;
};
class KeyGenerator {
constructor() {
this.keyspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
}
module.exports = KeyGenerator;
createKey(keyLength) {
const buffer = crypto.randomBytes(keyLength);
let key = '';
for (let i = 0; i < buffer.length; i++) {
key += this.keyspace.charAt(buffer[i] % this.keyspace.length);
}
return key;
}
}
module.exports = KeyGenerator;