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

Conversation

whot
Copy link
Contributor

@whot whot commented Jan 10, 2025

The token is used as part of an object path so it has to meet those requirements. We can't escape it since the caller presumably expects to use the token as-is so where it fails the validity simply error out.

Closes: #1549

@whot whot force-pushed the wip/validate-token branch from 3cd75da to ad83903 Compare January 10, 2025 05:42
The token is used as part of an object path so it has to meet those
requirements. We can't escape it since the caller presumably expects to
use the token as-is so where it fails the validity simply error out.

Closes: flatpak#1549
@whot whot force-pushed the wip/validate-token branch from ad83903 to 2d68212 Compare January 10, 2025 05:43
Comment on lines +55 to +56
with pytest.raises(dbus.exceptions.DBusException) as excinfo:
request.call("CreateSession", options=options)
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


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.

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.)


for (i = 0; token[i]; i++)
{
if (!g_ascii_isalnum(token[i]) && token[i] != '_')
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] != '_')

@@ -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)

@@ -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))

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.)
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Needs Triage
3 participants