Skip to content

space_as_plus does not encode plus sign #903

Closed

Description

@yangfl

EDIT: for anyone seeking a quick fix: boost::urls::encode("aA1 +?", boost::urls::unreserved_chars + "whatever your unreserved chars")

With the default option encoding_opts::space_as_plus = true, url::params() decodes plus sign into space, but never encodes it into %2B, leading to surprising (*url.params().find(key)).value != value.

https://compiler-explorer.com/z/n15TqsocE

#include <iostream>
#include <boost/url.hpp>
namespace urls = boost::urls;

int main() {
    {
        urls::url url("https://a/a");

        auto params = url.params();
        params.append({"k", "500+500"});

        for (const auto &param : url.params()) {
            std::cout << param.key << " " << param.value << std::endl;
        }
    }
}

Output: k 500 500

Activity

  1. alandefreitas commented on Apr 11, 2025

    @alandefreitas
    Member

    This looks correct to me. params() uses default encoding opts, append assumes it's receiving input to be encoded, and then it encodes + as whitespace, as defined in the default encoding opts.

    Aren't you expecting params/params_ref/params_view to behave like encoded_params/params_encoded_ref/params_encoded_view? You probably want encoded_params() so the behavior is not surprising/encoded.

    Although I agree, it's surprising that an element of params looks different when appending and retrieving. I'm not sure it's possible to make them always look the same because these URL transformations are not one-to-one or reversible.

  2. yangfl commented on Apr 12, 2025

    @yangfl
    Author

    It does not encode + into whitespace; actually it encodes into %20.

    url.params().append({"k", "a+b c"});
    std::cout << url << std::endl;
    // https://a/a?k=a+b%20c

    The problem is, params() decodes + into whitespace, which is desirable, but somehow leaves + unencoded. If encoded + means decoded whitespace, shouldn't decoded + be mapped to something else?

  3. alandefreitas commented on Apr 17, 2025

    @alandefreitas
    Member

    Yes. That makes sense. I'll have a look at this.

  4. alandefreitas commented on May 8, 2025

    @alandefreitas
    Member

    I've been experimenting with this for a while now. The root of the problem is that this library is based on RFC 3986, but also supports this extra encoding option that's not only not part of the RFC (spaces as plus is an HTTP thing) but ambiguous with its rules. Although the problem is easy to explain ("what represents a literal plus when plus means space?"), it is quite complex to solve for two reasons:

    1) The normalization problem

    The intuition for an initial solution is simple: if + and represent whitespaces (it's even quite common for applications to directly encode "+" as "%20" when it's in the query), then something has to represent a literal + and the obvious choice is %2B. However, when we normalize a URL, its meaning shouldn't change, and normalizing "%2B" in a query gives us "+" because "+" is an unreserved query char and all unreserved chars should be decoded during normalization. But now "%2B" became "+", changing its meaning from "+" to " ".

    For this problem, the obvious intuition would be to just implement an extension to RFC 3986 where normalization would take this new rule into account and not decode "%2B" into "+" when we're encoding space as plus. However, this creates a design problem, because URLs or URL views in the library don't carry information about which encoding options are used by each component. When calling a function such as params(), the library assumes encoding options are just options regarding how the query parameters should be interpreted locally. The library doesn't assume these encoding options are a property of the URL.

    • One solution would be to also extent other functions, such as url_base::normalize() to also accept encoding options and then the user would be responsible for ensuring the same encoding options are being used whenever they call a functions that accepts encoding options. This is unsafe and semantically wrong.

    • A second solution would be to give URLs this property so each URL knows which encoding options it assumes. This opens up a new set of design problems. How would every other function that parses and creates URL need to change? How are URLs with different encoding options compared? Should URLs be enforcing rules that are specific to HTTP URLs? Which encoding options should be the default (the default for params() is space_as_plus as true, which is specific to HTTP)? What happens to existing functions that currently accept encoding options? What happens to existing functions that currently do to accept encoding options? This would be breaking change almost for sure.

    • A third solution would be a breaking change where params() would have space_as_plus = false by default. That means URLs don't use HTTP specific rules that cause ambiguity by default. The normalization problem still remains if the user explicitly decides to interpret the URL in a way that goes beyond the RFC 3986 rules.

    • A fourth option is to only change this behavior for params_ref when you call append because now we're talking about query parameters. So appending "500+500" would append an encoded "500%2B500" and this would be decoded as "500+500" instead of "500 500". In this case, the user would be responsible for not normalizing the query string because normalization would use RFC 3986 rules that would make this ambiguous. This is not so simple to implement because append and many other functions currently use a private URL function edit_params that interacts with other functionality but the behavior is easy to describe. The problem here is we're leaving many problems unsolved and might even be making behavior more complex.

    2) The standardization problem

    Although this might be a smaller problem, whatever behavior we implement needs to be formalized because we don't want to follow our intuition to later find out some other standard actually describes how this should work. As I understand it, this "space-as-plus rule" is mentioned sparsely in the WHATWG standard, which we don't track with this library because it's a "live" standard.

  5. alandefreitas commented on May 9, 2025

    @alandefreitas
    Member

    Here's the spec: https://datatracker.ietf.org/doc/html/rfc1866#section-8.2.1 / https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

    Unfortunately still ambiguous:

    “space characters are replaced by `+’, and then reserved characters are escaped as per [URL];“,

    but "URL" says we must not encode “+”, so “+” could represent both ” ” and “+”.

    My partial conclusion is encoding_opts in Boost.URL shouldn’t exist at all. Now that it exists, we need a breaking change:

    • The default encoding_opts should always be the one that follows the RFC 1866 rules (no space-as-plus by default).
    • params would decode + as when space-as-plus was explicitly asked for
    • params would encode + as %2B when space-as-plus was explicitly asked for
    • These would only affect params locally and attempting to normalize a URL that encodes + as %2B would make it ambiguous.
  6. yangfl commented on May 9, 2025

    @yangfl
    Author

    Agree. But maybe space_as_plus

    True if spaces encode to and from plus signs

    should be clarified as "spaces encode from plus signs only" for now.

  7. added 5 commits that reference this issue on May 14, 2025
    3bc07ca
    89d887d
    246c0a9
    97b5b72
    3b7c26d
  8. added a commit that references this issue on May 15, 2025
    64859a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions