Skip to content

Commit c48a275

Browse files
committed
add timestamp to auth log, add fail2ban conf+docs
1 parent 14d9171 commit c48a275

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Fail2Ban filter for code-server
2+
#
3+
#
4+
5+
[Definition]
6+
7+
8+
failregex = ^INFO\s+Failed login attempt\s+{\"password\":\"(\\.|[^"])*\",\"remote_address\":\"<HOST>\"
9+
10+
ignoreregex =
11+
12+
datepattern = "timestamp":{EPOCH}}$
13+
14+
# Author: Dean Sheather
15+

doc/security/fail2ban.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Protecting code-server from bruteforce attempts
2+
3+
code-server outputs all failed login attempts, along with the IP address,
4+
provided password, user agent and timestamp by default. When using a reverse
5+
proxy such as Nginx or Apache, the remote address may appear to be `127.0.0.1`
6+
or a similar address unless the `--trust-proxy` argument is provided to
7+
code-server.
8+
9+
When used with the `--trust-proxy` argument, code-server will use the last IP in
10+
`X-Forwarded-For` (if provided) instead of the remote socket address. Ensure
11+
that you are setting this value in your reverse proxy:
12+
13+
Nginx:
14+
```
15+
location / {
16+
...
17+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
18+
...
19+
}
20+
```
21+
22+
Apache:
23+
```
24+
<VirtualEnv>
25+
...
26+
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
27+
...
28+
</VirtualEnv>
29+
```
30+
31+
It is extremely important that if you enable `--trust-proxy` you ensure your
32+
code-server instance is not accessible from the internet (block it in your
33+
firewall).
34+
35+
## Fail2Ban
36+
37+
Fail2Ban allows for automatically banning and logging repeated failed
38+
authentication attempts for many applications through regex filters. A working
39+
filter for code-server can be found in `./code-server.fail2ban.conf`. Once this
40+
is installed and configured correctly, repeated failed login attempts should
41+
automatically be banned from connecting to your server.
42+

packages/server/src/server.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ export const createApp = async (options: CreateAppOptions): Promise<{
8989
if (cookies.password) {
9090
if (!safeCompare(cookies.password, options.password)) {
9191
let userAgent = req.headers["user-agent"];
92+
let timestamp = Math.floor(new Date().getTime() / 1000);
9293
if (Array.isArray(userAgent)) {
9394
userAgent = userAgent.join(", ");
9495
}
9596
logger.info("Failed login attempt",
9697
field("password", cookies.password),
9798
field("remote_address", remoteAddress(req)),
98-
field("user_agent", userAgent));
99+
field("user_agent", userAgent),
100+
field("timestamp", timestamp));
99101

100102
return false;
101103
}

0 commit comments

Comments
 (0)