@tldraw/validate 5.7.0-next.0ef3ff64787a → 5.7.0-next.147eae59044f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1878,26 +1878,99 @@ function parseUrl(str: string) {
1878
1878
  }
1879
1879
  }
1880
1880
 
1881
- const validLinkProtocols = new Set(['http:', 'https:', 'mailto:'])
1881
+ // A block list rather than an allow list: every client and the sync server validate shapes with
1882
+ // this, so a scheme accepted by one version and rejected by another breaks shared boards. Blocking
1883
+ // a scheme later has the same problem, so only add one here with a migration that blanks it.
1884
+ // Whether an accepted link opens without asking is decided when it's clicked, by tldraw's
1885
+ // `getLinkOpenPolicy`, which can change freely.
1886
+ const blockedLinkProtocols = new Set([
1887
+ // Run script or render arbitrary content in the page's origin
1888
+ 'javascript:',
1889
+ 'vbscript:',
1890
+ 'data:',
1891
+ 'blob:',
1892
+ 'filesystem:',
1893
+ // Reach local files or browser internals
1894
+ 'file:',
1895
+ 'about:',
1896
+ 'view-source:',
1897
+ 'chrome:',
1898
+ 'chrome-extension:',
1899
+ 'moz-extension:',
1900
+ 'resource:',
1901
+ 'jar:',
1902
+ // tldraw's local asset store, not something a link can open
1903
+ 'asset:',
1904
+ // Windows protocol handlers with known remote code execution exploits, such as CVE-2022-30190
1905
+ // ("Follina") through `ms-msdt:` and `search-ms:`
1906
+ 'ms-msdt:',
1907
+ 'search-ms:',
1908
+ 'search:',
1909
+ 'ms-officecmd:',
1910
+ 'ms-appinstaller:',
1911
+ 'shell:',
1912
+ 'hcp:',
1913
+ 'ms-its:',
1914
+ 'its:',
1915
+ 'mk:',
1916
+ 'res:',
1917
+ // Open remote Office documents, a common phishing route that also leaks the NTLM hash
1918
+ 'ms-word:',
1919
+ 'ms-excel:',
1920
+ 'ms-powerpoint:',
1921
+ 'ms-visio:',
1922
+ 'ms-access:',
1923
+ 'ms-project:',
1924
+ 'ms-publisher:',
1925
+ 'ms-spd:',
1926
+ 'ms-infopath:',
1927
+ // Leak the Windows NTLM hash to a remote host
1928
+ 'smb:',
1929
+ 'ldap:',
1930
+ 'ldaps:',
1931
+ // Drive the OS into account setup, settings, or another browser
1932
+ 'ms-cxh:',
1933
+ 'ms-cxh-full:',
1934
+ 'ms-settings:',
1935
+ 'microsoft-edge:',
1936
+ // Install or run remote apps
1937
+ 'intent:',
1938
+ 'jnlp:',
1939
+ 'itms-services:',
1940
+ 'steam:',
1941
+ ])
1942
+
1943
+ // `localhost:3000` and `example.com:8080` parse as a `localhost:` or `example.com:` scheme. Ports
1944
+ // are at most five digits, so this doesn't catch phone links like `tel:5551234`.
1945
+ const hostWithPortRegex = /^\d{1,5}(?:[/?#]|$)/
1882
1946
 
1883
1947
  /**
1884
- * Validator for URLs that are safe to use as user-facing links. Accepts http, https, and mailto protocols.
1885
- * This validator provides security by rejecting potentially dangerous protocols like javascript:.
1948
+ * Validator for URLs that are safe to use as user-facing links. Accepts http, https, and mailto
1949
+ * links, and app links that open other apps (such as `vscode://file/…`). This validator provides
1950
+ * security by rejecting potentially dangerous protocols like javascript:.
1886
1951
  *
1887
1952
  * @example
1888
1953
  * ```ts
1889
1954
  * const link = T.linkUrl.validate("https://example.com") // Valid
1890
1955
  * const email = T.linkUrl.validate("mailto:user@example.com") // Valid
1956
+ * const appLink = T.linkUrl.validate("vscode://file/src/index.ts") // Valid
1891
1957
  * T.linkUrl.validate("") // Valid (empty string allowed)
1892
1958
  * T.linkUrl.validate("javascript:alert(1)") // Throws ValidationError (unsafe protocol)
1959
+ * T.linkUrl.validate("localhost:3000") // Throws ValidationError (a host, not a protocol)
1893
1960
  * ```
1894
1961
  * @public
1895
1962
  */
1896
1963
  export const linkUrl = string.check((value) => {
1897
1964
  if (value === '') return
1898
1965
  const url = parseUrl(value)
1966
+ const protocol = url.protocol.toLowerCase()
1899
1967
 
1900
- if (!validLinkProtocols.has(url.protocol.toLowerCase())) {
1968
+ if (
1969
+ blockedLinkProtocols.has(protocol) ||
1970
+ // A Windows drive letter, as in `C:/Users`
1971
+ protocol.length === 2 ||
1972
+ hostWithPortRegex.test(url.href.slice(protocol.length))
1973
+ ) {
1901
1974
  throw new ValidationError(
1902
1975
  `Expected a valid url, got ${JSON.stringify(value)} (invalid protocol)`
1903
1976
  )
@@ -56,4 +56,36 @@ describe('§15 URL validators', () => {
56
56
  expect(() => T.srcUrl.validate(null)).toThrow('Expected string, got null')
57
57
  expect(() => T.httpUrl.validate(undefined)).toThrow('Expected string, got undefined')
58
58
  })
59
+
60
+ it('[UR7] linkUrl accepts app links with any scheme outside the block list', () => {
61
+ expect(T.linkUrl.validate('vscode://file/src/index.ts')).toBe('vscode://file/src/index.ts')
62
+ expect(T.linkUrl.validate('spotify:track:abc')).toBe('spotify:track:abc')
63
+ expect(T.linkUrl.validate('tel:5551234')).toBe('tel:5551234')
64
+
65
+ expect(() => T.linkUrl.validate('javascript://%0aalert(1)')).toThrow('(invalid protocol)')
66
+ expect(() => T.linkUrl.validate('data://text/html,hi')).toThrow('(invalid protocol)')
67
+ expect(() => T.linkUrl.validate('file:///etc/passwd')).toThrow('(invalid protocol)')
68
+ expect(() => T.linkUrl.validate('about:blank#blocked')).toThrow('(invalid protocol)')
69
+ expect(() => T.linkUrl.validate('ms-msdt:/id PCWDiagnostic')).toThrow('(invalid protocol)')
70
+ for (const url of [
71
+ 'ms-word:ofe|u|https://evil/x.docx',
72
+ 'smb://evil/share',
73
+ 'ldap://evil',
74
+ 'ms-settings:privacy',
75
+ 'microsoft-edge:https://evil',
76
+ 'intent://scan/#Intent;scheme=zxing;end',
77
+ 'jnlp://evil/app.jnlp',
78
+ 'Steam://run/1',
79
+ 'itms-services://?action=download-manifest',
80
+ ]) {
81
+ expect(() => T.linkUrl.validate(url)).toThrow('(invalid protocol)')
82
+ }
83
+ expect(() => T.linkUrl.validate('C:/Users/me/file.txt')).toThrow('(invalid protocol)')
84
+ })
85
+
86
+ it('[UR8] linkUrl rejects a host with a port, which parses as a scheme', () => {
87
+ expect(() => T.linkUrl.validate('localhost:3000')).toThrow('(invalid protocol)')
88
+ expect(() => T.linkUrl.validate('example.com:8080/path')).toThrow('(invalid protocol)')
89
+ expect(() => T.linkUrl.validate('intranet:80?q=1')).toThrow('(invalid protocol)')
90
+ })
59
91
  })