Skip to content

Commit f5be3f9

Browse files
authored
Merge pull request #1293 from ruhan1/indy-1.8.x
Port fixes from master
2 parents 60e4c70 + 1b118c9 commit f5be3f9

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

addons/changelog/common/src/main/java/org/commonjava/indy/changelog/DiffUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public static String diffPatch( final String fileName, final String changed, fin
4646
DIFF_PATCH_CONTEXT_LINES );
4747
StringBuilder builder = new StringBuilder();
4848
patchDiff.forEach( ps -> builder.append( ps ).append( LINE_BREAK ) );
49-
builder.deleteCharAt( builder.lastIndexOf( LINE_BREAK ) );
49+
if ( builder.lastIndexOf( LINE_BREAK ) > 0 )
50+
{
51+
builder.deleteCharAt( builder.lastIndexOf( LINE_BREAK ) );
52+
}
5053
return builder.toString();
5154
}
5255

core/src/main/java/org/commonjava/indy/core/ctl/ContentController.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
import javax.enterprise.context.ApplicationScoped;
4545
import javax.inject.Inject;
46-
import javax.ws.rs.core.MediaType;
4746
import java.io.BufferedReader;
4847
import java.io.ByteArrayInputStream;
4948
import java.io.IOException;
@@ -57,11 +56,12 @@
5756
import java.util.Map;
5857
import java.util.Set;
5958
import java.util.TreeMap;
59+
import java.util.regex.Pattern;
6060

6161
import static org.apache.commons.io.IOUtils.closeQuietly;
62-
import static org.apache.commons.lang.StringUtils.isNotBlank;
6362
import static org.commonjava.maven.galley.util.PathUtils.normalize;
6463
import static org.commonjava.maven.galley.util.PathUtils.parentPath;
64+
import static org.jsoup.helper.StringUtil.isBlank;
6565

6666
@ApplicationScoped
6767
public class ContentController
@@ -82,6 +82,8 @@ public class ContentController
8282

8383
public static final String HTML_TAG_PATTERN = ".*\\<(!DOCTYPE|[-_.a-zA-Z0-9]+).*";
8484

85+
public static final Pattern PATH_PATTERN = Pattern.compile( "^([-\\w:@&?=+,.!/~*'%$_;\\(\\)]*)?$");
86+
8587
private static final int MAX_PEEK_BYTES = 16384;
8688

8789
@Inject
@@ -192,16 +194,27 @@ public Transfer store( final StoreKey key, final String path, final InputStream
192194
return contentManager.store( store, path, stream, TransferOperation.UPLOAD, eventMetadata );
193195
}
194196

195-
private void validatePath( final StoreKey key, final String path )
196-
throws IndyWorkflowException
197+
private void validatePath( final StoreKey key, final String path ) throws IndyWorkflowException
197198
{
198-
if ( isNotBlank( path ) && ( path.contains( "{" ) || path.contains( "}" ) || path.contains( "@@" )
199-
|| path.contains( "%" ) ) )
199+
if ( !isValidPath( path ) )
200200
{
201201
throw new IndyWorkflowException( 400, "Invalid path: %s (target repo: %s)", path, key );
202202
}
203203
}
204204

205+
boolean isValidPath( String path )
206+
{
207+
if ( isBlank( path ) )
208+
{
209+
return false;
210+
}
211+
if ( !PATH_PATTERN.matcher( path ).matches() )
212+
{
213+
return false;
214+
}
215+
return true;
216+
}
217+
205218
public void rescan( final StoreKey key )
206219
throws IndyWorkflowException
207220
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.core.ctl;
17+
18+
import org.junit.Test;
19+
20+
import static junit.framework.TestCase.assertTrue;
21+
import static org.junit.Assert.assertFalse;
22+
23+
public class ControllerTest
24+
{
25+
@Test
26+
public void validatePath()
27+
{
28+
ContentController controller = new ContentController();
29+
30+
assertTrue( controller.isValidPath( "/foo/bar" ) );
31+
assertTrue( controller.isValidPath( "/@babel%2fparser" ) );
32+
assertTrue( controller.isValidPath( "@babel%2fparser" ) );
33+
assertTrue( controller.isValidPath( "/a-zA-Z0-9.-_~!$&'()*+,;=:@" ) );
34+
35+
assertFalse( controller.isValidPath( "" ) );
36+
assertFalse( controller.isValidPath( null ) );
37+
}
38+
39+
}

0 commit comments

Comments
 (0)