2424use OCP \ICacheFactory ;
2525use OCP \IRequest ;
2626use OCP \IURLGenerator ;
27+ use OCP \Security \ITrustedDomainHelper ;
2728use OCP \Share \IShare ;
2829use Psr \Container \ContainerExceptionInterface ;
2930use Psr \Container \NotFoundExceptionInterface ;
@@ -43,6 +44,7 @@ public function __construct(
4344 private AppConfig $ appConfig ,
4445 private IRequest $ request ,
4546 private IURLGenerator $ urlGenerator ,
47+ private ITrustedDomainHelper $ trustedDomainHelper ,
4648 ) {
4749 $ this ->cache = $ cacheFactory ->createDistributed ('richdocuments_remote/ ' );
4850 try {
@@ -73,6 +75,11 @@ public function getRemoteCollaboraURL($remote) {
7375 if (!$ this ->isTrustedRemote ($ remote )) {
7476 throw new \Exception ('Unable to determine collabora URL of remote server ' . $ remote . ' - Remote is not a trusted server ' );
7577 }
78+
79+ if ($ this ->trustedDomainHelper ->isTrustedUrl ($ remote )) {
80+ return $ this ->appConfig ->getCollaboraUrlInternal ();
81+ }
82+
7683 $ remoteCollabora = $ this ->cache ->get ('richdocuments_remote/ ' . $ remote );
7784 if ($ remoteCollabora !== null ) {
7885 return $ remoteCollabora ;
@@ -112,7 +119,12 @@ public function isTrustedRemote($domainWithPort) {
112119 if (!is_string ($ trusted )) {
113120 break ;
114121 }
122+
123+ // This regular expression ensures that wildcards for trusted domains
124+ // are parsed properly in order to match subdomains:
125+ // *.example.com => /^[-\.a-zA-Z0-9]*\.example\.com$/i
115126 $ regex = '/^ ' . implode ('[-\.a-zA-Z0-9]* ' , array_map (fn ($ v ) => preg_quote ($ v , '/ ' ), explode ('* ' , $ trusted ))) . '$/i ' ;
127+
116128 if (preg_match ($ regex , $ domain ) || preg_match ($ regex , $ domainWithPort )) {
117129 return true ;
118130 }
0 commit comments