Skip to content

Commit

Permalink
Ensure scheme not lowercased if URI variable
Browse files Browse the repository at this point in the history
Closes gh-33699
  • Loading branch information
rstoyanchev committed Oct 15, 2024
1 parent a63cf06 commit c261ca3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.web.util;

import java.util.Locale;
import java.util.Set;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -508,7 +509,8 @@ public InternalParser resolveIfOpaque() {
}

public InternalParser captureScheme() {
this.scheme = captureComponent("scheme").toLowerCase();
String scheme = captureComponent("scheme");
this.scheme = (!scheme.startsWith("{") ? scheme.toLowerCase(Locale.ROOT) : scheme);
return this;
}

Expand Down Expand Up @@ -633,8 +635,11 @@ public boolean processCurlyBrackets(char c) {
return true;
}
else if (c == '}') {
this.openCurlyBracketCount--;
return true;
if (this.openCurlyBracketCount > 0) {
this.openCurlyBracketCount--;
return true;
}
return false;
}
return (this.openCurlyBracketCount > 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void setState(State newState) {
logger.trace("Changing state from " + this.state + " to " + newState + " (cur: " + c + ")");
}
this.state = newState;
this.openCurlyBracketCount = 0;
this.openCurlyBracketCount = (this.buffer.toString().equals("{") ? this.openCurlyBracketCount : 0);
}

private boolean processCurlyBrackets(int c) {
Expand All @@ -244,8 +244,11 @@ private boolean processCurlyBrackets(int c) {
return true;
}
if (c == '}') {
this.openCurlyBracketCount--;
return true;
if (this.openCurlyBracketCount > 0) {
this.openCurlyBracketCount--;
return true;
}
return false;
}
return (this.openCurlyBracketCount > 0 && c != EOF);
}
Expand Down Expand Up @@ -756,7 +759,7 @@ private enum State {
public void handle(int c, UrlRecord url, WhatWgUrlParser p) {
// If c is an ASCII alpha, append c, lowercased, to buffer, and set state to scheme state.
if (isAsciiAlpha(c)) {
p.append(Character.toLowerCase((char) c));
p.append(p.openCurlyBracketCount == 0 ? Character.toLowerCase((char) c) : c);
p.setState(SCHEME);
}
// EXTRA: if c is '{', append to buffer and continue as SCHEME
Expand All @@ -782,7 +785,7 @@ else if (p.stateOverride == null) {
public void handle(int c, UrlRecord url, WhatWgUrlParser p) {
// If c is an ASCII alphanumeric, U+002B (+), U+002D (-), or U+002E (.), append c, lowercased, to buffer.
if (isAsciiAlphaNumeric(c) || (c == '+' || c == '-' || c == '.')) {
p.append(Character.toLowerCase((char) c));
p.append(p.openCurlyBracketCount == 0 ? Character.toLowerCase((char) c) : c);
}
// Otherwise, if c is U+003A (:), then:
else if (c == ':') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,16 @@ void buildAndExpandOpaque(ParserType parserType) {
assertThat(result.toUriString()).isEqualTo("mailto:foo@example.com");
}

@ParameterizedTest // gh-33699
@EnumSource(value = ParserType.class)
void schemeVariableMixedCase(ParserType parserType) {
URI uri = UriComponentsBuilder
.fromUriString("{TheScheme}://example.org", parserType)
.buildAndExpand(Map.of("TheScheme", "ws"))
.toUri();
assertThat(uri.toString()).isEqualTo("ws://example.org");
}

@ParameterizedTest
@EnumSource(value = ParserType.class)
void queryParamWithValueWithEquals(ParserType parserType) {
Expand Down

0 comments on commit c261ca3

Please sign in to comment.