(themeRoot: string)
| 27 | }; |
| 28 | |
| 29 | const validateThemeRoot = (themeRoot: string) => { |
| 30 | let resultUrl; |
| 31 | let isSameOrigin = false; |
| 32 | |
| 33 | try { |
| 34 | if (themeRoot.startsWith(".") || (themeRoot.startsWith("/") && !themeRoot.startsWith("//"))) { |
| 35 | // Handle relative url |
| 36 | // new URL("/newExmPath", "http://example.com/exmPath") => http://example.com/newExmPath |
| 37 | // new URL("./newExmPath", "http://example.com/exmPath") => http://example.com/exmPath/newExmPath |
| 38 | // new URL("../newExmPath", "http://example.com/exmPath") => http://example.com/newExmPath |
| 39 | resultUrl = new URL(themeRoot, getLocationHref()).toString(); |
| 40 | isSameOrigin = true; |
| 41 | } else { |
| 42 | // Protocol-relative URLs (//host/path) need a base to resolve the protocol |
| 43 | const themeRootURL = themeRoot.startsWith("//") ? new URL(themeRoot, getLocationHref()) : new URL(themeRoot); |
| 44 | const origin = themeRootURL.origin; |
| 45 | const currentOrigin = new URL(getLocationHref()).origin; |
| 46 | |
| 47 | // Check if the absolute URL is same-origin |
| 48 | isSameOrigin = origin === currentOrigin; |
| 49 | |
| 50 | if (origin && validateThemeOrigin(origin, isSameOrigin)) { |
| 51 | // If origin is allowed, use it |
| 52 | resultUrl = themeRootURL.toString(); |
| 53 | } else { |
| 54 | // If origin is not allowed, return undefined to indicate validation failed |
| 55 | return undefined; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (!resultUrl.endsWith("/")) { |
| 60 | resultUrl = `${resultUrl}/`; |
| 61 | } |
| 62 | |
| 63 | return `${resultUrl}UI5/`; |
| 64 | } catch (e) { |
| 65 | // Catch if URL is not correct |
| 66 | return undefined; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | export default validateThemeRoot; |
no test coverage detected
searching dependent graphs…