Skip to content

Commit

Permalink
Fix bug to read sub string correctly
Browse files Browse the repository at this point in the history
Motivation:

VariableAwareExpression uses substring to get remaining part of String. It has a bug, it is using the length of the string in second index, instead of using last position + 1 of required substring. Without this fix, expressions like {foo}bar can not be processed.

Changes:

This patch fixes it.
  • Loading branch information
sarankk authored and vietj committed Jan 15, 2025
1 parent 40b2546 commit 9f53fc4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public VariableAwareExpression(String value) {
int openingCurlyBracePos = value.indexOf("{", currentPos);
if (openingCurlyBracePos == -1) {
if (currentPos < value.length()) {
String authorizationPart = value.substring(currentPos, value.length() - currentPos);
String authorizationPart = value.substring(currentPos);
tmpParts.add(ctx -> authorizationPart);
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.vertx.ext.auth.authorization.impl;

import io.vertx.core.MultiMap;
import io.vertx.ext.auth.User;
import io.vertx.ext.auth.authorization.AuthorizationContext;
import org.junit.Test;

import java.util.function.Function;

import static org.junit.Assert.assertEquals;

public class VariableAwareExpressionTest {

@Test
public void test2() {
VariableAwareExpression expression = new VariableAwareExpression("{bar}end");
String resolved = expression.resolve(new MockAuthorizationContext(MultiMap.caseInsensitiveMultiMap().add("bar", "foo")));
Function<AuthorizationContext, String>[] parts = expression.parts();
assertEquals("fooend", resolved);
}

@Test
public void test3() {
VariableAwareExpression expression = new VariableAwareExpression("begin{bar}");
String resolved = expression.resolve(new MockAuthorizationContext(MultiMap.caseInsensitiveMultiMap().add("bar", "foo")));
assertEquals("beginfoo", resolved);
}

@Test
public void test4() {
VariableAwareExpression expression = new VariableAwareExpression("part1,part2{bar}");
String resolved = expression.resolve(new MockAuthorizationContext(MultiMap.caseInsensitiveMultiMap().add("bar", "foo")));
assertEquals("part1,part2foo", resolved);
}

@Test
public void test5() {
VariableAwareExpression expression = new VariableAwareExpression("part1{bar}part2,part3");
String resolved = expression.resolve(new MockAuthorizationContext(MultiMap.caseInsensitiveMultiMap().add("bar", "foo")));
assertEquals("part1foopart2,part3", resolved);
}

private static class MockAuthorizationContext implements AuthorizationContext {

private final MultiMap variables;

public MockAuthorizationContext(MultiMap variables) {
this.variables = variables;
}

@Override
public User user() {
return null;
}

@Override
public MultiMap variables() {
return variables;
}
}
}

0 comments on commit 9f53fc4

Please sign in to comment.