Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41f2afc
commit b140c81
Showing
7 changed files
with
519 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
ninja-core/src/main/java/ninja/AssetsControllerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,51 @@ | |||
/** | |||
* Copyright (C) 2012-2015 the original author or authors. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||
* use this file except in compliance with the License. You may obtain a copy of | |||
* the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||
* License for the specific language governing permissions and limitations under | |||
* the License. | |||
*/ | |||
package ninja; | |||
|
|||
import org.apache.commons.io.FilenameUtils; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
|
|||
import org.apache.commons.lang.StringUtils; | |||
|
|||
public class AssetsControllerHelper { | |||
|
|||
private final static Logger logger = LoggerFactory | |||
.getLogger(AssetsControllerHelper.class); | |||
|
|||
/** | |||
* If we get - for whatever reason - a relative URL like | |||
* assets/../conf/application.conf we expand that to the "real" path. In the | |||
* above case conf/application.conf. | |||
* | |||
* You should then add the assets prefix. | |||
* | |||
* Otherwise someone can create an attack and read all resources of our app. | |||
* If we expand and normalize the incoming path this is no longer possible. | |||
* | |||
* @param fileName A potential "fileName" | |||
* @param enforceUnixSeparator If true it will force the usage of the unix separator '/' | |||
* If false it will use the separator of the underlying system. | |||
* usually '/' in case of unix and '\' in case of windows. | |||
* @return A normalized fileName. | |||
*/ | |||
public String normalizePathWithoutLeadingSlash(String fileName, boolean enforceUnixSeparator) { | |||
String fileNameNormalized = enforceUnixSeparator | |||
? FilenameUtils.normalize(fileName, true) | |||
: FilenameUtils.normalize(fileName); | |||
return StringUtils.removeStart(fileNameNormalized, "/"); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
ninja-core/src/test/java/ninja/AssetsControllerHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,64 @@ | |||
/** | |||
* Copyright (C) 2012-2015 the original author or authors. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||
* use this file except in compliance with the License. You may obtain a copy of | |||
* the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||
* License for the specific language governing permissions and limitations under | |||
* the License. | |||
*/ | |||
package ninja; | |||
|
|||
import static org.junit.Assert.assertEquals; | |||
import static org.powermock.api.mockito.PowerMockito.mockStatic; | |||
import org.apache.commons.io.FilenameUtils; | |||
import org.junit.Before; | |||
|
|||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.mockito.Mockito; | |||
import org.powermock.api.mockito.PowerMockito; | |||
import org.powermock.core.classloader.annotations.PrepareForTest; | |||
import org.powermock.modules.junit4.PowerMockRunner; | |||
|
|||
@RunWith(PowerMockRunner.class) | |||
@PrepareForTest(FilenameUtils.class) | |||
public class AssetsControllerHelperTest { | |||
|
|||
AssetsControllerHelper assetsControllerHelper; | |||
|
|||
@Before | |||
public void setup() { | |||
assetsControllerHelper = new AssetsControllerHelper(); | |||
} | |||
|
|||
@Test | |||
public void testNormalizePathWithoutLeadingSlash() { | |||
assertEquals("dir1/test.test", assetsControllerHelper.normalizePathWithoutLeadingSlash("/dir1/test.test", true)); | |||
assertEquals("dir1/test.test", assetsControllerHelper.normalizePathWithoutLeadingSlash("dir1/test.test", true)); | |||
assertEquals(null, assetsControllerHelper.normalizePathWithoutLeadingSlash("/../test.test", true)); | |||
assertEquals(null, assetsControllerHelper.normalizePathWithoutLeadingSlash("../test.test", true)); | |||
assertEquals("dir2/file.test", assetsControllerHelper.normalizePathWithoutLeadingSlash("/dir1/../dir2/file.test", true)); | |||
assertEquals(null, assetsControllerHelper.normalizePathWithoutLeadingSlash(null, true)); | |||
assertEquals("", assetsControllerHelper.normalizePathWithoutLeadingSlash("", true)); | |||
} | |||
|
|||
@Test | |||
public void testNormalizePathWithoutLeadingSlashCorrectFilnameUtilStaticMethodsCalled() { | |||
PowerMockito.mockStatic(FilenameUtils.class, Mockito.CALLS_REAL_METHODS); | |||
|
|||
assetsControllerHelper.normalizePathWithoutLeadingSlash("/dir1/test.test", false); | |||
PowerMockito.verifyStatic(); | |||
FilenameUtils.normalize("/dir1/test.test"); | |||
|
|||
assetsControllerHelper.normalizePathWithoutLeadingSlash("/dir1/test.test", true); | |||
PowerMockito.verifyStatic(); | |||
FilenameUtils.normalize("/dir1/test.test", true); | |||
} | |||
} |
Oops, something went wrong.