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
d9604942
Commit
d9604942
authored
Jul 05, 2021
by
Dheeraj Joshi
Committed by
Miguel Rincon
Jul 05, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Disallow specific data attributes from DOMPurify's default config [RUN AS-IF-FOSS] [RUN ALL RSPEC]
parent
34c70444
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
0 deletions
+27
-0
app/assets/javascripts/lib/dompurify.js
app/assets/javascripts/lib/dompurify.js
+11
-0
spec/frontend/lib/dompurify_spec.js
spec/frontend/lib/dompurify_spec.js
+16
-0
No files found.
app/assets/javascripts/lib/dompurify.js
View file @
d9604942
...
...
@@ -7,6 +7,8 @@ const defaultConfig = {
ADD_TAGS
:
[
'
use
'
],
};
const
forbiddenDataAttrs
=
[
'
data-remote
'
,
'
data-url
'
,
'
data-type
'
,
'
data-method
'
];
// Only icons urls from `gon` are allowed
const
getAllowedIconUrls
=
(
gon
=
window
.
gon
)
=>
[
gon
.
sprite_file_icons
,
gon
.
sprite_icons
].
filter
(
Boolean
);
...
...
@@ -44,10 +46,19 @@ const sanitizeSvgIcon = (node) => {
removeUnsafeHref
(
node
,
'
xlink:href
'
);
};
const
sanitizeHTMLAttributes
=
(
node
)
=>
{
forbiddenDataAttrs
.
forEach
((
attr
)
=>
{
if
(
node
.
hasAttribute
(
attr
))
{
node
.
removeAttribute
(
attr
);
}
});
};
addHook
(
'
afterSanitizeAttributes
'
,
(
node
)
=>
{
if
(
node
.
tagName
.
toLowerCase
()
===
'
use
'
)
{
sanitizeSvgIcon
(
node
);
}
sanitizeHTMLAttributes
(
node
);
});
export
const
sanitize
=
(
val
,
config
=
defaultConfig
)
=>
dompurifySanitize
(
val
,
config
);
spec/frontend/lib/dompurify_spec.js
View file @
d9604942
...
...
@@ -30,6 +30,9 @@ const unsafeUrls = [
`https://evil.url/
${
absoluteGon
.
sprite_file_icons
}
`
,
];
const
forbiddenDataAttrs
=
[
'
data-remote
'
,
'
data-url
'
,
'
data-type
'
,
'
data-method
'
];
const
acceptedDataAttrs
=
[
'
data-random
'
,
'
data-custom
'
];
describe
(
'
~/lib/dompurify
'
,
()
=>
{
let
originalGon
;
...
...
@@ -95,4 +98,17 @@ describe('~/lib/dompurify', () => {
expect
(
sanitize
(
htmlXlink
)).
toBe
(
expectedSanitized
);
});
});
describe
(
'
handles data attributes correctly
'
,
()
=>
{
it
.
each
(
forbiddenDataAttrs
)(
'
removes %s attributes
'
,
(
attr
)
=>
{
const
htmlHref
=
`<a
${
attr
}
="true">hello</a>`
;
expect
(
sanitize
(
htmlHref
)).
toBe
(
'
<a>hello</a>
'
);
});
it
.
each
(
acceptedDataAttrs
)(
'
does not remove %s attributes
'
,
(
attr
)
=>
{
const
attrWithValue
=
`
${
attr
}
="true"`
;
const
htmlHref
=
`<a
${
attrWithValue
}
>hello</a>`
;
expect
(
sanitize
(
htmlHref
)).
toBe
(
`<a
${
attrWithValue
}
>hello</a>`
);
});
});
});
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