fix: Sanitize channel names in CQL queries
This commit is contained in:
parent
81b9c032cd
commit
c9a7b77765
@ -5,6 +5,14 @@ interface Messages {
|
|||||||
error: Error | null;
|
error: Error | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sanitizeChannelName(channelName: string) {
|
||||||
|
return channelName
|
||||||
|
.toLowerCase()
|
||||||
|
.replaceAll(' ', '-')
|
||||||
|
.replaceAll(/[^a-z-]+/g, '')
|
||||||
|
.replaceAll('-', '_');
|
||||||
|
}
|
||||||
|
|
||||||
class Db {
|
class Db {
|
||||||
private client: cassandra.Client = new cassandra.Client({
|
private client: cassandra.Client = new cassandra.Client({
|
||||||
contactPoints: ['localhost'],
|
contactPoints: ['localhost'],
|
||||||
@ -32,6 +40,7 @@ class Db {
|
|||||||
// Create Channel Method
|
// Create Channel Method
|
||||||
async createChannel(channelName: string) {
|
async createChannel(channelName: string) {
|
||||||
try {
|
try {
|
||||||
|
channelName = sanitizeChannelName(channelName);
|
||||||
await this.client.execute(`
|
await this.client.execute(`
|
||||||
CREATE TABLE IF NOT EXISTS channels.${channelName} (
|
CREATE TABLE IF NOT EXISTS channels.${channelName} (
|
||||||
id UUID,
|
id UUID,
|
||||||
@ -50,6 +59,7 @@ class Db {
|
|||||||
async sendMessage(channelName: string, content: string, sender: string, id: string) {
|
async sendMessage(channelName: string, content: string, sender: string, id: string) {
|
||||||
try {
|
try {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
channelName = sanitizeChannelName(channelName);
|
||||||
await this.client.execute(`INSERT INTO channels.${channelName} (id, message_content, channel_name, timestamp, sender) VALUES (?, ?, ?, ?, ?)`, {
|
await this.client.execute(`INSERT INTO channels.${channelName} (id, message_content, channel_name, timestamp, sender) VALUES (?, ?, ?, ?, ?)`, {
|
||||||
id,
|
id,
|
||||||
message_content: content,
|
message_content: content,
|
||||||
@ -89,6 +99,7 @@ class Db {
|
|||||||
// Get messages method
|
// Get messages method
|
||||||
async getMessages(channelName: string, limit: number): Promise<Messages> {
|
async getMessages(channelName: string, limit: number): Promise<Messages> {
|
||||||
try {
|
try {
|
||||||
|
channelName = sanitizeChannelName(channelName);
|
||||||
const res = await this.client.execute(`SELECT * FROM channels.${channelName} WHERE channel_name = ? ORDER BY timestamp DESC LIMIT ${limit}`, {
|
const res = await this.client.execute(`SELECT * FROM channels.${channelName} WHERE channel_name = ? ORDER BY timestamp DESC LIMIT ${limit}`, {
|
||||||
channel_name: channelName,
|
channel_name: channelName,
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user