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

KiwiUrls#prependLeadingSlash should accept empty or white space-only strings #1106

Closed
sleberknight opened this issue Mar 5, 2024 · 0 comments · Fixed by #1107
Closed

KiwiUrls#prependLeadingSlash should accept empty or white space-only strings #1106

sleberknight opened this issue Mar 5, 2024 · 0 comments · Fixed by #1107
Assignees
Labels
bug Something isn't working
Milestone

Comments

@sleberknight
Copy link
Member

sleberknight commented Mar 5, 2024

KiwiUrls#prependLeadingSlash throws a StringIndexOutOfBoundsException if it is given an empty string. While an empty string may not be normal, sometimes it makes sense. For example, code that accepts a path representing AWS instance metadata at the well-known base URL http://169.254.169.254/latest/meta-data. This URL returns the top-level metadata, while you can do the following to obtain instance tags:

 curl http://169.254.169.254/latest/meta-data/tags/instance

A utility method might accept a path relative to the base URL, such that you could pass in an empty string to represent the top-level of metadata. So, the method might be defined with a signature like:

public List<InstanceMetadataEntry> getInstanceMetadata(String path) {
    var normalizedPath = isNull(path) ? "/" : KiwiUrls.prependLeadingSlash(path);

    // rest of code to make local HTTP request to http://169.254.169.254/latest/meta-data
    // plus the normalizedPath starting with a /

    return instanceMetadataList;
}

In this code with path is an empty string, we would want KiwiUrls.prependLeadingSlash(path) to return a "/", which would then result in the URL http://169.254.169.254/latest/meta-data/.

What about null input? Currently, a NullPointerException is thrown with the message:

java.lang.NullPointerException: Cannot invoke "String.trim()" because "path" is null

We can extend this by accepting strings that consist only of white space, since it is possible for URLs to contain (encoded) white spaces. However, null should never be considered a valid path in the context of a URL, so I think throwing the NPE is fine, especially now that Java provides a useful message. It probably isn't worth adding an argument check, either, just to throw an IllegalArgumentException, but we could do this later if needed for some reason.

@sleberknight sleberknight added the bug Something isn't working label Mar 5, 2024
@sleberknight sleberknight added this to the 3.3.1 milestone Mar 5, 2024
@sleberknight sleberknight self-assigned this Mar 5, 2024
@sleberknight sleberknight changed the title KiwiUrls#prependLeadingSlash should accept empty strings KiwiUrls#prependLeadingSlash should accept empty or white space-only strings Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant