Skip to content

Commit 784ac09

Browse files
committed
protocol: fix host origin checking
Thanks @ben365 (tsl0922#75)
1 parent 7a25074 commit 784ac09

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/protocol.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,28 @@ check_host_origin(struct lws *wsi) {
6060
char buf[origin_length + 1];
6161
memset(buf, 0, sizeof(buf));
6262
int len = lws_hdr_copy(wsi, buf, sizeof(buf), WSI_TOKEN_ORIGIN);
63-
if (len > 0) {
64-
const char *prot, *address, *path;
65-
int port;
66-
if (lws_parse_uri(buf, &prot, &address, &port, &path))
67-
return false;
63+
if (len <= 0) {
64+
return false;
65+
}
66+
67+
const char *prot, *address, *path;
68+
int port;
69+
if (lws_parse_uri(buf, &prot, &address, &port, &path))
70+
return false;
71+
if (port == 80 || port == 443) {
72+
sprintf(buf, "%s", address);
73+
} else {
6874
sprintf(buf, "%s:%d", address, port);
69-
int host_length = lws_hdr_total_length(wsi, WSI_TOKEN_HOST);
70-
if (host_length != strlen(buf))
71-
return false;
72-
char host_buf[host_length + 1];
73-
memset(host_buf, 0, sizeof(host_buf));
74-
len = lws_hdr_copy(wsi, host_buf, sizeof(host_buf), WSI_TOKEN_HOST);
75-
return len > 0 && strcasecmp(buf, host_buf) == 0;
7675
}
77-
return false;
76+
77+
int host_length = lws_hdr_total_length(wsi, WSI_TOKEN_HOST);
78+
if (host_length != strlen(buf))
79+
return false;
80+
char host_buf[host_length + 1];
81+
memset(host_buf, 0, sizeof(host_buf));
82+
len = lws_hdr_copy(wsi, host_buf, sizeof(host_buf), WSI_TOKEN_HOST);
83+
84+
return len > 0 && strcasecmp(buf, host_buf) == 0;
7885
}
7986

8087
void

src/server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ main(int argc, char **argv) {
270270
}
271271
break;
272272
case 'i':
273-
strncpy(iface, optarg, sizeof(iface));
273+
strncpy(iface, optarg, sizeof(iface) - 1);
274274
iface[sizeof(iface) - 1] = '\0';
275275
break;
276276
case 'c':

0 commit comments

Comments
 (0)