File tree Expand file tree Collapse file tree 3 files changed +13
-3
lines changed Expand file tree Collapse file tree 3 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -154,10 +154,10 @@ def reject_all_values_if_none(source_list)
154
154
# e.g. *.github.jpy.wang asdf.github.com becomes *.github.jpy.wang
155
155
def dedup_source_list ( sources )
156
156
sources = sources . uniq
157
- wild_sources = sources . select { |source | source =~ STAR_REGEXP }
157
+ wild_sources = sources . select { |source | source =~ DOMAIN_WILDCARD_REGEX }
158
158
159
159
if wild_sources . any?
160
- schemes = sources . map { |source | [ source , URI ( source ) . scheme ] } . to_h
160
+ schemes = sources . map { |source | [ source , source_scheme ( source ) ] } . to_h
161
161
sources . reject do |source |
162
162
!wild_sources . include? ( source ) &&
163
163
wild_sources . any? { |pattern | schemes [ pattern ] == schemes [ source ] && File . fnmatch ( pattern , source ) }
@@ -212,5 +212,9 @@ def strip_source_schemes(source_list)
212
212
def symbol_to_hyphen_case ( sym )
213
213
sym . to_s . tr ( "_" , "-" )
214
214
end
215
+
216
+ def source_scheme ( source )
217
+ URI ( source . sub ( PORT_WILDCARD_REGEX , '' ) ) . scheme
218
+ end
215
219
end
216
220
end
Original file line number Diff line number Diff line change @@ -152,7 +152,8 @@ def self.included(base)
152
152
153
153
FETCH_SOURCES = ALL_DIRECTIVES - NON_FETCH_SOURCES - NON_SOURCE_LIST_SOURCES
154
154
155
- STAR_REGEXP = Regexp . new ( Regexp . escape ( STAR ) )
155
+ DOMAIN_WILDCARD_REGEX = /(?<=\A |[^:])\* /
156
+ PORT_WILDCARD_REGEX = /:\* /
156
157
HTTP_SCHEME_REGEX = %r{\A https?://}
157
158
158
159
WILDCARD_SOURCES = [
Original file line number Diff line number Diff line change @@ -56,6 +56,11 @@ module SecureHeaders
56
56
expect ( csp . value ) . to eq ( "default-src *.example.org" )
57
57
end
58
58
59
+ it "allows for port wildcards" do
60
+ csp = ContentSecurityPolicy . new ( connect_src : %w( ws://localhost:* ) )
61
+ expect ( csp . value ) . to eq ( "connect-src ws://localhost:*" )
62
+ end
63
+
59
64
it "removes http/s schemes from hosts" do
60
65
csp = ContentSecurityPolicy . new ( default_src : %w( https://example.org ) )
61
66
expect ( csp . value ) . to eq ( "default-src example.org" )
You can’t perform that action at this time.
0 commit comments