Skip to content

Commit

Permalink
Merge pull request #3439 from oleg-nenashev/bug/JENKINS-51179
Browse files Browse the repository at this point in the history
[JENKINS-51179] - Provide diagnostics when loading corrupted fingerprint files
  • Loading branch information
oleg-nenashev committed May 15, 2018
2 parents cdda62c + 9226175 commit 9e64bcd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/main/java/hudson/model/Fingerprint.java
Expand Up @@ -1366,7 +1366,12 @@ public Api getApi() {
start = System.currentTimeMillis();

try {
Fingerprint f = (Fingerprint) configFile.read();
Object loaded = configFile.read();
if (!(loaded instanceof Fingerprint)) {
throw new IOException("Unexpected Fingerprint type. Expected " + Fingerprint.class + " or subclass but got "
+ (loaded != null ? loaded.getClass() : "null"));
}
Fingerprint f = (Fingerprint) loaded;
if(logger.isLoggable(Level.FINE))
logger.fine("Loading fingerprint "+file+" took "+(System.currentTimeMillis()-start)+"ms");
if (f.facets==null)
Expand Down
18 changes: 18 additions & 0 deletions test/src/test/java/hudson/model/FingerprintTest.java
Expand Up @@ -23,13 +23,16 @@
*/
package hudson.model;

import hudson.XmlFile;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.AuthorizationMatrixProperty;
import hudson.security.Permission;
import hudson.security.ProjectMatrixAuthorizationStrategy;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.Fingerprinter;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -50,6 +53,7 @@
import org.jvnet.hudson.test.SecuredMockFolder;
import org.jvnet.hudson.test.WorkspaceCopyFileBuilder;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
Expand Down Expand Up @@ -115,6 +119,20 @@ public void shouldCreateUsageLinks() throws Exception {
assertTrue("Usages do not have a reference to " + project, usages.containsKey(project.getName()));
assertTrue("Usages do not have a reference to " + project2, usages.containsKey(project2.getName()));
}

@Test
@Issue("JENKINS-51179")
public void shouldThrowIOExceptionWhenFileIsInvalid() throws Exception {
XmlFile f = new XmlFile(new File(rule.jenkins.getRootDir(), "foo.xml"));
f.write("Hello, world!");
try {
Fingerprint.load(f.getFile());
} catch (IOException ex) {
assertThat(ex.getMessage(), containsString("Unexpected Fingerprint type"));
return;
}
fail("Expected IOException");
}

@Test
@Issue("SECURITY-153")
Expand Down

0 comments on commit 9e64bcd

Please sign in to comment.