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
00a126aa
Commit
00a126aa
authored
Feb 10, 2021
by
Thomas Randolph
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add URL Utility for making safe-ER URL paths
parent
c39870f2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
0 deletions
+53
-0
app/assets/javascripts/lib/utils/url_utility.js
app/assets/javascripts/lib/utils/url_utility.js
+30
-0
spec/frontend/lib/utils/url_utility_spec.js
spec/frontend/lib/utils/url_utility_spec.js
+23
-0
No files found.
app/assets/javascripts/lib/utils/url_utility.js
View file @
00a126aa
...
...
@@ -16,6 +16,36 @@ function decodeUrlParameter(val) {
return
decodeURIComponent
(
val
.
replace
(
/
\+
/g
,
'
%20
'
));
}
/**
* Safely encodes a string to be used as a path
*
* Note: This function DOES encode typical URL parts like ?, =, &, #, and +
* If you need to use search parameters or URL fragments, they should be
* added AFTER calling this function, not before.
*
* @param {String} potentiallyUnsafePath
* @returns {String}
*/
export
function
encodeSaferUrl
(
potentiallyUnsafePath
)
{
const
unencode
=
[
'
%2F
'
];
const
encode
=
[
'
#
'
,
'
!
'
,
'
~
'
,
'
\\
*
'
,
"
'
"
,
'
\\
(
'
,
'
\\
)
'
];
let
saferPath
=
encodeURIComponent
(
potentiallyUnsafePath
);
unencode
.
forEach
((
code
)
=>
{
saferPath
=
saferPath
.
replace
(
new
RegExp
(
code
,
'
g
'
),
decodeURIComponent
(
code
));
});
encode
.
forEach
((
code
)
=>
{
const
encodedValue
=
code
.
codePointAt
(
code
.
length
-
1
)
.
toString
(
16
)
.
toUpperCase
();
saferPath
=
saferPath
.
replace
(
new
RegExp
(
code
,
'
g
'
),
`%
${
encodedValue
}
`
);
});
return
saferPath
;
}
export
function
cleanLeadingSeparator
(
path
)
{
return
path
.
replace
(
PATH_SEPARATOR_LEADING_REGEX
,
''
);
}
...
...
spec/frontend/lib/utils/url_utility_spec.js
View file @
00a126aa
...
...
@@ -880,4 +880,27 @@ describe('URL utility', () => {
expect
(
urlUtils
.
getURLOrigin
(
url
)).
toBe
(
expectation
);
});
});
describe
(
'
encodeSaferUrl
'
,
()
=>
{
it
.
each
`
character | input | output
${
'
'
}
|
${
'
/url/hello 1.jpg
'
}
|
${
'
/url/hello%201.jpg
'
}
${
'
#
'
}
|
${
'
/url/hello#1.jpg
'
}
|
${
'
/url/hello%231.jpg
'
}
${
'
!
'
}
|
${
'
/url/hello!.jpg
'
}
|
${
'
/url/hello%21.jpg
'
}
${
'
~
'
}
|
${
'
/url/hello~.jpg
'
}
|
${
'
/url/hello%7E.jpg
'
}
${
'
*
'
}
|
${
'
/url/hello*.jpg
'
}
|
${
'
/url/hello%2A.jpg
'
}
${
"
'
"
}
|
${
"
/url/hello'.jpg
"
}
|
${
'
/url/hello%27.jpg
'
}
${
'
(
'
}
|
${
'
/url/hello(.jpg
'
}
|
${
'
/url/hello%28.jpg
'
}
${
'
)
'
}
|
${
'
/url/hello).jpg
'
}
|
${
'
/url/hello%29.jpg
'
}
${
'
?
'
}
|
${
'
/url/hello?.jpg
'
}
|
${
'
/url/hello%3F.jpg
'
}
${
'
=
'
}
|
${
'
/url/hello=.jpg
'
}
|
${
'
/url/hello%3D.jpg
'
}
${
'
+
'
}
|
${
'
/url/hello+.jpg
'
}
|
${
'
/url/hello%2B.jpg
'
}
${
'
&
'
}
|
${
'
/url/hello&.jpg
'
}
|
${
'
/url/hello%26.jpg
'
}
`
(
'
properly escapes `$character` characters while retaining the integrity of the URL
'
,
({
input
,
output
})
=>
{
expect
(
urlUtils
.
encodeSaferUrl
(
input
)).
toBe
(
output
);
},
);
});
});
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