diff --git a/lib/McdeSys/netmount.c b/lib/McdeSys/netmount.c index 9a25fbc3..a22a24f9 100644 --- a/lib/McdeSys/netmount.c +++ b/lib/McdeSys/netmount.c @@ -142,11 +142,22 @@ ParseUrl(const char *url, char *scheme, char *user, char *host, char *path) snprintf(host, 256, "%.*s", (int) n, p); if (slash != NULL) snprintf(path, 512, "%s", slash); - return GoodHost(host) && + /* a user that starts with "-" would be an option of sshfs: its first + * argument is "USER@HOST:PATH" (as a host may not start with "-") */ + return GoodHost(host) && user[0] != '-' && strspn(user, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789._-") == strlen(user) && strchr(path, '\n') == NULL; } +/* 1 when URL would reach a command line (the tests' view of ParseUrl) */ +int +mcde_sys_net_url_ok(const char *url) +{ + char scheme[16], user[64], host[256], path[512]; + + return url != NULL && ParseUrl(url, scheme, user, host, path); +} + /* the command that connects URL to MOUNTPOINT: the tool, its arguments, and * what goes to its standard input (a password for sshfs). Kept in one * structure so that mcde_sys_net_mount (waits) and mcde_sys_net_mount_start diff --git a/lib/McdeSys/private.h b/lib/McdeSys/private.h index 741ce6eb..edcbb458 100644 --- a/lib/McdeSys/private.h +++ b/lib/McdeSys/private.h @@ -98,6 +98,8 @@ const char *mcde_sys_afp_conf(void); void mcde_sys_export_line(const char *path, char *buf, size_t len); /* the backend: make running servers read their configuration again */ void mcde_sys_shares_reload(void); +/* netmount.c: would URL (sftp://, nfs://, afp://) reach a command line */ +int mcde_sys_net_url_ok(const char *url); /* netmount.c: a service of KIND found on HOST (NAME as it calls itself) * into HOSTS, merged by host; the new count */ int mcde_sys_net_add(McdeSysNetHost *hosts, int n, size_t max, diff --git a/lib/McdeSys/test_netmount.c b/lib/McdeSys/test_netmount.c index ebefdf1a..f977ee5f 100644 --- a/lib/McdeSys/test_netmount.c +++ b/lib/McdeSys/test_netmount.c @@ -34,6 +34,38 @@ static const struct { { ".", 0 }, /* empty after the dot */ }; +/* the address a person types (or a favourite holds): what may reach sshfs, + * mount_nfs and mount_afp as an argument (review 2026-11-13, tasksd t0081). + * The sftp tool gets "USER@HOST:PATH" as its first argument, so a USER that + * starts with "-" would be an option. */ +static const struct { + const char *url; + int ok; +} urls[] = { + { "sftp://host/", 1 }, + { "sftp://user@host/path", 1 }, + { "sftp://u.ser-1_x@10.0.0.1/p/q", 1 }, + { "nfs://server.local/export", 1 }, + { "afp://user@host/Volume", 1 }, + { "sftp://fe80::1/x", 1 }, /* IPv6 written bare */ + { "sftp://@host/", 1 }, /* an empty user: none */ + { "sftp://-oProxyCommand=id/", 0 }, /* a host that is an option */ + { "sftp://user@-host/", 0 }, + { "sftp://-l@host/", 0 }, /* a user that is an option */ + { "sftp://-p22@host/", 0 }, + { "sftp://us er@host/", 0 }, + { "sftp://user;id@host/", 0 }, + { "sftp://user@host name/", 0 }, + { "sftp://host/a\nb", 0 }, /* a newline in the path */ + { "sftp:///path", 0 }, /* no host */ + { "sftp://", 0 }, + { "host/path", 0 }, /* no scheme */ + { "abcdefghijklmnop://host/", 0 }, /* a scheme of 16 characters */ + { "sftp://user@host@evil/", 0 }, + { "sftp://host$(id)/", 0 }, + { "", 0 }, +}; + int main(void) { @@ -41,6 +73,40 @@ main(void) size_t i; int failures = 0, n; + for (i = 0; i < sizeof urls / sizeof urls[0]; i++) { + n = mcde_sys_net_url_ok(urls[i].url) != 0; + if (n != urls[i].ok) { + printf("FAIL url \"%s\": %s\n", urls[i].url, + n ? "accepted" : "refused"); + failures++; + } else + printf("ok url \"%s\": %s\n", urls[i].url, + n ? "accepted" : "refused"); + } + /* a host of 256 characters and one of 255 */ + { + char long_url[320]; + + memset(long_url, 0, sizeof long_url); + strcpy(long_url, "sftp://"); + memset(long_url + 7, 'a', 256); + strcat(long_url, "/"); + if (mcde_sys_net_url_ok(long_url)) { + printf("FAIL a host of 256 characters accepted\n"); + failures++; + } + long_url[7 + 255] = '\0'; + strcat(long_url, "/"); + if (!mcde_sys_net_url_ok(long_url)) { + printf("FAIL a host of 255 characters refused\n"); + failures++; + } + } + if (mcde_sys_net_url_ok(NULL)) { + printf("FAIL NULL accepted\n"); + failures++; + } + for (i = 0; i < sizeof cases / sizeof cases[0]; i++) { n = mcde_sys_net_add(hosts, 0, 4, "name", cases[i].host, MCDE_NET_HAS_SSH);