Skip to content

Commit 3c8c216

Browse files
easelAndré Behrens
authored andcommitted
Strip trailing . from DNS lookups when converting to autodiscover url
- Factored out dns response checks - Added test case ensuring validity checks are correct - Fixes OfficeDev#418
1 parent 79874ea commit 3c8c216

2 files changed

Lines changed: 94 additions & 16 deletions

File tree

src/main/java/microsoft/exchange/webservices/data/autodiscover/AutodiscoverDnsClient.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
package microsoft.exchange.webservices.data.autodiscover;
2525

2626
import microsoft.exchange.webservices.data.core.EwsUtilities;
27-
import microsoft.exchange.webservices.data.dns.DnsClient;
28-
import microsoft.exchange.webservices.data.dns.DnsSrvRecord;
2927
import microsoft.exchange.webservices.data.core.enumeration.misc.TraceFlags;
3028
import microsoft.exchange.webservices.data.core.exception.dns.DnsException;
29+
import microsoft.exchange.webservices.data.dns.DnsClient;
30+
import microsoft.exchange.webservices.data.dns.DnsSrvRecord;
3131

3232
import javax.xml.stream.XMLStreamException;
3333

@@ -70,12 +70,28 @@ protected AutodiscoverDnsClient(AutodiscoverService service) {
7070
this.service = service;
7171
}
7272

73+
/**
74+
* Extracts a valid autodiscover hostname, if any, from a dns srv response.
75+
*
76+
* @param dnsNameTarget The hostname response returned by DNS
77+
* @return Autodiscover hostname (will be null if dnsNameTarget is invalid).
78+
*/
79+
protected static String extractHostnameFromDnsSrv(String dnsNameTarget) {
80+
if (dnsNameTarget == null || dnsNameTarget.isEmpty()) {
81+
return null;
82+
} else {
83+
if (dnsNameTarget.endsWith(".")) {
84+
dnsNameTarget = dnsNameTarget.substring(0, dnsNameTarget.length()-1);
85+
}
86+
return dnsNameTarget;
87+
}
88+
}
89+
7390
/**
7491
* Finds the Autodiscover host from DNS SRV records.
7592
*
7693
* @param domain the domain
7794
* @return Autodiscover hostname (will be null if lookup failed).
78-
* @throws XMLStreamException the XML stream exception
7995
* @throws IOException signals that an I/O exception has occurred.
8096
*/
8197
protected String findAutodiscoverHostFromSrv(String domain)
@@ -84,20 +100,17 @@ protected String findAutodiscoverHostFromSrv(String domain)
84100

85101
DnsSrvRecord dnsSrvRecord = this
86102
.findBestMatchingSrvRecord(domainToMatch);
87-
88-
if ((dnsSrvRecord == null) || dnsSrvRecord.getNameTarget() == null ||
89-
dnsSrvRecord.getNameTarget().isEmpty()) {
90-
this.service.traceMessage(TraceFlags.AutodiscoverConfiguration,
91-
"No appropriate SRV record was found.");
92-
return null;
93-
} else {
94-
this.service.traceMessage(TraceFlags.AutodiscoverConfiguration,
95-
String.format(
96-
"DNS query for SRV record for domain %s found %s",
97-
domain, dnsSrvRecord.getNameTarget()));
98-
99-
return dnsSrvRecord.getNameTarget();
103+
if (dnsSrvRecord != null) {
104+
String hostName = extractHostnameFromDnsSrv(dnsSrvRecord.getNameTarget());
105+
if (hostName != null) {
106+
this.service.traceMessage(TraceFlags.AutodiscoverConfiguration, String
107+
.format("DNS query for SRV record for domain %s found %s", domain, hostName));
108+
return hostName;
109+
}
100110
}
111+
this.service.traceMessage(TraceFlags.AutodiscoverConfiguration,
112+
"No appropriate SRV record was found.");
113+
return null;
101114
}
102115

103116
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* The MIT License
3+
* Copyright (c) 2012 Microsoft Corporation
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package microsoft.exchange.webservices.data.autodiscover;
25+
26+
import static org.junit.Assert.assertEquals;
27+
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
public class AutodiscoverDnsClientTest {
34+
35+
final String validResponse = "autodiscover.contoso.com";
36+
final String trailingDotResponse = "autodiscover.contoso.com.";
37+
38+
/**
39+
* If DNS gives us a null, we should return a null
40+
*/
41+
@Test public void textExtractNullHostnameFromDnsSrv() {
42+
assertEquals(AutodiscoverDnsClient.extractHostnameFromDnsSrv(null), null);
43+
}
44+
45+
/**
46+
* If DNS gives us back an empty string, we should return a null
47+
*/
48+
@Test public void textExtractEmptyHostnameFromDnsSrv() {
49+
assertEquals(AutodiscoverDnsClient.extractHostnameFromDnsSrv(""), null);
50+
}
51+
52+
/**
53+
* If DNS gives us back a plain domain, we should pass it through.
54+
*/
55+
@Test public void textExtractValidHostnameFromDnsSrv() {
56+
assertEquals(AutodiscoverDnsClient.extractHostnameFromDnsSrv(validResponse), validResponse);
57+
}
58+
59+
/**
60+
* If DNS gives us back a domain with a trailing dot, we should strip it.
61+
*/
62+
@Test public void textExtractTrailingDotHostnameFromDnsSrv() {
63+
assertEquals(AutodiscoverDnsClient.extractHostnameFromDnsSrv(trailingDotResponse), validResponse);
64+
}
65+
}

0 commit comments

Comments
 (0)