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

Fix for query string parsing in handleEventsEndpoint #1682

Closed
Linch1 opened this issue Jan 16, 2025 · 1 comment
Closed

Fix for query string parsing in handleEventsEndpoint #1682

Linch1 opened this issue Jan 16, 2025 · 1 comment
Labels
Milestone

Comments

@Linch1
Copy link

Linch1 commented Jan 16, 2025

Hello, at line

https://github.com/AsamK/signal-cli/blob/e11e0930209e330649af182eaa0317d2d987d4e8/src/main/java/org/asamk/signal/http/HttpServerHandler.java#L159C13-L160C110

we can see that the getQuery and the Utils.getQueryMap that is used in the next line conflicts, causing the account that are sent, in the query string, for filtering purpose to never be found from the getManagerFromQuery.

List<Manager> managers = getManagerFromQuery(query);

both the formats ?account=+335544777 and ?account=%2B335544777 results after being parsed to someting like

{account: " 335544777"} that starts with a blank space, so an account that starts with a blank space instead that + symbol.

fix: use getRawQuery instead than of getQuery in line 159.

minial example:

import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class ServerSimulation {

    public static void main(String[] args) {
        // Simula una URI di esempio con query parameters
        String uriString = "http://localhost:8080/resource?account=%2B3434523423";
        URI uri = URI.create(uriString);

        // Estrae i query parameters
        String queryString = uri.getQuery();
        Map<String, String> query = queryString == null ? Map.of() : getQueryMap(queryString);

        // Stampa i risultati
        System.out.println("Query Parameters:");
        query.forEach((key, value) -> System.out.println(key + " = " + value));
    }

    
    public static Map<String, String> getQueryMap(String query) {
        var params = query.split("&");
        var map = new HashMap<String, String>();
        for (var param : params) {
            final var paramParts = param.split("=");
            var name = URLDecoder.decode(paramParts[0], StandardCharsets.UTF_8);
            var value = paramParts.length == 1 ? null : URLDecoder.decode(paramParts[1], StandardCharsets.UTF_8);
            map.put(name, value);
        }
        return map;
    }
}

just compile it and run it javac ServerSimulation.java and java ServerSimulation :)
Thanks for the great library.

@AsamK
Copy link
Owner

AsamK commented Jan 17, 2025

Thanks for the hint!

Btw in recent java version you can run it with just java ServerSimulation.java ;)

@AsamK AsamK closed this as completed in a5d2e1e Jan 17, 2025
@AsamK AsamK added the bug label Jan 17, 2025
@AsamK AsamK added this to the next-version milestone Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants