Skip to content

Commit

Permalink
Fix the problem of being able to configure invalid external URL
Browse files Browse the repository at this point in the history
Signed-off-by: JohnNiang <[email protected]>
  • Loading branch information
JohnNiang committed Oct 12, 2024
1 parent 9468e87 commit 442e3aa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.HashSet;
Expand Down Expand Up @@ -69,9 +70,26 @@ public boolean supports(Class<?> clazz) {
@Override
public void validate(Object target, Errors errors) {
var props = (HaloProperties) target;
if (props.isUseAbsolutePermalink() && props.getExternalUrl() == null) {
var externalUrl = props.getExternalUrl();
if (props.isUseAbsolutePermalink() && externalUrl == null) {
errors.rejectValue("externalUrl", "external-url.required.when-using-absolute-permalink",
"External URL is required when property `use-absolute-permalink` is set to true.");
}
// check if the external URL is a http or https URL and is not an opaque URL.
if (externalUrl != null && !isValidExternalUrl(externalUrl)) {
errors.rejectValue("externalUrl", "external-url.invalid-format",
"External URL must be a http or https URL.");
}
}

private boolean isValidExternalUrl(URL externalUrl) {
try {
var uri = externalUrl.toURI();
return !uri.isOpaque()
&& uri.getAuthority() != null
&& Set.of("http", "https").contains(uri.getScheme());
} catch (URISyntaxException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -34,4 +35,10 @@ class TestController {

}


@Test
void urlTest() {
URI uri = URI.create("https:///path");
System.out.println(uri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package run.halo.app.infra.properties;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.validation.SimpleErrors;

class HaloPropertiesTest {

static Stream<Arguments> validateTest() throws MalformedURLException {
return Stream.of(
Arguments.of(true, new URL("http://localhost:8080"), true),
Arguments.of(false, new URL("http://localhost:8080"), true),
Arguments.of(true, new URL("https://localhost:8080"), true),
Arguments.of(false, new URL("https://localhost:8080"), true),
Arguments.of(true, new URL("ftp://localhost:8080"), false),
Arguments.of(false, new URL("ftp://localhost:8080"), false),
Arguments.of(true, new URL("http:www/halo/run"), false),
Arguments.of(false, new URL("http:www/halo.run"), false),
Arguments.of(true, new URL("https:www/halo/run"), false),
Arguments.of(false, new URL("https:www/halo/run"), false),
Arguments.of(true, new URL("https:///path"), false),
Arguments.of(false, new URL("https:///path"), false),
Arguments.of(true, new URL("http:///path"), false),
Arguments.of(false, new URL("http:///path"), false),
Arguments.of(true, null, false),
Arguments.of(false, null, true)
);
}

@ParameterizedTest
@MethodSource
void validateTest(boolean useAbsolutePermalink, URL externalUrl, boolean valid) {
var properties = new HaloProperties();
properties.setUseAbsolutePermalink(useAbsolutePermalink);
properties.setExternalUrl(externalUrl);
var errors = new SimpleErrors(properties);
properties.validate(properties, errors);
Assertions.assertEquals(valid, !errors.hasErrors());
}
}

0 comments on commit 442e3aa

Please sign in to comment.