From 9186cf7f1196aacce486558b5b18751b548aea17 Mon Sep 17 00:00:00 2001 From: Jan Vanhercke Date: Sun, 30 Jul 2017 00:03:05 +0200 Subject: Add wrapper class to return a default encoding Unknown encodings may cause gitblit to fail to start. This modification injects a wrapper class in the JGit internal to fake a valid return value. --- .../com/gitblit/manager/RepositoryManager.java | 31 +++++- src/main/java/com/gitblit/utils/MapUtils.java | 116 +++++++++++++++++++++ 2 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/gitblit/utils/MapUtils.java diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index 2be65873..efbdef6a 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -19,9 +19,11 @@ import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -87,6 +89,7 @@ import com.gitblit.utils.CommitCache; import com.gitblit.utils.DeepCopier; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.JGitUtils.LastChange; +import com.gitblit.utils.MapUtils; import com.gitblit.utils.MetricUtils; import com.gitblit.utils.ModelUtils; import com.gitblit.utils.ObjectCache; @@ -1924,6 +1927,7 @@ public class RepositoryManager implements IRepositoryManager { } } + @SuppressWarnings("unchecked") protected void configureJGit() { // Configure JGit WindowCacheConfig cfg = new WindowCacheConfig(); @@ -1944,16 +1948,39 @@ public class RepositoryManager implements IRepositoryManager { } catch (IllegalArgumentException e) { logger.error("Failed to configure JGit parameters!", e); } - + try { // issue-486/ticket-151: UTF-9 & UTF-18 // issue-560/ticket-237: 'UTF8' Field field = RawParseUtils.class.getDeclaredField("encodingAliases"); field.setAccessible(true); - Map encodingAliases = (Map) field.get(null); + + Map encodingAliases; + + try { + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + + // Since unknown encodingAliases cause GitBlit to crash we replace the + // map by a wrapper class that always returns a value. ISO-8859-1 seems + // appropriate since it is a fixed 8-bit encoding. + encodingAliases = (Map) field.get(null); + encodingAliases = MapUtils.defaultMap(encodingAliases, StandardCharsets.ISO_8859_1); + field.set(null, encodingAliases); + + modifiersField.setInt(field, field.getModifiers() | Modifier.FINAL); + modifiersField.setAccessible(false); + } catch(Throwable t) { + logger.error("Failed to inject wrapper class for encoding Aliases", t); + encodingAliases = (Map) field.get(null); + } + + // Provided sensible default mappings encodingAliases.put("'utf8'", RawParseUtils.UTF8_CHARSET); encodingAliases.put("utf-9", RawParseUtils.UTF8_CHARSET); encodingAliases.put("utf-18", RawParseUtils.UTF8_CHARSET); + logger.info("Alias 'UTF8', UTF-9 & UTF-18 encodings as UTF-8 in JGit"); } catch (Throwable t) { logger.error("Failed to inject UTF-9 & UTF-18 encoding aliases into JGit", t); diff --git a/src/main/java/com/gitblit/utils/MapUtils.java b/src/main/java/com/gitblit/utils/MapUtils.java new file mode 100644 index 00000000..98568780 --- /dev/null +++ b/src/main/java/com/gitblit/utils/MapUtils.java @@ -0,0 +1,116 @@ +package com.gitblit.utils; + +import java.text.MessageFormat; +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +import org.apache.mina.util.ConcurrentHashSet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A utility wrapper class to generate a default value over an existing map + * + * @author Jan Vanhercke + * + */ +public class MapUtils { + + private static final Logger logger = LoggerFactory.getLogger(MapUtils.class); + + public static Map defaultMap(Map delegate, V value) { + return new Wrap<>(delegate, value); + } + + private static class Wrap implements Map { + + private Map delegate; + + // HashSet is only used to reduce logging + + private Set unknownKeys = new ConcurrentHashSet<>(); + + private V value; + + private Wrap(Map delegate, V value) { + this.delegate = delegate; + this.value = value; + } + + @Override + public int size() { + return delegate.size(); + } + + @Override + public boolean isEmpty() { + return delegate.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return true; + } + + @Override + public boolean containsValue(Object value) { + return true; + } + + @SuppressWarnings("unchecked") + @Override + public V get(Object key) { + V retv = delegate.get(key); + + if (retv == null) { + if (unknownKeys.add((K) key)) + logger.error(MessageFormat.format("Default value {0} generated for key {1}", value, key)); + + return value; + } + + return retv; + } + + @Override + public V put(K key, V value) { + return delegate.put(key, value); + } + + @Override + public V remove(Object key) { + V retv = delegate.remove(key); + + if (retv == null) + return value; + + return value; + } + + @Override + public void putAll(Map m) { + delegate.putAll(m); + } + + @Override + public void clear() { + delegate.clear(); + } + + @Override + public Set keySet() { + return delegate.keySet(); + } + + @Override + public Collection values() { + return delegate.values(); + } + + @Override + public Set> entrySet() { + return delegate.entrySet(); + } + } +} -- cgit v1.2.3 From aad622a6933a8e334aea8c8b8a644a1b6e39bc61 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Fri, 11 Nov 2022 11:58:23 +0100 Subject: Remove workaround for JGit crashing on 'utf-9' etc Updating JGit fixed the issue that a commit in a repo with an unknown character set throws an exception. This would crash the RepositoryManager. The extra handling, which patches JGit classes during runtime is completely removed. --- .../com/gitblit/manager/RepositoryManager.java | 43 -------- src/main/java/com/gitblit/utils/MapUtils.java | 116 --------------------- 2 files changed, 159 deletions(-) delete mode 100644 src/main/java/com/gitblit/utils/MapUtils.java diff --git a/src/main/java/com/gitblit/manager/RepositoryManager.java b/src/main/java/com/gitblit/manager/RepositoryManager.java index efbdef6a..8d18c811 100644 --- a/src/main/java/com/gitblit/manager/RepositoryManager.java +++ b/src/main/java/com/gitblit/manager/RepositoryManager.java @@ -19,11 +19,8 @@ import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -55,7 +52,6 @@ import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.storage.file.WindowCacheConfig; import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.FileUtils; -import org.eclipse.jgit.util.RawParseUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -89,7 +85,6 @@ import com.gitblit.utils.CommitCache; import com.gitblit.utils.DeepCopier; import com.gitblit.utils.JGitUtils; import com.gitblit.utils.JGitUtils.LastChange; -import com.gitblit.utils.MapUtils; import com.gitblit.utils.MetricUtils; import com.gitblit.utils.ModelUtils; import com.gitblit.utils.ObjectCache; @@ -1927,7 +1922,6 @@ public class RepositoryManager implements IRepositoryManager { } } - @SuppressWarnings("unchecked") protected void configureJGit() { // Configure JGit WindowCacheConfig cfg = new WindowCacheConfig(); @@ -1948,43 +1942,6 @@ public class RepositoryManager implements IRepositoryManager { } catch (IllegalArgumentException e) { logger.error("Failed to configure JGit parameters!", e); } - - try { - // issue-486/ticket-151: UTF-9 & UTF-18 - // issue-560/ticket-237: 'UTF8' - Field field = RawParseUtils.class.getDeclaredField("encodingAliases"); - field.setAccessible(true); - - Map encodingAliases; - - try { - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); - - // Since unknown encodingAliases cause GitBlit to crash we replace the - // map by a wrapper class that always returns a value. ISO-8859-1 seems - // appropriate since it is a fixed 8-bit encoding. - encodingAliases = (Map) field.get(null); - encodingAliases = MapUtils.defaultMap(encodingAliases, StandardCharsets.ISO_8859_1); - field.set(null, encodingAliases); - - modifiersField.setInt(field, field.getModifiers() | Modifier.FINAL); - modifiersField.setAccessible(false); - } catch(Throwable t) { - logger.error("Failed to inject wrapper class for encoding Aliases", t); - encodingAliases = (Map) field.get(null); - } - - // Provided sensible default mappings - encodingAliases.put("'utf8'", RawParseUtils.UTF8_CHARSET); - encodingAliases.put("utf-9", RawParseUtils.UTF8_CHARSET); - encodingAliases.put("utf-18", RawParseUtils.UTF8_CHARSET); - - logger.info("Alias 'UTF8', UTF-9 & UTF-18 encodings as UTF-8 in JGit"); - } catch (Throwable t) { - logger.error("Failed to inject UTF-9 & UTF-18 encoding aliases into JGit", t); - } } protected void configureCommitCache() { diff --git a/src/main/java/com/gitblit/utils/MapUtils.java b/src/main/java/com/gitblit/utils/MapUtils.java deleted file mode 100644 index 98568780..00000000 --- a/src/main/java/com/gitblit/utils/MapUtils.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.gitblit.utils; - -import java.text.MessageFormat; -import java.util.Collection; -import java.util.Map; -import java.util.Set; - -import org.apache.mina.util.ConcurrentHashSet; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * A utility wrapper class to generate a default value over an existing map - * - * @author Jan Vanhercke - * - */ -public class MapUtils { - - private static final Logger logger = LoggerFactory.getLogger(MapUtils.class); - - public static Map defaultMap(Map delegate, V value) { - return new Wrap<>(delegate, value); - } - - private static class Wrap implements Map { - - private Map delegate; - - // HashSet is only used to reduce logging - - private Set unknownKeys = new ConcurrentHashSet<>(); - - private V value; - - private Wrap(Map delegate, V value) { - this.delegate = delegate; - this.value = value; - } - - @Override - public int size() { - return delegate.size(); - } - - @Override - public boolean isEmpty() { - return delegate.isEmpty(); - } - - @Override - public boolean containsKey(Object key) { - return true; - } - - @Override - public boolean containsValue(Object value) { - return true; - } - - @SuppressWarnings("unchecked") - @Override - public V get(Object key) { - V retv = delegate.get(key); - - if (retv == null) { - if (unknownKeys.add((K) key)) - logger.error(MessageFormat.format("Default value {0} generated for key {1}", value, key)); - - return value; - } - - return retv; - } - - @Override - public V put(K key, V value) { - return delegate.put(key, value); - } - - @Override - public V remove(Object key) { - V retv = delegate.remove(key); - - if (retv == null) - return value; - - return value; - } - - @Override - public void putAll(Map m) { - delegate.putAll(m); - } - - @Override - public void clear() { - delegate.clear(); - } - - @Override - public Set keySet() { - return delegate.keySet(); - } - - @Override - public Collection values() { - return delegate.values(); - } - - @Override - public Set> entrySet() { - return delegate.entrySet(); - } - } -} -- cgit v1.2.3 From d9933399a64be0ae30b8ea63d568b9092b850386 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Fri, 11 Nov 2022 12:08:01 +0100 Subject: deps: Update JGit to version 4.8.0.201706111038-r --- .classpath | 11 ++++++++--- build.moxie | 2 +- gitblit.iml | 18 +++++++++--------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.classpath b/.classpath index 434e5a40..fff569ae 100644 --- a/.classpath +++ b/.classpath @@ -58,14 +58,14 @@ - - + + - + @@ -114,5 +114,10 @@ + + + + + diff --git a/build.moxie b/build.moxie index 84edf30a..cb4ddcc5 100644 --- a/build.moxie +++ b/build.moxie @@ -109,7 +109,7 @@ properties: { slf4j.version : 1.7.29 wicket.version : 1.4.22 lucene.version : 5.5.2 - jgit.version : 4.6.1.201703071140-r + jgit.version : 4.8.0.201706111038-r groovy.version : 2.4.4 bouncycastle.version : 1.69 selenium.version : 2.28.0 diff --git a/gitblit.iml b/gitblit.iml index 4d842d49..eebba6a7 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -583,24 +583,24 @@ - + - + - + - + - + - + @@ -660,13 +660,13 @@ - + - + - + -- cgit v1.2.3 From d00bdf2feed0f0a2eefe437ab9dc1561f0049002 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Fri, 11 Nov 2022 12:12:13 +0100 Subject: bugtraq: Fallback to UTF-8 if commit encoding is unsupported Reading the encoding of a commit can result in a Unsupported- or IllegalCharsetException. This happens when for whatever reason the commit has an encoding recorded that the system doesn't understand. Instead of completely failing, fallback to UTF-8. --- src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java index bce0bd8a..ca0b317f 100644 --- a/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java +++ b/src/main/bugtraq/com/syntevo/bugtraq/BugtraqConfig.java @@ -31,6 +31,9 @@ package com.syntevo.bugtraq; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; +import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -55,6 +58,8 @@ import org.eclipse.jgit.treewalk.filter.PathFilterGroup; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static java.nio.charset.StandardCharsets.UTF_8; + public final class BugtraqConfig { // Constants ============================================================== @@ -229,7 +234,7 @@ public final class BugtraqConfig { FileMode entmode = tw.getFileMode(0); if (FileMode.REGULAR_FILE == entmode) { ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB); - content = new String(ldr.getCachedBytes(), commit.getEncoding()); + content = new String(ldr.getCachedBytes(), guessEncoding(commit)); break; } } @@ -265,6 +270,15 @@ public final class BugtraqConfig { return baseConfig; } + @NotNull + private static Charset guessEncoding(RevCommit commit) { + try { + return commit.getEncoding(); + } catch (IllegalCharsetNameException | UnsupportedCharsetException e) { + return UTF_8; + } + } + @Nullable private static String getString(@Nullable String subsection, @NotNull String key, @NotNull Config config, @Nullable Config baseConfig) { final String value = config.getString(BUGTRAQ, subsection, key); -- cgit v1.2.3 From 30abff544589292fbead49d2b04a96d604938eb0 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Fri, 11 Nov 2022 12:14:55 +0100 Subject: bugtraq: Catch exceptions from bugtraq and show message Catch all exceptions, and not just IOExceptions, from bugtraq formatter. If an exception is caught, ignore the bugtraq handling of the commit message and show the plain message. Way better then not showing anything just because something broke in bugtraq. --- src/main/java/com/gitblit/utils/BugtraqProcessor.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/gitblit/utils/BugtraqProcessor.java b/src/main/java/com/gitblit/utils/BugtraqProcessor.java index 67f220f6..8ecf948a 100644 --- a/src/main/java/com/gitblit/utils/BugtraqProcessor.java +++ b/src/main/java/com/gitblit/utils/BugtraqProcessor.java @@ -155,10 +155,10 @@ public class BugtraqProcessor { formatter.formatLogMessage(text, new BugtraqOutputHandler(sb)); text = sb.toString(); } - } catch (IOException e) { - logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e); } catch (ConfigInvalidException e) { - logger.error(MessageFormat.format("Bugtraq config for {0} is invalid!", repositoryName), e); + logger.warn("Bugtraq config for {} is invalid!", repositoryName, e); + } catch (Exception e) { + logger.warn("Failed to parse message through Bugtraq.", e); } return text; -- cgit v1.2.3 From 3198339587e7c3056d022ecff2d271b576953f2e Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Fri, 11 Nov 2022 12:56:47 +0100 Subject: deps: Update JGit to 4.11.9.201909030838-r and other dependencies Update JGit, and also update other dependencies where the 4.11 JGit version uses newer versions than we do: commond-codec updated to 1.9 commons-compress updated to 1.15 gson updated to 2.8.2 --- .classpath | 20 ++++++++-------- build.moxie | 8 +++---- gitblit.iml | 76 ++++++++++++++++++++++++++++++------------------------------- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/.classpath b/.classpath index fff569ae..c39b210a 100644 --- a/.classpath +++ b/.classpath @@ -58,14 +58,15 @@ - + + - - - - - + + + + + @@ -75,13 +76,13 @@ - + - - + + @@ -112,7 +113,6 @@ - diff --git a/build.moxie b/build.moxie index cb4ddcc5..dd26d3f6 100644 --- a/build.moxie +++ b/build.moxie @@ -109,7 +109,7 @@ properties: { slf4j.version : 1.7.29 wicket.version : 1.4.22 lucene.version : 5.5.2 - jgit.version : 4.8.0.201706111038-r + jgit.version : 4.11.9.201909030838-r groovy.version : 2.4.4 bouncycastle.version : 1.69 selenium.version : 2.28.0 @@ -175,19 +175,19 @@ dependencies: - compile 'org.apache.sshd:sshd-core:${sshd.version}' :war !org.easymock - compile 'org.apache.mina:mina-core:${mina.version}' :war !org.easymock - compile 'rome:rome:0.9' :war :manager :api -- compile 'com.google.code.gson:gson:2.3.1' :war :fedclient :manager :api +- compile 'com.google.code.gson:gson:2.8.2' :war :fedclient :manager :api - compile 'org.codehaus.groovy:groovy-all:${groovy.version}' :war - compile 'com.unboundid:unboundid-ldapsdk:2.3.8' :war - compile 'org.apache.ivy:ivy:2.2.0' :war - compile 'com.toedter:jcalendar:1.3.2' :authority -- compile 'org.apache.commons:commons-compress:1.4.1' :war +- compile 'org.apache.commons:commons-compress:1.15' :war - compile 'commons-io:commons-io:2.2' :war - compile 'com.force.api:force-partner-api:24.0.0' :war - compile 'org.freemarker:freemarker:2.3.22' :war - compile 'com.github.dblock.waffle:waffle-jna:1.7.3' :war - compile 'org.kohsuke:libpam4j:1.8' :war - compile 'args4j:args4j:2.0.29' :war :fedclient -- compile 'commons-codec:commons-codec:1.7' :war +- compile 'commons-codec:commons-codec:1.9' :war - compile 'redis.clients:jedis:2.6.2' :war - compile 'ro.fortsoft.pf4j:pf4j:0.9.0' :war - compile 'org.apache.tika:tika-core:1.5' :war diff --git a/gitblit.iml b/gitblit.iml index eebba6a7..2f1bfb32 100644 --- a/gitblit.iml +++ b/gitblit.iml @@ -583,13 +583,13 @@ - + - + - + @@ -604,6 +604,17 @@ + + + + + + + + + + + @@ -616,57 +627,57 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -770,13 +781,13 @@ - + - + - + @@ -823,24 +834,24 @@ - + - + - + - + - + - + @@ -1174,17 +1185,6 @@ - - - - - - - - - - - -- cgit v1.2.3