File tree Expand file tree Collapse file tree 3 files changed +24
-4
lines changed Expand file tree Collapse file tree 3 files changed +24
-4
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 || source =~ PORT_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,13 @@ 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 = URI ( source . sub ( PORT_WILDCARD_REGEX , "" ) )
218
+ # If host is nil the given source doesn't contain a scheme
219
+ # e.g. for `example.org:443` it would return `example.org` as the scheme
220
+ # which is of course incorrect
221
+ uri . scheme if uri . host
222
+ end
215
223
end
216
224
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,16 @@ module SecureHeaders
56
56
expect ( csp . value ) . to eq ( "default-src *.example.org" )
57
57
end
58
58
59
+ it "minifies overlapping port wildcards" do
60
+ csp = ContentSecurityPolicy . new ( default_src : %w( example.org example.org:* example.org:443 https://example.org:80 ) )
61
+ expect ( csp . value ) . to eq ( "default-src example.org example.org:*" )
62
+ end
63
+
64
+ it "allows for port wildcards" do
65
+ csp = ContentSecurityPolicy . new ( connect_src : %w( ws://localhost:* ) )
66
+ expect ( csp . value ) . to eq ( "connect-src ws://localhost:*" )
67
+ end
68
+
59
69
it "removes http/s schemes from hosts" do
60
70
csp = ContentSecurityPolicy . new ( default_src : %w( https://example.org ) )
61
71
expect ( csp . value ) . to eq ( "default-src example.org" )
@@ -102,7 +112,8 @@ module SecureHeaders
102
112
end
103
113
104
114
it "deduplicates any source expressions" do
105
- csp = ContentSecurityPolicy . new ( default_src : %w( example.org example.org example.org ) )
115
+ src = %w( example.org example.org http://example.org https://example.org )
116
+ csp = ContentSecurityPolicy . new ( default_src : src )
106
117
expect ( csp . value ) . to eq ( "default-src example.org" )
107
118
end
108
119
You can’t perform that action at this time.
0 commit comments