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