@@ -35,18 +35,29 @@ const fetchProjectToken = async projectId => {
3535 // Parse ?token=abcdef
3636 const searchParams = new URLSearchParams ( location . search ) ;
3737 if ( searchParams . has ( 'token' ) ) {
38- return searchParams . get ( 'token' ) ;
38+ const token = searchParams . get ( 'token' ) ;
39+ // for security reasons, remove token from URL without reloading and without history
40+ searchParams . delete ( 'token' ) ;
41+ const newUrl = `${ location . pathname } ${ searchParams . toString ( ) ? '?' + searchParams . toString ( ) : '' } ${ location . hash } ` ;
42+ window . history . replaceState ( { } , document . title , newUrl ) ;
43+ return token ;
3944 }
4045 // Parse #1?token=abcdef
4146 const hashParams = new URLSearchParams ( location . hash . split ( '?' ) [ 1 ] ) ;
42- if ( hashParams . has ( 'token' ) ) {
43- return hashParams . get ( 'token' ) ;
47+ if ( hashParams && hashParams . has ( 'token' ) ) {
48+ const token = hashParams . get ( 'token' ) ;
49+ // for security reasons, remove token from URL without reloading and without history
50+ hashParams . delete ( 'token' ) ;
51+ const newHash = hashParams . toString ( ) ? `?${ hashParams . toString ( ) } ` : '' ;
52+ const newUrl = `${ location . pathname } ${ location . search } ${ location . hash . split ( '?' ) [ 0 ] } ${ newHash } ` ;
53+ window . history . replaceState ( { } , document . title , newUrl ) ;
54+ return token ;
4455 }
4556 try {
4657 const metadata = await fetchProjectMeta ( projectId ) ;
4758 return metadata . project_token ;
4859 } catch ( e ) {
49- log . error ( e ) ;
60+ console . error ( e ) ; // Use console.error instead of log.error for browser compatibility.
5061 throw new Error ( 'Cannot access project token. Project is probably unshared. See https://docs.turbowarp.org/unshared-projects' ) ;
5162 }
5263} ;
0 commit comments