Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session: Validate the token for correctness #1562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/xdp-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ xdp_session_authorize_callback (GDBusInterfaceSkeleton *interface,
return TRUE;
}

static gboolean
is_valid_token(const char *token)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
is_valid_token(const char *token)
is_valid_token (const char *token)

{
int i;

for (i = 0; token[i]; i++)
{
if (!g_ascii_isalnum(token[i]) && token[i] != '_')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is _ a valid token? Doesn't gdbus already provide something like this?

Copy link
Collaborator

@smcv smcv Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is being used in object paths, https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-names https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path is the canonical specification.

gdbus probably doesn't have a function for "is this a valid object-path element?", though, just "is this a valid complete object path?" (which is g_variant_is_object_path()).

Copy link
Collaborator

@smcv smcv Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could validate that the result of g_strdup_printf ("/foo/%s", token) is a valid object path, which I think is what we actually require. That's pretty ugly, but is it less ugly than open-coding validation? I don't know.

g_dbus_is_member_name() is almost right, but member names are not allowed to start with digits, whereas object path elements are, so using that would be unnecessarily restrictive.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is _ a valid token?

If our only functional requirement is "can we append it to /blah/blah/ and get a valid object path?" then yes it is.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!g_ascii_isalnum(token[i]) && token[i] != '_')
if (!g_ascii_isalnum (token[i]) && token[i] != '_')

return FALSE;
}

return TRUE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation would return true for the empty string, but "/blah/blah/" + "" is not a syntactically valid object path (an object path must not end with / unless it is exactly /). So we must check for that.

(We are probably appending it to some string in the /org/freedesktop/... namespace rather than literally /foo/ or /blah/blah/, but it's the "shape" that matters.)

}

static gboolean
xdp_session_initable_init (GInitable *initable,
GCancellable *cancellable,
Expand All @@ -335,6 +349,15 @@ xdp_session_initable_init (GInitable *initable,
return FALSE;
}

if (!is_valid_token(session->token))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!is_valid_token(session->token))
if (!is_valid_token (session->token))

{
g_set_error (error,
XDG_DESKTOP_PORTAL_ERROR,
XDG_DESKTOP_PORTAL_ERROR_INVALID_ARGUMENT,
"Invalid token '%s'", session->token);
return FALSE;
}

id = g_strdup_printf ("/org/freedesktop/portal/desktop/session/%s/%s",
sender_escaped, session->token);

Expand Down
13 changes: 13 additions & 0 deletions tests/test_remotedesktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ def test_remote_desktop_create_close_session(self, portals, dbus_con):
session.close()
xdp.wait_for(lambda: session.closed)

def test_remote_desktop_create_session_invalid(self, portals, dbus_con):
remotedesktop_intf = xdp.get_portal_iface(dbus_con, "RemoteDesktop")

request = xdp.Request(dbus_con, remotedesktop_intf)
options = {"session_handle_token": "Invalid-token&"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can we specifically test "/foo" and "" here, too? Those are special cases would be easy to get wrong.

for badness in ("Invalid-token&", "/foo", ""):
    options = { "session_handle_token": badness }

    with pytest.raises (etc.)
    ...


with pytest.raises(dbus.exceptions.DBusException) as excinfo:
request.call("CreateSession", options=options)
Comment on lines +55 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh, that's nice


e = excinfo.value
assert e.get_dbus_name() == "org.freedesktop.portal.Error.InvalidArgument"
assert "Invalid token" in e.get_dbus_message()

@pytest.mark.parametrize(
"template_params", ({"remotedesktop": {"force-close": 500}},)
)
Expand Down
Loading