2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-25 08:12:20 -04:00
|
|
|
require 'fast_spec_helper'
|
|
|
|
require 'html/pipeline'
|
|
|
|
require 'addressable'
|
Extract SanitizeNodeLink and apply to WikiLinkFilter
The SanitizationFilter was running before the WikiFilter. Since
WikiFilter can modify links, we could see links that _should_ be stopped
by SanatizationFilter being rendered on the page. I (kerrizor) had
previously addressed the bug in: https://gitlab.com/gitlab-org/gitlab-ee/commit/7bc971915bbeadb950bb0e1f13510bf3038229a4
However, an additional exploit was discovered after that was merged.
Working through the issue, we couldn't simply shuffle the order of
filters, due to some implicit assumptions about the order of filters, so
instead we've extracted the logic that sanitizes a Nokogiri-generated
Node object, and applied it to the WikiLinkFilter as well.
On moving filters around:
Once we start moving around filters, we get cascading failures; fix one,
another one crops up. Many of the existing filters in the WikiPipeline
chain seem to assume that other filters have already done their work,
and thus operate on a "transform anything that's left" basis;
WikiFilter, for instance, assumes any link it finds in the markdown
should be prepended with the wiki_base_path.. but if it does that, it
also turns `href="@user"` into `href="/path/to/wiki/@user"`, which the
UserReferenceFilter doesn't see as a user reference it needs to
transform into a user profile link. This is true for all the reference
filters in the WikiPipeline.
2019-07-26 09:41:11 -04:00
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::Utils::SanitizeNodeLink do
|
Extract SanitizeNodeLink and apply to WikiLinkFilter
The SanitizationFilter was running before the WikiFilter. Since
WikiFilter can modify links, we could see links that _should_ be stopped
by SanatizationFilter being rendered on the page. I (kerrizor) had
previously addressed the bug in: https://gitlab.com/gitlab-org/gitlab-ee/commit/7bc971915bbeadb950bb0e1f13510bf3038229a4
However, an additional exploit was discovered after that was merged.
Working through the issue, we couldn't simply shuffle the order of
filters, due to some implicit assumptions about the order of filters, so
instead we've extracted the logic that sanitizes a Nokogiri-generated
Node object, and applied it to the WikiLinkFilter as well.
On moving filters around:
Once we start moving around filters, we get cascading failures; fix one,
another one crops up. Many of the existing filters in the WikiPipeline
chain seem to assume that other filters have already done their work,
and thus operate on a "transform anything that's left" basis;
WikiFilter, for instance, assumes any link it finds in the markdown
should be prepended with the wiki_base_path.. but if it does that, it
also turns `href="@user"` into `href="/path/to/wiki/@user"`, which the
UserReferenceFilter doesn't see as a user reference it needs to
transform into a user profile link. This is true for all the reference
filters in the WikiPipeline.
2019-07-26 09:41:11 -04:00
|
|
|
let(:klass) do
|
|
|
|
struct = Struct.new(:value)
|
|
|
|
struct.include(described_class)
|
|
|
|
|
|
|
|
struct
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:object) { klass.new(:value) }
|
|
|
|
|
|
|
|
invalid_schemes = [
|
|
|
|
"javascript:",
|
|
|
|
"JaVaScRiPt:",
|
|
|
|
"\u0001java\u0003script:",
|
|
|
|
"javascript :",
|
|
|
|
"javascript: ",
|
|
|
|
"javascript : ",
|
|
|
|
":javascript:",
|
|
|
|
"javascript:",
|
|
|
|
"javascript:",
|
|
|
|
"  javascript:"
|
|
|
|
]
|
|
|
|
|
|
|
|
invalid_schemes.each do |scheme|
|
|
|
|
context "with the scheme: #{scheme}" do
|
|
|
|
describe "#remove_unsafe_links" do
|
|
|
|
tags = {
|
|
|
|
a: {
|
|
|
|
doc: HTML::Pipeline.parse("<a href='#{scheme}alert(1);'>foo</a>"),
|
|
|
|
attr: "href",
|
|
|
|
node_to_check: -> (doc) { doc.children.first }
|
|
|
|
},
|
|
|
|
img: {
|
|
|
|
doc: HTML::Pipeline.parse("<img src='#{scheme}alert(1);'>"),
|
|
|
|
attr: "src",
|
|
|
|
node_to_check: -> (doc) { doc.children.first }
|
|
|
|
},
|
|
|
|
video: {
|
|
|
|
doc: HTML::Pipeline.parse("<video><source src='#{scheme}alert(1);'></video>"),
|
|
|
|
attr: "src",
|
|
|
|
node_to_check: -> (doc) { doc.children.first.children.filter("source").first }
|
2019-10-09 08:06:13 -04:00
|
|
|
},
|
|
|
|
audio: {
|
|
|
|
doc: HTML::Pipeline.parse("<audio><source src='#{scheme}alert(1);'></audio>"),
|
|
|
|
attr: "src",
|
|
|
|
node_to_check: -> (doc) { doc.children.first.children.filter("source").first }
|
Extract SanitizeNodeLink and apply to WikiLinkFilter
The SanitizationFilter was running before the WikiFilter. Since
WikiFilter can modify links, we could see links that _should_ be stopped
by SanatizationFilter being rendered on the page. I (kerrizor) had
previously addressed the bug in: https://gitlab.com/gitlab-org/gitlab-ee/commit/7bc971915bbeadb950bb0e1f13510bf3038229a4
However, an additional exploit was discovered after that was merged.
Working through the issue, we couldn't simply shuffle the order of
filters, due to some implicit assumptions about the order of filters, so
instead we've extracted the logic that sanitizes a Nokogiri-generated
Node object, and applied it to the WikiLinkFilter as well.
On moving filters around:
Once we start moving around filters, we get cascading failures; fix one,
another one crops up. Many of the existing filters in the WikiPipeline
chain seem to assume that other filters have already done their work,
and thus operate on a "transform anything that's left" basis;
WikiFilter, for instance, assumes any link it finds in the markdown
should be prepended with the wiki_base_path.. but if it does that, it
also turns `href="@user"` into `href="/path/to/wiki/@user"`, which the
UserReferenceFilter doesn't see as a user reference it needs to
transform into a user profile link. This is true for all the reference
filters in the WikiPipeline.
2019-07-26 09:41:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tags.each do |tag, opts|
|
|
|
|
context "<#{tag}> tags" do
|
|
|
|
it "removes the unsafe link" do
|
|
|
|
node = opts[:node_to_check].call(opts[:doc])
|
|
|
|
|
|
|
|
expect { object.remove_unsafe_links({ node: node }, remove_invalid_links: true) }
|
|
|
|
.to change { node[opts[:attr]] }
|
|
|
|
|
|
|
|
expect(node[opts[:attr]]).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#safe_protocol?" do
|
|
|
|
let(:doc) { HTML::Pipeline.parse("<a href='#{scheme}alert(1);'>foo</a>") }
|
|
|
|
let(:node) { doc.children.first }
|
2022-08-01 23:09:33 -04:00
|
|
|
let(:uri) { Addressable::URI.parse(node['href']) }
|
Extract SanitizeNodeLink and apply to WikiLinkFilter
The SanitizationFilter was running before the WikiFilter. Since
WikiFilter can modify links, we could see links that _should_ be stopped
by SanatizationFilter being rendered on the page. I (kerrizor) had
previously addressed the bug in: https://gitlab.com/gitlab-org/gitlab-ee/commit/7bc971915bbeadb950bb0e1f13510bf3038229a4
However, an additional exploit was discovered after that was merged.
Working through the issue, we couldn't simply shuffle the order of
filters, due to some implicit assumptions about the order of filters, so
instead we've extracted the logic that sanitizes a Nokogiri-generated
Node object, and applied it to the WikiLinkFilter as well.
On moving filters around:
Once we start moving around filters, we get cascading failures; fix one,
another one crops up. Many of the existing filters in the WikiPipeline
chain seem to assume that other filters have already done their work,
and thus operate on a "transform anything that's left" basis;
WikiFilter, for instance, assumes any link it finds in the markdown
should be prepended with the wiki_base_path.. but if it does that, it
also turns `href="@user"` into `href="/path/to/wiki/@user"`, which the
UserReferenceFilter doesn't see as a user reference it needs to
transform into a user profile link. This is true for all the reference
filters in the WikiPipeline.
2019-07-26 09:41:11 -04:00
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
expect(object.safe_protocol?(scheme)).to be_falsy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|