-
Notifications
You must be signed in to change notification settings - Fork 1.4k
DNS Provider URL Validation #13821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
DNS Provider URL Validation #13821
Changes from all commits
9714982
1fa1638
6981138
b88552b
1545add
28624df
05b86b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,8 @@ | |
| import com.cloud.user.dao.AccountDao; | ||
| import com.cloud.utils.Pair; | ||
| import com.cloud.utils.StringUtils; | ||
| import com.cloud.utils.UriUtils; | ||
| import com.cloud.utils.net.NetUtils; | ||
| import com.cloud.utils.component.ManagerBase; | ||
| import com.cloud.utils.component.PluggableService; | ||
| import com.cloud.utils.db.Filter; | ||
|
|
@@ -107,9 +109,7 @@ | |
| import com.cloud.vm.Nic; | ||
| import com.cloud.vm.VirtualMachine; | ||
| import com.cloud.vm.VirtualMachineManager; | ||
| import com.cloud.vm.dao.NicDao; | ||
| import com.cloud.vm.dao.NicDetailsDao; | ||
| import com.cloud.vm.dao.UserVmDao; | ||
| import com.cloud.vm.dao.VMInstanceDao; | ||
|
|
||
| @Component | ||
|
|
@@ -126,10 +126,6 @@ | |
| @Inject | ||
| DnsZoneNetworkMapDao dnsZoneNetworkMapDao; | ||
| @Inject | ||
| UserVmDao userVmDao; | ||
| @Inject | ||
| NicDao nicDao; | ||
| @Inject | ||
| DomainDao domainDao; | ||
| @Inject | ||
| DnsZoneJoinDao dnsZoneJoinDao; | ||
|
|
@@ -162,14 +158,41 @@ | |
| throw new CloudRuntimeException("No plugin found for DNS provider type: " + type); | ||
| } | ||
|
|
||
| /** | ||
| * Trims and rejects a DNS provider URL that resolves to an illegal address before any provider client | ||
| * is given the chance to connect to it. See {@link UriUtils#validateUrl(String)} for the exact rules | ||
| * enforced (including the requirement that the URL declares an {@code http}/{@code https} scheme). | ||
| * Private/site-local addresses (e.g. {@code 192.168.0.0/16}) are only permitted for root admin callers. | ||
| * | ||
| * @throws InvalidParameterValueException if the URL is blank, fails validation, or is a private address | ||
| * requested by a non-root-admin caller. | ||
| */ | ||
| private void validateDnsServerUrl(String trimmedUrl, Account caller) { | ||
| if (StringUtils.isBlank(trimmedUrl)) { | ||
|
Check failure on line 171 in server/src/main/java/org/apache/cloudstack/dns/DnsProviderManagerImpl.java
|
||
| throw new InvalidParameterValueException("URL cannot be blank."); | ||
| } | ||
| Pair<String, Integer> hostAndPort; | ||
| try { | ||
| hostAndPort = UriUtils.validateUrl(trimmedUrl); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new InvalidParameterValueException(e.getMessage()); | ||
| } | ||
| if (!accountMgr.isRootAdmin(caller.getId()) && NetUtils.isSiteLocalAddress(hostAndPort.first())) { | ||
| throw new InvalidParameterValueException( | ||
| "Only root admin accounts can configure a DNS server on a private/internal network address."); | ||
| } | ||
|
DaanHoogland marked this conversation as resolved.
Comment on lines
+175
to
+183
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sudo87 this suggestion by Copilot seems to make sense
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @winterhazel sure, will try to address this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @winterhazel There are few options:
We prefer 2 option |
||
| } | ||
|
winterhazel marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| @ActionEvent(eventType = EventTypes.EVENT_DNS_SERVER_ADD, eventDescription = "Adding a DNS Server") | ||
| public DnsServer addDnsServer(AddDnsServerCmd cmd) { | ||
| Account caller = CallContext.current().getCallingAccount(); | ||
| DnsServer existing = dnsServerDao.findByUrlAndAccount(cmd.getUrl(), caller.getId()); | ||
| String dnsUrl = StringUtils.trim(cmd.getUrl()); | ||
|
Check failure on line 190 in server/src/main/java/org/apache/cloudstack/dns/DnsProviderManagerImpl.java
|
||
| validateDnsServerUrl(dnsUrl, caller); | ||
| DnsServer existing = dnsServerDao.findByUrlAndAccount(dnsUrl, caller.getId()); | ||
| if (existing != null) { | ||
| throw new InvalidParameterValueException( | ||
| "This Account already has a DNS server integration for URL: " + cmd.getUrl()); | ||
| "This Account already has a DNS server integration for URL: " + dnsUrl); | ||
| } | ||
|
|
||
| boolean isDnsPublic = cmd.isPublic(); | ||
|
|
@@ -185,7 +208,7 @@ | |
| } | ||
|
|
||
| DnsProviderType type = cmd.getProvider(); | ||
| DnsServerVO server = new DnsServerVO(cmd.getName(), cmd.getUrl(), cmd.getPort(), type, | ||
| DnsServerVO server = new DnsServerVO(cmd.getName(), dnsUrl, cmd.getPort(), type, | ||
| cmd.getDnsUserName(), cmd.getDnsApiKey(), isDnsPublic, publicDomainSuffix, cmd.getNameServers(), | ||
| caller.getAccountId(), caller.getDomainId()); | ||
|
|
||
|
|
@@ -251,12 +274,14 @@ | |
| } | ||
|
|
||
| if (cmd.getUrl() != null) { | ||
| if (!cmd.getUrl().equals(originalUrl)) { | ||
| DnsServer duplicate = dnsServerDao.findByUrlAndAccount(cmd.getUrl(), dnsServer.getAccountId()); | ||
| String dnsUrl = StringUtils.trim(cmd.getUrl()); | ||
|
Check failure on line 277 in server/src/main/java/org/apache/cloudstack/dns/DnsProviderManagerImpl.java
|
||
| if (!dnsUrl.equals(originalUrl)) { | ||
| validateDnsServerUrl(dnsUrl, caller); | ||
| DnsServer duplicate = dnsServerDao.findByUrlAndAccount(dnsUrl, dnsServer.getAccountId()); | ||
| if (duplicate != null && duplicate.getId() != dnsServer.getId()) { | ||
| throw new InvalidParameterValueException("Another DNS server with this URL already exists."); | ||
| } | ||
| dnsServer.setUrl(cmd.getUrl()); | ||
| dnsServer.setUrl(dnsUrl); | ||
| validationRequired = true; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would invert these two because
isSiteLocalAddressis a simpler operation that does not query the database