From 78daf86eb3b224825be812aad673c6c0f7184589 Mon Sep 17 00:00:00 2001 From: lda Date: Wed, 29 Nov 2023 12:57:42 +0100 Subject: [PATCH] [MOD/WIP] Start doing checks on room alias NOTE: Currently UNTESTED. --- src/Parser.c | 2 ++ src/Routes/RouteAliasDirectory.c | 41 ++++++++++++++++++++++++++++---- src/include/Parser.h | 8 +++++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/Parser.c b/src/Parser.c index 3dda8a0..e514c6d 100644 --- a/src/Parser.c +++ b/src/Parser.c @@ -267,6 +267,8 @@ ParseCommonID(char *str, CommonID *id) switch (sigil) { + case '#': /* It seems like the localpart should be the same as the + user's: everything, except ':'. */ case '@': if (!ParseUserLocalpart(&str, &id->local)) { diff --git a/src/Routes/RouteAliasDirectory.c b/src/Routes/RouteAliasDirectory.c index b1ae042..7a4772f 100644 --- a/src/Routes/RouteAliasDirectory.c +++ b/src/Routes/RouteAliasDirectory.c @@ -27,6 +27,9 @@ #include #include + +#include +#include #include ROUTE_IMPL(RouteAliasDirectory, path, argp) @@ -43,15 +46,33 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) JsonValue *val; char *token; + char *msg; User *user = NULL; - /* TODO: Return HTTP 400 M_INVALID_PARAM if alias is invalid */ + CommonID commonID; + Config *config; + + commonID.sigil = '\0'; + commonID.local = NULL; + commonID.server.hostname = NULL; + commonID.server.port = NULL; + + config = ConfigLock(db); + + if (!ParseCommonID(alias, &commonID) || commonID.sigil != '#') + { + msg = "Invalid room alias."; + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_INVALID_PARAM, msg); + goto finish; + } ref = DbLock(db, 1, "aliases"); if (!ref && !(ref = DbCreate(db, 1, "aliases"))) { + msg = "Unable to access alias database.", HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); - response = MatrixErrorCreate(M_UNKNOWN, "Unable to access alias database."); + response = MatrixErrorCreate(M_UNKNOWN, msg); goto finish; } @@ -69,8 +90,9 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) } else { + msg = "There is no mapped room ID for this room alias."; HttpResponseStatus(args->context, HTTP_NOT_FOUND); - response = MatrixErrorCreate(M_NOT_FOUND, "There is no mapped room ID for this room alias."); + response = MatrixErrorCreate(M_NOT_FOUND, msg); } break; case HTTP_PUT: @@ -93,8 +115,15 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) { HashMap *newAlias; - /* TODO: Validate alias domain and make sure it matches - * server name and is well formed. */ + /* Check for server name. + * TODO: Take the port into account, that might need a + * refactor for it to use a ServerPart */ + if (!StrEquals(commonID.server.hostname, config->serverName)) + { + HttpResponseStatus(args->context, HTTP_BAD_REQUEST); + response = MatrixErrorCreate(M_INVALID_PARAM, "Invalid servername"); + goto finish; + } if (JsonGet(aliases, 2, "alias", alias)) { @@ -156,6 +185,8 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp) } finish: + CommonIDFree(commonID); + ConfigUnlock(config); UserUnlock(user); DbUnlock(db, ref); JsonFree(request); diff --git a/src/include/Parser.h b/src/include/Parser.h index 788f92f..68853b5 100644 --- a/src/include/Parser.h +++ b/src/include/Parser.h @@ -44,8 +44,8 @@ typedef struct ServerPart { char *port; } ServerPart; /** - * A common identifier in the form '&local[:server]', where - * & determines the *type* of the identifier. + * A common identifier in the form '&local[:server]', where & determines the + * *type* of the identifier. */ typedef struct CommonID { char sigil; @@ -59,5 +59,9 @@ typedef struct CommonID { */ extern int ParseCommonID(char *, CommonID *); +/** + * Frees a CommonID's values. Note that it doesn't free the CommonID itself. */ +extern void CommonIDFree(CommonID id); + #endif /* TELODENDRIA_PARSER_H */