osc: ignore OSC 7 when hostname doesn't match our own hostname

This commit is contained in:
Daniel Eklöf 2020-04-03 20:15:54 +02:00
parent a208104743
commit 7466e87a31
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

37
osc.c
View file

@ -2,6 +2,8 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h>
#include <unistd.h>
#define LOG_MODULE "osc" #define LOG_MODULE "osc"
#define LOG_ENABLE_DBG 0 #define LOG_ENABLE_DBG 0
@ -320,36 +322,53 @@ osc_set_pwd(struct terminal *term, char *string)
return; return;
string += 7; string += 7;
/* Skip past hostname */ const char *hostname = string;
if ((string = strchr(string, '/')) == NULL) char *hostname_end = strchr(string, '/');
if (hostname_end == NULL)
return; return;
char this_host[HOST_NAME_MAX];
if (gethostname(this_host, sizeof(this_host)) < 0)
this_host[0] = '\0';
/* Ignore this CWD if the hostname isn't 'localhost' or our gethostname() */
size_t hostname_len = hostname_end - hostname;
if (strncmp(hostname, "", hostname_len) != 0 &&
strncmp(hostname, "localhost", hostname_len) != 0 &&
strncmp(hostname, this_host, hostname_len) != 0)
{
LOG_DBG("ignoring OSC 7: hostname mismatch: %.*s != %s",
(int)hostname_len, hostname, this_host);
return;
}
/* Decode %xx encoded characters */ /* Decode %xx encoded characters */
char *pwd = malloc(strlen(string) + 1); const char *path = hostname_end;
char *pwd = malloc(strlen(path) + 1);
char *p = pwd; char *p = pwd;
while (true) { while (true) {
/* Find next '%' */ /* Find next '%' */
char *next = strchr(string, '%'); const char *next = strchr(path, '%');
if (next == NULL) { if (next == NULL) {
strcpy(p, string); strcpy(p, path);
break; break;
} }
/* Copy everything leading up to the '%' */ /* Copy everything leading up to the '%' */
size_t prefix_len = next - string; size_t prefix_len = next - path;
memcpy(p, string, prefix_len); memcpy(p, path, prefix_len);
p += prefix_len; p += prefix_len;
if (isxdigit(next[1]) && isxdigit(next[2])) { if (isxdigit(next[1]) && isxdigit(next[2])) {
*p++ = nibble2hex(next[1]) << 4 | nibble2hex(next[2]); *p++ = nibble2hex(next[1]) << 4 | nibble2hex(next[2]);
*p = '\0'; *p = '\0';
string = next + 3; path = next + 3;
} else { } else {
*p++ = *next; *p++ = *next;
*p = '\0'; *p = '\0';
string = next + 1; path = next + 1;
} }
} }