• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_STRINGS_ESCAPE_H_
6 #define BASE_STRINGS_ESCAPE_H_
7 
8 #include <stdint.h>
9 
10 #include <set>
11 #include <string>
12 
13 #include "base/base_export.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/utf_offset_string_conversions.h"
16 
17 namespace base {
18 
19 // Escaping --------------------------------------------------------------------
20 
21 // Escapes all characters except unreserved characters. Unreserved characters,
22 // as defined in RFC 3986, include alphanumerics and -._~
23 BASE_EXPORT std::string EscapeAllExceptUnreserved(StringPiece text);
24 
25 // Escapes characters in text suitable for use as a query parameter value.
26 // We %XX everything except alphanumerics and -_.!~*'()
27 // Spaces change to "+" unless you pass usePlus=false.
28 // This is basically the same as encodeURIComponent in javascript.
29 BASE_EXPORT std::string EscapeQueryParamValue(StringPiece text, bool use_plus);
30 
31 // Escapes a partial or complete file/pathname.  This includes:
32 // non-printable, non-7bit, and (including space)  "#%:<>?[\]^`{|}
33 BASE_EXPORT std::string EscapePath(StringPiece path);
34 
35 #if BUILDFLAG(IS_APPLE)
36 // Escapes characters as per expectations of NSURL. This includes:
37 // non-printable, non-7bit, and (including space)  "#%<>[\]^`{|}
38 BASE_EXPORT std::string EscapeNSURLPrecursor(StringPiece precursor);
39 #endif  // BUILDFLAG(IS_APPLE)
40 
41 // Escapes application/x-www-form-urlencoded content.  This includes:
42 // non-printable, non-7bit, and (including space)  ?>=<;+'&%$#"![\]^`{|}
43 // Space is escaped as + (if use_plus is true) and other special characters
44 // as %XX (hex).
45 BASE_EXPORT std::string EscapeUrlEncodedData(StringPiece path, bool use_plus);
46 
47 // Escapes all non-ASCII input, as well as escaping % to %25.
48 BASE_EXPORT std::string EscapeNonASCIIAndPercent(StringPiece input);
49 
50 // Escapes all non-ASCII input. Note this function leaves % unescaped, which
51 // means the unescaping the resulting string will not give back the original
52 // input.
53 BASE_EXPORT std::string EscapeNonASCII(StringPiece input);
54 
55 // Escapes characters in text suitable for use as an external protocol handler
56 // command.
57 // We %XX everything except alphanumerics and -_.!~*'() and the restricted
58 // characters (;/?:@&=+$,#[]) and a valid percent escape sequence (%XX).
59 BASE_EXPORT std::string EscapeExternalHandlerValue(StringPiece text);
60 
61 // Appends the given character to the output string, escaping the character if
62 // the character would be interpreted as an HTML delimiter.
63 BASE_EXPORT void AppendEscapedCharForHTML(char c, std::string* output);
64 
65 // Escapes chars that might cause this text to be interpreted as HTML tags.
66 BASE_EXPORT std::string EscapeForHTML(StringPiece text);
67 BASE_EXPORT std::u16string EscapeForHTML(StringPiece16 text);
68 
69 // Unescaping ------------------------------------------------------------------
70 
71 class UnescapeRule {
72  public:
73   // A combination of the following flags that is passed to the unescaping
74   // functions.
75   typedef uint32_t Type;
76 
77   // Don't unescape anything at all.
78   static constexpr Type NONE = 0;
79 
80   // Don't unescape anything special, but all normal unescaping will happen.
81   // This is a placeholder and can't be combined with other flags (since it's
82   // just the absence of them). All other unescape rules imply "normal" in
83   // addition to their special meaning. Things like escaped letters, digits,
84   // and most symbols will get unescaped with this mode.
85   static constexpr Type NORMAL = 1 << 0;
86 
87   // Convert %20 to spaces. In some places where we're showing URLs, we may
88   // want this. In places where the URL may be copied and pasted out, then
89   // you wouldn't want this since it might not be interpreted in one piece
90   // by other applications.  Other UTF-8 spaces will not be unescaped.
91   static constexpr Type SPACES = 1 << 1;
92 
93   // Unescapes '/' and '\\'. If these characters were unescaped, the resulting
94   // URL won't be the same as the source one. Moreover, they are dangerous to
95   // unescape in strings that will be used as file paths or names. This value
96   // should only be used when slashes don't have special meaning, like data
97   // URLs.
98   static constexpr Type PATH_SEPARATORS = 1 << 2;
99 
100   // Unescapes various characters that will change the meaning of URLs,
101   // including '%', '+', '&', '#'. Does not unescape path separators.
102   // If these characters were unescaped, the resulting URL won't be the same
103   // as the source one. This flag is used when generating final output like
104   // filenames for URLs where we won't be interpreting as a URL and want to do
105   // as much unescaping as possible.
106   static constexpr Type URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS = 1 << 3;
107 
108   // URL queries use "+" for space. This flag controls that replacement.
109   static constexpr Type REPLACE_PLUS_WITH_SPACE = 1 << 4;
110 };
111 
112 // Unescapes |escaped_text| and returns the result.
113 // Unescaping consists of looking for the exact pattern "%XX", where each X is
114 // a hex digit, and converting to the character with the numerical value of
115 // those digits. Thus "i%20=%203%3b" unescapes to "i = 3;", if the
116 // "UnescapeRule::SPACES" used.
117 //
118 // This method does not ensure that the output is a valid string using any
119 // character encoding. However, it does leave escaped certain byte sequences
120 // that would be dangerous to display to the user, because if interpreted as
121 // UTF-8, they could be used to mislead the user. Callers that want to
122 // unconditionally unescape everything for uses other than displaying data to
123 // the user should use UnescapeBinaryURLComponent().
124 BASE_EXPORT std::string UnescapeURLComponent(StringPiece escaped_text,
125                                              UnescapeRule::Type rules);
126 
127 // Unescapes the given substring as a URL, and then tries to interpret the
128 // result as being encoded as UTF-8. If the result is convertible into UTF-8, it
129 // will be returned as converted. If it is not, the original escaped string will
130 // be converted into a std::u16string and returned.  |adjustments| provides
131 // information on how the original string was adjusted to get the string
132 // returned.
133 BASE_EXPORT std::u16string UnescapeAndDecodeUTF8URLComponentWithAdjustments(
134     StringPiece text,
135     UnescapeRule::Type rules,
136     OffsetAdjuster::Adjustments* adjustments);
137 
138 // Unescapes a component of a URL for use as binary data. Unlike
139 // UnescapeURLComponent, leaves nothing unescaped, including nulls, invalid
140 // characters, characters that are unsafe to display, etc. This should *not*
141 // be used when displaying the decoded data to the user.
142 //
143 // Only the NORMAL and REPLACE_PLUS_WITH_SPACE rules are allowed.
144 BASE_EXPORT std::string UnescapeBinaryURLComponent(
145     StringPiece escaped_text,
146     UnescapeRule::Type rules = UnescapeRule::NORMAL);
147 
148 // Variant of UnescapeBinaryURLComponent().  Writes output to |unescaped_text|.
149 // Returns true on success, returns false and clears |unescaped_text| on
150 // failure. Fails on characters escaped that are unsafe to unescape in some
151 // contexts, which are defined as characters "\0" through "\x1F" (Which includes
152 // CRLF but not space), and optionally path separators. Path separators include
153 // both forward and backward slashes on all platforms. Does not fail if any of
154 // those characters appear unescaped in the input string.
155 BASE_EXPORT bool UnescapeBinaryURLComponentSafe(StringPiece escaped_text,
156                                                 bool fail_on_path_separators,
157                                                 std::string* unescaped_text);
158 
159 // Returns true if |escaped_text| contains any element of |bytes| in
160 // percent-encoded form.
161 //
162 // For example, if |bytes| is {'%', '/'}, returns true if |escaped_text|
163 // contains "%25" or "%2F", but not if it just contains bare '%' or '/'
164 // characters.
165 BASE_EXPORT bool ContainsEncodedBytes(StringPiece escaped_text,
166                                       const std::set<unsigned char>& bytes);
167 
168 // Unescapes the following ampersand character codes from |text|:
169 // &lt; &gt; &amp; &quot; &#39;
170 BASE_EXPORT std::u16string UnescapeForHTML(StringPiece16 text);
171 
172 }  // namespace base
173 
174 #endif  // BASE_STRINGS_ESCAPE_H_
175