Copilot commented on code in PR #13821:
URL: https://github.com/apache/cloudstack/pull/13821#discussion_r3749433014


##########
server/src/test/java/org/apache/cloudstack/dns/DnsProviderManagerImplTest.java:
##########
@@ -781,18 +781,51 @@ public void testListDnsZones() {
     public void testAddDnsServerAlreadyExists() {
         org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd cmd = mock(
                 
org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd.class);
-        when(cmd.getUrl()).thenReturn("http://newpdns:8081";);
+        when(cmd.getUrl()).thenReturn("http://192.0.2.1:8081";);
         when(dnsServerDao.findByUrlAndAccount(anyString(), 
anyLong())).thenReturn(serverVO);
         manager.addDnsServer(cmd);
     }
 
+    @Test
+    public void testAddDnsServerTrimsUrlBeforeDuplicateCheckAndPersistence() 
throws Exception {
+        org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd cmd = mock(
+                
org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd.class);
+        when(accountMgr.isRootAdmin(callerMock.getId())).thenReturn(true);
+        when(cmd.getUrl()).thenReturn("  http://192.0.2.1:8081  ");
+        when(cmd.getProvider()).thenReturn(DnsProviderType.PowerDNS);
+        when(dnsServerDao.findByUrlAndAccount(anyString(), 
anyLong())).thenReturn(null);
+        
when(dnsProviderMock.validateAndResolveServer(any())).thenReturn("resolved-id");
+        when(dnsServerDao.persist(any())).thenReturn(serverVO);
+
+        manager.addDnsServer(cmd);
+
+        verify(dnsServerDao).findByUrlAndAccount(eq("http://192.0.2.1:8081";), 
anyLong());
+        verify(dnsServerDao).persist(Mockito.argThat(s -> 
"http://192.0.2.1:8081".equals(((DnsServerVO) s).getUrl())));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddDnsServerRejectsLoopbackUrl() {
+        org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd cmd = mock(
+                
org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd.class);
+        when(cmd.getUrl()).thenReturn("http://127.0.0.1:8081";);
+        manager.addDnsServer(cmd);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddDnsServerRejectsUrlWithoutScheme() {
+        org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd cmd = mock(
+                
org.apache.cloudstack.api.command.user.dns.AddDnsServerCmd.class);
+        when(cmd.getUrl()).thenReturn("192.0.2.1:8081");
+        manager.addDnsServer(cmd);
+    }
+

Review Comment:
   URL validation tests cover loopback and missing/unsupported schemes, but 
there’s no test ensuring `file:` URLs are rejected. Given 
`UriUtils.validateUrl` accepts `file`, this is an important security edge case 
to lock down with a unit test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to