Skip to content

Commit

Permalink
Merge pull request #7117 from eclipse/che6mergemaster
Browse files Browse the repository at this point in the history
Merge master to che6
  • Loading branch information
skabashnyuk authored Oct 31, 2017
2 parents 8044dbc + 32b9f4b commit fb62f8f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ If you are interested in fixing issues and contributing directly to the code bas
* **Support:** You can ask questions, report bugs, and request features using [GitHub issues](https://github.com/eclipse/che/issues).
* **Public Chat:** Join the public [eclipse-che](https://mattermost.eclipse.org/eclipse/channels/eclipse-che) Mattermost channel to discuss with community and contributors.
* **Roadmap:** We maintain [the roadmap](https://github.com/eclipse/che/wiki/Roadmap) on the wiki.
* **Weekly Meetings:** Join us on [a hangout](https://github.com/eclipse/che/wiki/Weekly-Planning-Meetings).
* **Weekly Meetings:** Join us in our [Che community meeting](https://github.com/eclipse/che/wiki/Che-Dev-Meetings) every second monday.

### License
Che is open sourced under the Eclipse Public License 1.0.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class URLFactoryBuilder {

/** Default docker image (if repository has no dockerfile) */
protected static final String DEFAULT_DOCKER_IMAGE = "codenvy/ubuntu_jdk8";
protected static final String DEFAULT_DOCKER_IMAGE = "eclipse/ubuntu_jdk8";

/** Default docker type (if repository has no dockerfile) */
protected static final String DEFAULT_MEMORY_LIMIT_BYTES = Long.toString(2000L * 1024L * 1024L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.inject.Singleton;
import javax.validation.constraints.NotNull;
Expand All @@ -40,6 +42,9 @@ public class URLFetcher {
/** Maximum size of allowed data. (30KB) */
protected static final long MAXIMUM_READ_BYTES = 30 * 1000;

/** The Compiled REGEX PATTERN that can be used for http|https git urls */
final Pattern GIT_HTTP_URL_PATTERN = Pattern.compile("(?<sanitized>^http[s]?://.*)\\.git$");

/**
* Fetch the url provided and return its content To prevent DOS attack, limit the amount of the
* collected data
Expand All @@ -50,7 +55,7 @@ public class URLFetcher {
public String fetch(@NotNull final String url) {
requireNonNull(url, "url parameter can't be null");
try {
return fetch(new URL(url).openConnection());
return fetch(new URL(sanitized(url)).openConnection());
} catch (IOException e) {
// we shouldn't fetch if check is done before
LOG.debug("Invalid URL", e);
Expand Down Expand Up @@ -89,4 +94,22 @@ public String fetch(@NotNull URLConnection urlConnection) {
protected long getLimit() {
return MAXIMUM_READ_BYTES;
}

/**
* Simple method to sanitize the Git urls like &quot;https://github.com/demo.git&quot; or
* &quot;http://myowngit.example.com/demo.git&quot;
*
* @param url - the String format of the url
* @return if the url ends with .git will return the url without .git otherwise return the url as
* it is
*/
String sanitized(String url) {
if (url != null) {
final Matcher matcher = GIT_HTTP_URL_PATTERN.matcher(url);
if (matcher.find()) {
return matcher.group("sanitized");
}
}
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public void checkUrlFileIsInvalid() {
assertNull(result);
}

/** Check Sanitizing of Git URL works */
@Test
public void checkDotGitRemovedFromURL() {
String result = URLFetcher.sanitized("https://github.com/acme/demo.git");
assertEquals("https://github.com/acme/demo", result);

result = URLFetcher.sanitized("http://github.com/acme/demo.git");
assertEquals("http://github.com/acme/demo", result);
}

/** Check that when url doesn't exist */
@Test
public void checkMissingContent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

import static com.google.common.collect.Sets.newConcurrentHashSet;
import static java.nio.file.Files.exists;
import static org.eclipse.che.api.vfs.watcher.FileWatcherUtils.toInternalPath;

import com.google.inject.Inject;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.Iterator;
Expand All @@ -23,6 +25,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import javax.inject.Named;
import javax.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -44,8 +47,12 @@ public class FileWatcherByPathMatcher implements Consumer<Path> {
/** Registered path -> Path watch operation IDs */
private final Map<Path, Set<Integer>> pathWatchRegistrations = new ConcurrentHashMap<>();

private final File root;

@Inject
public FileWatcherByPathMatcher(FileWatcherByPathValue watcher) {
public FileWatcherByPathMatcher(
@Named("che.user.workspaces.storage") File root, FileWatcherByPathValue watcher) {
this.root = root;
this.watcher = watcher;
}

Expand All @@ -57,6 +64,7 @@ public void accept(Path path) {
}
paths.values().forEach(it -> it.remove(path));
paths.entrySet().removeIf(it -> it.getValue().isEmpty());
return;
}

for (PathMatcher matcher : matchers.keySet()) {
Expand All @@ -74,6 +82,7 @@ public void accept(Path path) {
watcher.watch(path, operation.create, operation.modify, operation.delete);
pathWatchRegistrations.putIfAbsent(path, newConcurrentHashSet());
pathWatchRegistrations.get(path).add(pathWatcherOperationId);
operation.create.accept(toInternalPath(root.toPath(), path));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.google.common.collect.Sets.newConcurrentHashSet;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.createDirectories;
import static java.nio.file.Files.exists;
import static java.nio.file.Files.isDirectory;
import static java.nio.file.Files.lines;
Expand Down Expand Up @@ -171,14 +172,7 @@ private void startTrackingIgnoreFile() {
fileWatcherManager.addIncludeMatcher(getIgnoreFileMatcher());
fileWatchingOperationID =
fileWatcherManager.registerByMatcher(
getCheDirectoryMatcher(),
getCreateConsumer(),
getModifyConsumer(),
getDeleteConsumer());
}

private PathMatcher getCheDirectoryMatcher() {
return path -> isDirectory(path) && CHE_DIR.equals(path.getFileName().toString());
getIgnoreFileMatcher(), getCreateConsumer(), getModifyConsumer(), getDeleteConsumer());
}

private PathMatcher getIgnoreFileMatcher() {
Expand Down Expand Up @@ -281,6 +275,11 @@ private boolean addExcludesToIgnoreFile(List<String> pathsToExclude) {

private void writeExcludesToIgnoreFile(Path ignoreFilePath, Set<String> locationsToExclude) {
try {
Path cheDir = ignoreFilePath.getParent();
if (!exists(cheDir)) {
createDirectories(cheDir);
}

write(ignoreFilePath, locationsToExclude, UTF_8, CREATE, APPEND);
} catch (IOException e) {
String errorMessage = "Can not add paths to File Watcher excludes ";
Expand Down

0 comments on commit fb62f8f

Please sign in to comment.