1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import {disableJavaScriptURLs} from 'shared/ReactFeatureFlags';
    
  11. 
    
  12. // A javascript: URL can contain leading C0 control or \u0020 SPACE,
    
  13. // and any newline or tab are filtered out as if they're not part of the URL.
    
  14. // https://url.spec.whatwg.org/#url-parsing
    
  15. // Tab or newline are defined as \r\n\t:
    
  16. // https://infra.spec.whatwg.org/#ascii-tab-or-newline
    
  17. // A C0 control is a code point in the range \u0000 NULL to \u001F
    
  18. // INFORMATION SEPARATOR ONE, inclusive:
    
  19. // https://infra.spec.whatwg.org/#c0-control-or-space
    
  20. 
    
  21. /* eslint-disable max-len */
    
  22. const isJavaScriptProtocol =
    
  23.   /^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*\:/i;
    
  24. 
    
  25. let didWarn = false;
    
  26. 
    
  27. function sanitizeURL<T>(url: T): T | string {
    
  28.   // We should never have symbols here because they get filtered out elsewhere.
    
  29.   // eslint-disable-next-line react-internal/safe-string-coercion
    
  30.   const stringifiedURL = '' + (url: any);
    
  31.   if (disableJavaScriptURLs) {
    
  32.     if (isJavaScriptProtocol.test(stringifiedURL)) {
    
  33.       // Return a different javascript: url that doesn't cause any side-effects and just
    
  34.       // throws if ever visited.
    
  35.       // eslint-disable-next-line no-script-url
    
  36.       return "javascript:throw new Error('React has blocked a javascript: URL as a security precaution.')";
    
  37.     }
    
  38.   } else if (__DEV__) {
    
  39.     if (!didWarn && isJavaScriptProtocol.test(stringifiedURL)) {
    
  40.       didWarn = true;
    
  41.       console.error(
    
  42.         'A future version of React will block javascript: URLs as a security precaution. ' +
    
  43.           'Use event handlers instead if you can. If you need to generate unsafe HTML try ' +
    
  44.           'using dangerouslySetInnerHTML instead. React was passed %s.',
    
  45.         JSON.stringify(stringifiedURL),
    
  46.       );
    
  47.     }
    
  48.   }
    
  49.   return url;
    
  50. }
    
  51. 
    
  52. export default sanitizeURL;