View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import static org.htmlunit.BrowserVersionFeatures.ANCHOR_SEND_PING_REQUEST;
18  import static org.htmlunit.BrowserVersionFeatures.FORM_SUBMISSION_HEADER_CACHE_CONTROL_MAX_AGE;
19  import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
20  
21  import java.io.IOException;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.Locale;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.htmlunit.BrowserVersion;
29  import org.htmlunit.FormEncodingType;
30  import org.htmlunit.HttpHeader;
31  import org.htmlunit.HttpMethod;
32  import org.htmlunit.Page;
33  import org.htmlunit.WebClient;
34  import org.htmlunit.WebRequest;
35  import org.htmlunit.WebWindow;
36  import org.htmlunit.html.csp.ContentSecurityPolicy;
37  import org.htmlunit.protocol.javascript.JavaScriptURLConnection;
38  import org.htmlunit.util.ArrayUtils;
39  import org.htmlunit.util.StringUtils;
40  import org.htmlunit.util.UrlUtils;
41  
42  /**
43   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
44   *
45   * A helper class to be used by elements which support {@link HyperlinkElement} -
46   * currently {@link HtmlAnchor} ({@code a}) and {@link HtmlArea} ({@code area}), the two
47   * elements the HTML spec defines as "hyperlink elements".
48   *
49   * @author Ronald Brill
50   */
51  public final class HyperlinkElementSupport {
52  
53      private static final Log LOG = LogFactory.getLog(HyperlinkElementSupport.class);
54  
55      private HyperlinkElementSupport() {
56          // util class
57      }
58  
59      /**
60       * Support method that is called from {@link HtmlAnchor#doClickStateUpdate(boolean, boolean, String)}
61       * and {@link HtmlArea#doClickStateUpdate(boolean, boolean)}: resolves the target URL, sends the
62       * hyperlink-auditing ("ping") request if applicable, builds the navigation {@link WebRequest}
63       * (including Sec-Fetch-* setup), and hands it off for download/navigation.
64       *
65       * @param element the hyperlink element being clicked
66       * @param shiftKey {@code true} if SHIFT is pressed
67       * @param ctrlKey {@code true} if CTRL is pressed
68       * @param hrefSuffix the suffix to add to the element's {@code href} attribute
69       *        (for instance coordinates from an image map)
70       * @throws IOException if an IO error occurs
71       */
72      public static void doClickStateUpdate(final HyperlinkElement element, final boolean shiftKey,
73              final boolean ctrlKey, final String hrefSuffix) throws IOException {
74          final HtmlElement htmlElement = (HtmlElement) element;
75  
76          final String href = (element.getHrefAttribute() + hrefSuffix).trim();
77          if (LOG.isDebugEnabled()) {
78              final String w = htmlElement.getPage().getEnclosingWindow().getName();
79              LOG.debug("do click action in window '" + w + "', using href '" + href + "'");
80          }
81          if (ATTRIBUTE_NOT_DEFINED == element.getHrefAttribute()) {
82              return;
83          }
84          final String downloadAttribute = element.getDownloadAttribute();
85          HtmlPage page = (HtmlPage) htmlElement.getPage();
86          if (StringUtils.startsWithIgnoreCase(href, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
87              if (!ContentSecurityPolicy.allowsJavaScriptUrl(page, htmlElement, href)) {
88                  return;
89              }
90  
91              final StringBuilder builder = new StringBuilder(href.length());
92              builder.append(JavaScriptURLConnection.JAVASCRIPT_PREFIX);
93              for (int i = JavaScriptURLConnection.JAVASCRIPT_PREFIX.length(); i < href.length(); i++) {
94                  final char ch = href.charAt(i);
95                  if (ch == '%' && i + 2 < href.length()) {
96                      final char ch1 = Character.toUpperCase(href.charAt(i + 1));
97                      final char ch2 = Character.toUpperCase(href.charAt(i + 2));
98                      if ((Character.isDigit(ch1) || ch1 >= 'A' && ch1 <= 'F')
99                              && (Character.isDigit(ch2) || ch2 >= 'A' && ch2 <= 'F')) {
100                         builder.append((char) Integer.parseInt(href.substring(i + 1, i + 3), 16));
101                         i += 2;
102                         continue;
103                     }
104                 }
105                 builder.append(ch);
106             }
107 
108             final String target;
109             if (shiftKey || ctrlKey || ATTRIBUTE_NOT_DEFINED != downloadAttribute) {
110                 target = WebClient.TARGET_BLANK;
111             }
112             else {
113                 target = page.getResolvedTarget(element.getTargetAttribute());
114             }
115             final WebWindow win = page.getWebClient().openTargetWindow(page.getEnclosingWindow(),
116                     target, WebClient.TARGET_SELF);
117             Page enclosedPage = win.getEnclosedPage();
118             if (enclosedPage == null) {
119                 win.getWebClient().getPage(win, WebRequest.newAboutBlankRequest());
120                 enclosedPage = win.getEnclosedPage();
121             }
122             if (enclosedPage != null && enclosedPage.isHtmlPage()) {
123                 page = (HtmlPage) enclosedPage;
124                 page.executeJavaScript(builder.toString(), "javascript url", htmlElement.getStartLineNumber());
125             }
126             return;
127         }
128 
129         final URL url = getTargetUrl(href, page);
130 
131         final URL pageUrl = page.getUrl();
132 
133         final WebClient webClient = page.getWebClient();
134         final BrowserVersion browser = webClient.getBrowserVersion();
135         if (ATTRIBUTE_NOT_DEFINED != element.getPingAttribute() && browser.hasFeature(ANCHOR_SEND_PING_REQUEST)) {
136             final URL pingUrl = getTargetUrl(element.getPingAttribute(), page);
137             final WebRequest pingRequest = new WebRequest(pingUrl, HttpMethod.POST);
138             pingRequest.setAdditionalHeader(HttpHeader.ACCEPT_ENCODING, browser.getAcceptEncodingHeader());
139             pingRequest.setAdditionalHeader(HttpHeader.PING_FROM, pageUrl.toExternalForm());
140             pingRequest.setAdditionalHeader(HttpHeader.PING_TO, url.toExternalForm());
141             pingRequest.setEncodingType(FormEncodingType.TEXT_PLAIN); // text/ping
142             try {
143                 pingRequest.setAdditionalHeader(HttpHeader.ORIGIN,
144                         UrlUtils.getUrlWithProtocolAndAuthority(pageUrl).toExternalForm());
145             }
146             catch (final MalformedURLException e) {
147                 if (LOG.isInfoEnabled()) {
148                     LOG.info("Invalid origin url '" + pageUrl + "'");
149                 }
150             }
151             if (browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_MAX_AGE)) {
152                 pingRequest.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "max-age=0");
153             }
154             pingRequest.setRequestBody("PING");
155 
156             // Sec-Fetch-* support (https://www.w3.org/TR/fetch-metadata/):
157             // hyperlink-auditing (ping) requests have no destination and are always no-cors
158             pingRequest.setFetchDestination(WebRequest.FetchDestination.EMPTY);
159             pingRequest.setFetchModeOverride(WebRequest.FetchMode.NO_CORS);
160             pingRequest.setRequestingUrl(pageUrl);
161 
162             try {
163                 webClient.loadWebResponse(pingRequest);
164             }
165             catch (final Exception e) {
166                 // ignore
167             }
168         }
169 
170         // upgrade-insecure-requests of the Content-Security-Policy (top level navigation)
171         final WebRequest webRequest = new WebRequest(ContentSecurityPolicy.upgradeInsecureNavigation(page, url),
172                                                             browser.getHtmlAcceptHeader(),
173                                                             browser.getAcceptEncodingHeader());
174         ContentSecurityPolicy.upgradeInsecureNavigationRedirects(page, webRequest);
175         // use the page encoding even if this is a GET requests
176         webRequest.setCharset(page.getCharset());
177 
178         // Sec-Fetch-* support (https://www.w3.org/TR/fetch-metadata/):
179         // this is a top-level navigation, initiated by the page containing the link.
180         // The initiator must be set regardless of "noreferrer", since Sec-Fetch-Site
181         // reflects the true relationship between initiator and target even when the
182         // Referer header itself is suppressed.
183         // TODO: userActivation is hardcoded to true here because doClickStateUpdate()
184         // currently has no way to tell a real (WebDriver-simulated) click apart from a
185         // script-triggered one (e.g. anchor.click()/area.click()); real browsers only
186         // send Sec-Fetch-User for the former. Once the click/event dispatch path exposes
187         // a trusted/user-gesture flag, thread it through instead of hardcoding this.
188         webRequest.markAsNavigation(page.getUrl(), true);
189 
190         if (!relContainsNoreferrer(element)) {
191             webRequest.setRefererHeader(page.getUrl());
192         }
193 
194         if (LOG.isDebugEnabled()) {
195             LOG.debug(
196                     "Getting page for " + url.toExternalForm()
197                     + ", derived from href '" + href
198                     + "', using the originating URL "
199                     + page.getUrl());
200         }
201 
202         final String target;
203         if (shiftKey || ctrlKey
204                 || (webClient.getAttachmentHandler() == null
205                         && ATTRIBUTE_NOT_DEFINED != downloadAttribute)) {
206             target = WebClient.TARGET_BLANK;
207         }
208         else {
209             target = page.getResolvedTarget(element.getTargetAttribute());
210         }
211         page.getWebClient().download(page.getEnclosingWindow(), target, webRequest,
212                 true, (ATTRIBUTE_NOT_DEFINED == downloadAttribute) ? null : downloadAttribute,
213                 htmlElement.getClass().getSimpleName() + " click");
214     }
215 
216     /**
217      * Returns {@code true} if the element's {@code rel} attribute contains {@code noreferrer}.
218      *
219      * @param element the hyperlink element
220      * @return {@code true} if the element's {@code rel} attribute contains {@code noreferrer}
221      */
222     public static boolean relContainsNoreferrer(final HyperlinkElement element) {
223         String rel = element.getRelAttribute();
224         if (rel != null) {
225             rel = rel.toLowerCase(Locale.ROOT);
226             return ArrayUtils.contains(StringUtils.splitAtBlank(rel), "noreferrer");
227         }
228         return false;
229     }
230 
231     /**
232      * Returns the calculated target url.
233      *
234      * @param href the href
235      * @param page the HtmlPage
236      * @return the calculated target url.
237      * @throws MalformedURLException if an IO error occurs
238      */
239     public static URL getTargetUrl(final String href, final HtmlPage page) throws MalformedURLException {
240         URL url = page.getFullyQualifiedUrl(href);
241         // fix for empty url
242         if (StringUtils.isEmptyOrNull(href)) {
243             url = UrlUtils.getUrlWithNewRef(url, null);
244         }
245         return url;
246     }
247 }