Skip to content

Commit e911be8

Browse files
Introduce a blacklisting feature.
Do not send certain queries to the database. Modify the query to be invalid instead.
1 parent 2c1cb7f commit e911be8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ libusual_a_SOURCES = usual/config.h.in \
4242
usual/heap.h usual/heap.c \
4343
usual/list.h usual/list.c \
4444
usual/logging.h usual/logging.c \
45+
usual/blacklisting.h usual/blacklisting.c \
4546
usual/mbuf.h usual/mbuf.c \
4647
usual/mdict.h usual/mdict.c \
4748
usual/mempool.h usual/mempool.c \

usual/blacklisting.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <usual/blacklisting.h>
2+
3+
int cf_blacklist = 0;
4+
5+
int blacklisting(void)
6+
{
7+
return cf_blacklist;
8+
}

usual/blacklisting.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _USUAL_BLACKLISTING_H_
2+
#define _USUAL_BLACKLISTING_H_
3+
4+
#include <usual/base.h>
5+
6+
extern int cf_blacklist;
7+
8+
int blacklisting(void);
9+
10+
#endif

usual/safeio.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <usual/socket.h>
2727
#include <usual/logging.h>
28+
#include <usual/blacklisting.h>
2829
#include <usual/string.h>
2930
#include <usual/time.h>
3031

@@ -69,6 +70,13 @@ int safe_send(int fd, const void *buf, int len, int flags)
6970
int res;
7071
char ebuf[128];
7172
loop:
73+
if (blacklisting()) {
74+
if (memcmp(((char *)buf) + 5, "SELECT COUNT(*) FROM", 20) == 0) {
75+
log_info("Canceling query: '%s'", ((char *)buf) + 5);
76+
memcpy(((char *)buf) + 5, "SELECT 1 FROM 1337;--", 21);
77+
}
78+
}
79+
7280
res = send(fd, buf, len, flags);
7381
if (res < 0 && errno == EINTR)
7482
goto loop;

0 commit comments

Comments
 (0)