|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025, CloudBees, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package jenkins.telemetry.impl; |
| 26 | + |
| 27 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 28 | +import hudson.Extension; |
| 29 | +import hudson.ExtensionList; |
| 30 | +import java.time.LocalDate; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Optional; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.TreeSet; |
| 35 | +import java.util.logging.Level; |
| 36 | +import java.util.logging.Logger; |
| 37 | +import java.util.stream.Collectors; |
| 38 | +import jenkins.security.csp.AdvancedConfigurationDescriptor; |
| 39 | +import jenkins.security.csp.Contributor; |
| 40 | +import jenkins.security.csp.CspBuilder; |
| 41 | +import jenkins.security.csp.CspHeader; |
| 42 | +import jenkins.security.csp.CspHeaderDecider; |
| 43 | +import jenkins.security.csp.impl.CspConfiguration; |
| 44 | +import jenkins.telemetry.Telemetry; |
| 45 | +import net.sf.json.JSONObject; |
| 46 | +import org.kohsuke.accmod.Restricted; |
| 47 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
| 48 | + |
| 49 | +/** |
| 50 | + * Collect information about Content Security Policy configuration. |
| 51 | + * |
| 52 | + */ |
| 53 | +@Restricted(NoExternalUse.class) |
| 54 | +@Extension |
| 55 | +public class ContentSecurityPolicy extends Telemetry { |
| 56 | + |
| 57 | + private static final Logger LOGGER = Logger.getLogger(ContentSecurityPolicy.class.getName()); |
| 58 | + |
| 59 | + @NonNull |
| 60 | + @Override |
| 61 | + public String getDisplayName() { |
| 62 | + return "Content Security Policy"; |
| 63 | + } |
| 64 | + |
| 65 | + @NonNull |
| 66 | + @Override |
| 67 | + public LocalDate getStart() { |
| 68 | + return LocalDate.of(2025, 12, 1); |
| 69 | + } |
| 70 | + |
| 71 | + @NonNull |
| 72 | + @Override |
| 73 | + public LocalDate getEnd() { |
| 74 | + return LocalDate.of(2026, 6, 1); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public JSONObject createContent() { |
| 79 | + final JSONObject data = new JSONObject(); |
| 80 | + data.put("enforce", ExtensionList.lookupSingleton(CspConfiguration.class).isEnforce()); |
| 81 | + final Optional<CspHeaderDecider> decider = CspHeaderDecider.getCurrentDecider(); |
| 82 | + data.put("decider", decider.map(Object::getClass).map(Class::getName).orElse(null)); |
| 83 | + data.put("header", decider.map(CspHeaderDecider::decide).filter(Optional::isPresent).map(Optional::get).map(CspHeader::getHeaderName).orElse(null)); |
| 84 | + |
| 85 | + Set<String> contributors = new TreeSet<>(); |
| 86 | + ExtensionList.lookup(Contributor.class).stream().map(Contributor::getClass).map(Class::getName).forEach(contributors::add); |
| 87 | + data.put("contributors", contributors); |
| 88 | + |
| 89 | + Set<String> configurations = new TreeSet<>(); |
| 90 | + ExtensionList.lookup(AdvancedConfigurationDescriptor.class).stream().map(AdvancedConfigurationDescriptor::getClass).map(Class::getName).forEach(configurations::add); |
| 91 | + data.put("configurations", configurations); |
| 92 | + |
| 93 | + try { |
| 94 | + Map<String, Map<String, Integer>> directivesSize = new CspBuilder().withDefaultContributions().getMergedDirectives().stream() |
| 95 | + .map(d -> Map.entry(d.name(), Map.of("entries", d.values().size(), "chars", String.join(" ", d.values()).length()))) |
| 96 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 97 | + data.put("directivesSize", directivesSize); |
| 98 | + } catch (RuntimeException ex) { |
| 99 | + LOGGER.log(Level.FINE, "Error during directive processing", ex); |
| 100 | + } |
| 101 | + |
| 102 | + data.put("components", buildComponentInformation()); |
| 103 | + return data; |
| 104 | + } |
| 105 | +} |
0 commit comments