Skip to content

Commit

Permalink
feat: add dirAllowed jetty configuration flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 27, 2023
1 parent aea5a11 commit 21d8bad
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ else if (Java.isPostJdk9()) {
ctx.getMetaData().addContainerResource(containerResources);
}

ctx.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", Boolean.toString(configuration.isDirAllowed()));
ctx.setParentLoaderPriority(true);
ctx.setWar(webapp);
ctx.setServer(server);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ abstract class AbstractEmbeddedJettyConfiguration extends AbstractConfiguration
*/
private final String webInfJarPattern;

/**
* If true, directory listings are returned if no welcome file is found.
* Else 403 Forbidden.
*/
private final boolean dirAllowed;

AbstractEmbeddedJettyConfiguration(
AbstractEmbeddedJettyConfigurationBuilder<?, ?> builder
) {
Expand All @@ -93,6 +99,7 @@ abstract class AbstractEmbeddedJettyConfiguration extends AbstractConfiguration
this.baseResource = builder.getBaseResource();
this.containerJarPattern = builder.getContainerJarPattern();
this.webInfJarPattern = builder.getWebInfJarPattern();
this.dirAllowed = builder.isDirAllowed();
}

/**
Expand Down Expand Up @@ -140,6 +147,15 @@ public String getWebInfJarPattern() {
return webInfJarPattern;
}

/**
* Get {@link #dirAllowed}
*
* @return {@link #dirAllowed}
*/
public boolean isDirAllowed() {
return dirAllowed;
}

@Override
public boolean equals(Object o) {
if (o == this) {
Expand All @@ -154,7 +170,8 @@ public boolean equals(Object o) {
&& Objects.equals(stopAtShutdown, c.stopAtShutdown)
&& Objects.equals(baseResource, c.baseResource)
&& Objects.equals(containerJarPattern, c.containerJarPattern)
&& Objects.equals(webInfJarPattern, c.webInfJarPattern);
&& Objects.equals(webInfJarPattern, c.webInfJarPattern)
&& Objects.equals(dirAllowed, c.dirAllowed);
}

return false;
Expand All @@ -163,12 +180,13 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
return Objects.hash(
super.hashCode(),
stopTimeout,
stopAtShutdown,
baseResource,
containerJarPattern,
webInfJarPattern
super.hashCode(),
stopTimeout,
stopAtShutdown,
baseResource,
containerJarPattern,
webInfJarPattern,
dirAllowed
);
}

Expand All @@ -186,6 +204,7 @@ public String toString() {
.append("baseResource", baseResource)
.append("containerJarPattern", containerJarPattern)
.append("webInfJarPattern", webInfJarPattern)
.append("dirAllowed", dirAllowed)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,16 @@ abstract class AbstractEmbeddedJettyConfigurationBuilder<
*/
private String webInfJarPattern;

/**
* If true, directory listings are returned if no welcome file is found.
* Else 403 Forbidden.
*/
private boolean dirAllowed;

protected AbstractEmbeddedJettyConfigurationBuilder() {
stopTimeout = DEFAULT_STOP_TIMEOUT;
stopAtShutdown = DEFAULT_STOP_AT_SHUTDOWN;
dirAllowed = true;
}

/**
Expand Down Expand Up @@ -127,6 +134,15 @@ public String getWebInfJarPattern() {
return webInfJarPattern;
}

/**
* Get current {@link #dirAllowed} value.
*
* @return {@link #dirAllowed}
*/
public boolean isDirAllowed() {
return dirAllowed;
}

/**
* Update {@link #stopTimeout} value.
*
Expand Down Expand Up @@ -198,4 +214,15 @@ public SELF withWebInfJarPattern(String webInfJarPattern) {
this.webInfJarPattern = webInfJarPattern;
return self();
}

/**
* Change {@link #dirAllowed} value.
*
* @param dirAllowed Directory listing flag.
* @return this
*/
public SELF withDirAllowed(boolean dirAllowed) {
this.dirAllowed = dirAllowed;
return self();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,12 @@ void it_should_disable_stop_at_shutdown() {
assertThat(result).isSameAs(builder);
assertThat(result.isStopAtShutdown()).isFalse();
}

@Test
void it_should_disallow_dir_listing() {
final EmbeddedJettyConfiguration.Builder result = builder.withDirAllowed(false);

assertThat(result).isSameAs(builder);
assertThat(result.isDirAllowed()).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void it_should_build_default_configuration() {
assertThat(result.getPath()).isEqualTo("/");
assertThat(result.getClasspath()).isEqualTo(".");
assertThat(result.getWebapp()).isEqualTo("src/main/webapp");
assertThat(result.isDirAllowed()).isTrue();
}

@Test
Expand All @@ -57,6 +58,7 @@ void it_should_build_configuration() {
final Resource resource = mock(Resource.class);
final String containerJarPattern = ".*\\.jar";
final String webInfJarPattern = ".*";
final boolean dirAllowed = false;

final EmbeddedJettyConfiguration result = EmbeddedJettyConfiguration.builder()
.withPort(port)
Expand All @@ -68,6 +70,7 @@ void it_should_build_configuration() {
.withBaseResource(resource)
.withContainerJarPattern(containerJarPattern)
.withWebInfJarPattern(webInfJarPattern)
.withDirAllowed(dirAllowed)
.build();

assertThat(result.getPort()).isEqualTo(port);
Expand All @@ -79,6 +82,7 @@ void it_should_build_configuration() {
assertThat(result.getBaseResource()).isSameAs(resource);
assertThat(result.getContainerJarPattern()).isEqualTo(containerJarPattern);
assertThat(result.getWebInfJarPattern()).isEqualTo(webInfJarPattern);
assertThat(result.isDirAllowed()).isEqualTo(dirAllowed);
}

@Test
Expand Down Expand Up @@ -107,7 +111,8 @@ void it_should_implement_to_string() {
"stopAtShutdown: true, " +
"baseResource: null, " +
"containerJarPattern: null, " +
"webInfJarPattern: null" +
"webInfJarPattern: null, " +
"dirAllowed: true" +
"}"
);
}
Expand Down

0 comments on commit 21d8bad

Please sign in to comment.