xref: /AOO41X/main/svgio/source/svgreader/linkedimagepolicy.hxx (revision a12ae337ec5ceb799fc055816d4155eeb3e45c1a)
1*a12ae337SPeter /**************************************************************
2*a12ae337SPeter  *
3*a12ae337SPeter  * Licensed to the Apache Software Foundation (ASF) under one
4*a12ae337SPeter  * or more contributor license agreements.  See the NOTICE file
5*a12ae337SPeter  * distributed with this work for additional information
6*a12ae337SPeter  * regarding copyright ownership.  The ASF licenses this file
7*a12ae337SPeter  * to you under the Apache License, Version 2.0 (the
8*a12ae337SPeter  * "License"); you may not use this file except in compliance
9*a12ae337SPeter  * with the License.  You may obtain a copy of the License at
10*a12ae337SPeter  *
11*a12ae337SPeter  *   http://www.apache.org/licenses/LICENSE-2.0
12*a12ae337SPeter  *
13*a12ae337SPeter  * Unless required by applicable law or agreed to in writing,
14*a12ae337SPeter  * software distributed under the License is distributed on an
15*a12ae337SPeter  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a12ae337SPeter  * KIND, either express or implied.  See the License for the
17*a12ae337SPeter  * specific language governing permissions and limitations
18*a12ae337SPeter  * under the License.
19*a12ae337SPeter  *
20*a12ae337SPeter  *************************************************************/
21*a12ae337SPeter 
22*a12ae337SPeter #ifndef INCLUDED_SVGIO_LINKEDIMAGEPOLICY_HXX
23*a12ae337SPeter #define INCLUDED_SVGIO_LINKEDIMAGEPOLICY_HXX
24*a12ae337SPeter 
25*a12ae337SPeter #include "sal/config.h"
26*a12ae337SPeter 
27*a12ae337SPeter #include "rtl/uri.hxx"
28*a12ae337SPeter #include "rtl/ustring.hxx"
29*a12ae337SPeter #include "svtools/linkpolicy.hxx"
30*a12ae337SPeter 
31*a12ae337SPeter namespace svgio { namespace svgreader {
32*a12ae337SPeter 
33*a12ae337SPeter /** Resolves an <image> reference against the SVG's own location and decides
34*a12ae337SPeter     whether it may be opened.
35*a12ae337SPeter 
36*a12ae337SPeter     An <image> href is document content, read while the SVG is decomposed. The
37*a12ae337SPeter     reference is only followed when it stays with the SVG: the feature it
38*a12ae337SPeter     exists for is an image delivered alongside the file (a relative href), so
39*a12ae337SPeter     a target that resolves to some other place on the machine, to another
40*a12ae337SPeter     host, or into a different scheme is not part of that feature and is
41*a12ae337SPeter     refused.
42*a12ae337SPeter 
43*a12ae337SPeter     Nothing here needs a document or a service: the decision is made from the
44*a12ae337SPeter     two strings, so it holds the same way on every route that reaches the
45*a12ae337SPeter     decompose step, and for a base URL that is itself remote (a remote base
46*a12ae337SPeter     contains no local files, so every local target falls outside it).
47*a12ae337SPeter 
48*a12ae337SPeter     @param rBase
49*a12ae337SPeter     the SVG's own absolute location, as the document reports it.
50*a12ae337SPeter 
51*a12ae337SPeter     @param rHref
52*a12ae337SPeter     the reference as it was written in the file, relative or absolute.
53*a12ae337SPeter 
54*a12ae337SPeter     @param rAbsoluteUrl
55*a12ae337SPeter     on success, the resolved URL to open; untouched otherwise.
56*a12ae337SPeter 
57*a12ae337SPeter     @return true when the reference may be opened.
58*a12ae337SPeter  */
resolveLinkedImageUrl(::rtl::OUString const & rBase,::rtl::OUString const & rHref,::rtl::OUString & rAbsoluteUrl)59*a12ae337SPeter inline bool resolveLinkedImageUrl(
60*a12ae337SPeter     ::rtl::OUString const & rBase,
61*a12ae337SPeter     ::rtl::OUString const & rHref,
62*a12ae337SPeter     ::rtl::OUString & rAbsoluteUrl )
63*a12ae337SPeter {
64*a12ae337SPeter     if ( rBase.getLength() == 0 || rHref.getLength() == 0 )
65*a12ae337SPeter         return false;
66*a12ae337SPeter 
67*a12ae337SPeter     // A malformed base makes the reference unresolvable. convertRelToAbs
68*a12ae337SPeter     // signals that by throwing, and the throw must not leave the decompose
69*a12ae337SPeter     // call -- it would cross a UNO interface that does not declare it.
70*a12ae337SPeter     ::rtl::OUString aAbsUrl;
71*a12ae337SPeter     try
72*a12ae337SPeter     {
73*a12ae337SPeter         aAbsUrl = ::rtl::Uri::convertRelToAbs( rBase, rHref );
74*a12ae337SPeter     }
75*a12ae337SPeter     catch ( const ::rtl::MalformedUriException & )
76*a12ae337SPeter     {
77*a12ae337SPeter         return false;
78*a12ae337SPeter     }
79*a12ae337SPeter     if ( aAbsUrl.getLength() == 0 )
80*a12ae337SPeter         return false;
81*a12ae337SPeter 
82*a12ae337SPeter     // This is opened as a plain file, so a file: reference on this machine is
83*a12ae337SPeter     // the only thing that can be read here; the test also refuses the schemes
84*a12ae337SPeter     // that address process state and a UNC reference to another host.
85*a12ae337SPeter     if ( !::svt::linkpolicy::isLocalFileUrl( aAbsUrl ) )
86*a12ae337SPeter         return false;
87*a12ae337SPeter 
88*a12ae337SPeter     // ... and only where the SVG itself sits.
89*a12ae337SPeter     if ( !::svt::linkpolicy::isContainedUnder( rBase, aAbsUrl ) )
90*a12ae337SPeter         return false;
91*a12ae337SPeter 
92*a12ae337SPeter     rAbsoluteUrl = aAbsUrl;
93*a12ae337SPeter     return true;
94*a12ae337SPeter }
95*a12ae337SPeter 
96*a12ae337SPeter } }
97*a12ae337SPeter 
98*a12ae337SPeter #endif
99