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 ;
@@ -91,17 +98,20 @@ public function getRemoteCollaboraURL($remote) {
9198 return '' ;
9299 }
93100
94- public function isTrustedRemote ($ domainWithPort ) {
95- if (str_starts_with ($ domainWithPort , 'http:// ' ) || str_starts_with ($ domainWithPort , 'https:// ' )) {
96- $ port = parse_url ($ domainWithPort , PHP_URL_PORT );
97- $ domainWithPort = parse_url ($ domainWithPort , PHP_URL_HOST ) . ($ port ? ': ' . $ port : '' );
101+ public function isTrustedRemote ($ domain ) {
102+ if (str_starts_with ($ domain , 'http:// ' ) || str_starts_with ($ domain , 'https:// ' )) {
103+ $ parsedDomain = parse_url ($ domain );
104+
105+ $ fullDomain = $ parsedDomain ['host ' ];
106+ $ fullDomain = $ fullDomain . ($ parsedDomain ['port ' ] ? ': ' . $ parsedDomain ['port ' ] : '' );
107+ $ fullDomain = $ fullDomain . ($ parsedDomain ['path ' ] ?: '' );
98108 }
99109
100- if ($ this ->appConfig ->isTrustedDomainAllowedForFederation () && $ this ->trustedServers !== null && $ this ->trustedServers ->isTrustedServer ($ domainWithPort )) {
110+ if ($ this ->appConfig ->isTrustedDomainAllowedForFederation () && $ this ->trustedServers !== null && $ this ->trustedServers ->isTrustedServer ($ fullDomain )) {
101111 return true ;
102112 }
103113
104- $ domain = $ this ->getDomainWithoutPort ($ domainWithPort );
114+ $ domain = $ this ->getDomainWithoutPort ($ fullDomain );
105115
106116 $ trustedList = array_merge ($ this ->appConfig ->getGlobalScaleTrustedHosts (), [$ this ->request ->getServerHost ()]);
107117 if (!is_array ($ trustedList )) {
@@ -112,8 +122,13 @@ public function isTrustedRemote($domainWithPort) {
112122 if (!is_string ($ trusted )) {
113123 break ;
114124 }
125+
126+ // This regular expression ensures that wildcards for trusted domains
127+ // are parsed properly in order to match subdomains:
128+ // *.example.com => /^[-\.a-zA-Z0-9]*\.example\.com$/i
115129 $ regex = '/^ ' . implode ('[-\.a-zA-Z0-9]* ' , array_map (fn ($ v ) => preg_quote ($ v , '/ ' ), explode ('* ' , $ trusted ))) . '$/i ' ;
116- if (preg_match ($ regex , $ domain ) || preg_match ($ regex , $ domainWithPort )) {
130+
131+ if (preg_match ($ regex , $ domain ) || preg_match ($ regex , $ fullDomain )) {
117132 return true ;
118133 }
119134 }
0 commit comments