Skip to content

FormattedTextHelper: stricter URL parsing#2832

Open
tal5 wants to merge 1 commit into
DenizenScript:devfrom
tal5:link_parsing_fixes
Open

FormattedTextHelper: stricter URL parsing#2832
tal5 wants to merge 1 commit into
DenizenScript:devfrom
tal5:link_parsing_fixes

Conversation

@tal5

@tal5 tal5 commented Jul 2, 2026

Copy link
Copy Markdown
Member

Reported on Discord.

TL;DR Adventure more strictly verifies URLs now which revealed some issues where the parser would end up including invalid characters in the embedded URL.

Can be solved by simply adding more characters to the indexOfAny check, but I decided to try a different approach and use a whitelist of valid characters to find the first non-url-valid character and end the theoretical URL there.
I also added an additional length check after the exact URL end index is found, just as a quick exit in some edge cases.

This also adds a new URI(url) test in a try/catch, as a final verification before adding the URL click event to the component.

Quick test of the logic:

public static void test(String test) {
    int urlEnd = findUrlEndIndex(test.toCharArray(), 0);
    String trimmed = test.substring(0, urlEnd);
    boolean parsed;
    try {
        new URI(trimmed);
        parsed = true;
    }
    catch (URISyntaxException ignored) {
        parsed = false;
    }
    System.out.println("Test URL Parse '" + test + "' End Index '" + urlEnd + "' Final String '" + trimmed + "' " + (parsed ? "Parsed" : "DID NOT PARSE"));
}

static {
    test("https://www.blob.com");
    test("https://www.blob.com{]");
    test("https://www.blob.com\"");
    test("https://www.world.tv?param=_&info=13");
    test("https://www.world.tv?param=_&info=13]");
    test("https://www.world.tv?param=_&info=13\"][");
    test("https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%22name%22");
    test("https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%22name%22\"ge");
    test("https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%2Qname%22");
    test("://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%2Qname%22");
}

Results in:

Test URL Parse 'https://www.blob.com' End Index '20' Final String 'https://www.blob.com' Parsed
Test URL Parse 'https://www.blob.com{]' End Index '20' Final String 'https://www.blob.com' Parsed
Test URL Parse 'https://www.blob.com"' End Index '20' Final String 'https://www.blob.com' Parsed
Test URL Parse 'https://www.world.tv?param=_&info=13' End Index '36' Final String 'https://www.world.tv?param=_&info=13' Parsed
Test URL Parse 'https://www.world.tv?param=_&info=13]' End Index '36' Final String 'https://www.world.tv?param=_&info=13' Parsed
Test URL Parse 'https://www.world.tv?param=_&info=13"][' End Index '36' Final String 'https://www.world.tv?param=_&info=13' Parsed
Test URL Parse 'https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%22name%22' End Index '61' Final String 'https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%22name%22' Parsed
Test URL Parse 'https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%22name%22"ge' End Index '61' Final String 'https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%22name%22' Parsed
Test URL Parse 'https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%2Qname%22' End Index '51' Final String 'https://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=' Parsed
Test URL Parse '://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=%2Qname%22' End Index '46' Final String '://www.blob.com/?numbers=%5B1%2C2%2C3%5D&info=' DID NOT PARSE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant