Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
3985f24f
Commit
3985f24f
authored
Jun 03, 2019
by
GitLab Bot
Browse files
Options
Browse Files
Download
Plain Diff
Automatic merge of gitlab-org/gitlab-ce master
parents
3512898e
07630b3b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
120 additions
and
0 deletions
+120
-0
app/assets/javascripts/lib/utils/url_utility.js
app/assets/javascripts/lib/utils/url_utility.js
+36
-0
spec/frontend/lib/utils/url_utility_spec.js
spec/frontend/lib/utils/url_utility_spec.js
+84
-0
No files found.
app/assets/javascripts/lib/utils/url_utility.js
View file @
3985f24f
...
...
@@ -121,4 +121,40 @@ export function webIDEUrl(route = undefined) {
return
returnUrl
;
}
/**
* Returns current base URL
*/
export
function
getBaseURL
()
{
const
{
protocol
,
host
}
=
window
.
location
;
return
`
${
protocol
}
//
${
host
}
`
;
}
/**
* Returns true if url is an absolute or root-relative URL
*
* @param {String} url
*/
export
function
isAbsoluteOrRootRelative
(
url
)
{
return
/^
(
https
?
:
)?\/
/
.
test
(
url
);
}
/**
* Checks if the provided URL is a safe URL (absolute http(s) or root-relative URL)
*
* @param {String} url that will be checked
* @returns {Boolean}
*/
export
function
isSafeURL
(
url
)
{
if
(
!
isAbsoluteOrRootRelative
(
url
))
{
return
false
;
}
try
{
const
parsedUrl
=
new
URL
(
url
,
getBaseURL
());
return
[
'
http:
'
,
'
https:
'
].
includes
(
parsedUrl
.
protocol
);
}
catch
{
return
false
;
}
}
export
{
join
as
joinPaths
}
from
'
path
'
;
spec/
javascripts
/lib/utils/url_utility_spec.js
→
spec/
frontend
/lib/utils/url_utility_spec.js
View file @
3985f24f
...
...
@@ -107,4 +107,88 @@ describe('URL utility', () => {
expect
(
url
).
toBe
(
'
/home/feature#install
'
);
});
});
describe
(
'
getBaseURL
'
,
()
=>
{
beforeEach
(()
=>
{
global
.
window
=
Object
.
create
(
window
);
Object
.
defineProperty
(
window
,
'
location
'
,
{
value
:
{
host
:
'
gitlab.com
'
,
protocol
:
'
https:
'
,
},
});
});
it
(
'
returns correct base URL
'
,
()
=>
{
expect
(
urlUtils
.
getBaseURL
()).
toBe
(
'
https://gitlab.com
'
);
});
});
describe
(
'
isAbsoluteOrRootRelative
'
,
()
=>
{
const
validUrls
=
[
'
https://gitlab.com/
'
,
'
http://gitlab.com/
'
,
'
/users/sign_in
'
];
const
invalidUrls
=
[
'
https://gitlab.com/
'
,
'
./file/path
'
,
'
notanurl
'
,
'
<a></a>
'
];
it
.
each
(
validUrls
)(
`returns true for %s`
,
url
=>
{
expect
(
urlUtils
.
isAbsoluteOrRootRelative
(
url
)).
toBe
(
true
);
});
it
.
each
(
invalidUrls
)(
`returns false for %s`
,
url
=>
{
expect
(
urlUtils
.
isAbsoluteOrRootRelative
(
url
)).
toBe
(
false
);
});
});
describe
(
'
isSafeUrl
'
,
()
=>
{
const
absoluteUrls
=
[
'
http://example.org
'
,
'
http://example.org:8080
'
,
'
https://example.org
'
,
'
https://example.org:8080
'
,
'
https://192.168.1.1
'
,
];
const
rootRelativeUrls
=
[
'
/relative/link
'
];
const
relativeUrls
=
[
'
./relative/link
'
,
'
../relative/link
'
];
const
urlsWithoutHost
=
[
'
http://
'
,
'
https://
'
,
'
https:https:https:
'
];
/* eslint-disable no-script-url */
const
nonHttpUrls
=
[
'
javascript:
'
,
'
javascript:alert("XSS")
'
,
'
jav
\t
ascript:alert("XSS");
'
,
'
 javascript:alert("XSS");
'
,
'
ftp://192.168.1.1
'
,
'
file:///
'
,
'
file:///etc/hosts
'
,
];
/* eslint-enable no-script-url */
// javascript:alert('XSS')
const
encodedJavaScriptUrls
=
[
'
javascript:alert('XSS')
'
,
'
javascript:alert('XSS')
'
,
'
javascript:alert('XSS')
'
,
'
\\
u006A
\\
u0061
\\
u0076
\\
u0061
\\
u0073
\\
u0063
\\
u0072
\\
u0069
\\
u0070
\\
u0074
\\
u003A
\\
u0061
\\
u006C
\\
u0065
\\
u0072
\\
u0074
\\
u0028
\\
u0027
\\
u0058
\\
u0053
\\
u0053
\\
u0027
\\
u0029
'
,
];
const
safeUrls
=
[...
absoluteUrls
,
...
rootRelativeUrls
];
const
unsafeUrls
=
[
...
relativeUrls
,
...
urlsWithoutHost
,
...
nonHttpUrls
,
...
encodedJavaScriptUrls
,
];
describe
(
'
with URL constructor support
'
,
()
=>
{
it
.
each
(
safeUrls
)(
'
returns true for %s
'
,
url
=>
{
expect
(
urlUtils
.
isSafeURL
(
url
)).
toBe
(
true
);
});
it
.
each
(
unsafeUrls
)(
'
returns false for %s
'
,
url
=>
{
expect
(
urlUtils
.
isSafeURL
(
url
)).
toBe
(
false
);
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment