Line data Source code
1 : // Copyright (C) 2016 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.mail.send; 16 : 17 : import static org.apache.commons.validator.routines.DomainValidator.ArrayType.GENERIC_PLUS; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.server.config.GerritServerConfig; 21 : import com.google.inject.Inject; 22 : import com.google.inject.Singleton; 23 : import org.apache.commons.validator.routines.DomainValidator; 24 : import org.apache.commons.validator.routines.EmailValidator; 25 : import org.eclipse.jgit.lib.Config; 26 : 27 : /** 28 : * Validator that checks if an email address is valid and allowed for receiving notification emails. 29 : * 30 : * <p>An email address is valid if it is syntactically correct. 31 : * 32 : * <p>An email address is allowed if its top level domain is allowed by Gerrit's configuration. 33 : */ 34 : @Singleton 35 : public class OutgoingEmailValidator { 36 150 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 37 : 38 : @Inject 39 150 : OutgoingEmailValidator(@GerritServerConfig Config config) { 40 150 : String[] allowTLD = config.getStringList("sendemail", null, "allowTLD"); 41 150 : if (allowTLD.length != 0) { 42 : try { 43 1 : DomainValidator.updateTLDOverride(GENERIC_PLUS, allowTLD); 44 0 : } catch (IllegalStateException e) { 45 : // Should only happen in tests, where the OutgoingEmailValidator 46 : // is instantiated repeatedly. 47 0 : logger.atSevere().log("Failed to update TLD override: %s", e.getMessage()); 48 1 : } 49 : } 50 150 : } 51 : 52 : public boolean isValid(String addr) { 53 107 : return EmailValidator.getInstance(true, true).isValid(addr); 54 : } 55 : }