GeezerVison is showing github.com — fix: sort csp directives per w3 spec (#96) · unrolled/secure@3d539f9
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: .github/workflows/test.yaml +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
|
28 | 28 | path: src/github.com/unrolled/secure |
29 | 29 | - uses: golangci/golangci-lint-action@v4 |
30 | 30 | with: |
31 | | - working-directory: src/github.com/7shifts/seven-deploy |
| 31 | + working-directory: src/github.com/unrolled/secure |
Collapse file
Copy file name to clipboardExpand all lines: cspbuilder/builder.go +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | package cspbuilder |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "sort" |
4 | 5 | "strings" |
5 | 6 | ) |
6 | 7 |
|
@@ -62,7 +63,16 @@ func (builder *Builder) MustBuild() string { |
62 | 63 | func (builder *Builder) Build() (string, error) { |
63 | 64 | var sb strings.Builder |
64 | 65 |
|
65 | | - for directive := range builder.Directives { |
| 66 | + // Pull the directive keys out. |
| 67 | + directiveKeys := []string{} |
| 68 | + for key := range builder.Directives { |
| 69 | + directiveKeys = append(directiveKeys, key) |
| 70 | + } |
| 71 | + |
| 72 | + // Sort the policies: https://www.w3.org/TR/CSP3/#framework-policy |
| 73 | + sort.Strings(directiveKeys) |
| 74 | + |
| 75 | + for _, directive := range directiveKeys { |
66 | 76 | if sb.Len() > 0 { |
67 | 77 | sb.WriteString("; ") |
68 | 78 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: cspbuilder/builder_test.go +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,6 +63,7 @@ func TestContentSecurityPolicyBuilder_Build_MultipleDirectives(t *testing.T) { |
63 | 63 | directives map[string]([]string) |
64 | 64 | builder Builder |
65 | 65 | wantParts []string |
| 66 | + wantFull string |
66 | 67 | wantErr bool |
67 | 68 | }{ |
68 | 69 | { |
@@ -86,6 +87,8 @@ func TestContentSecurityPolicyBuilder_Build_MultipleDirectives(t *testing.T) { |
86 | 87 | "trusted-types policy-1 policy-#=_/@.% 'allow-duplicates'", |
87 | 88 | "upgrade-insecure-requests", |
88 | 89 | }, |
| 90 | + |
| 91 | + wantFull: "default-src 'self' example.com *.example.com; frame-ancestors 'self' http://*.example.com; report-to group1; require-trusted-types-for 'script'; sandbox allow-scripts; trusted-types policy-1 policy-#=_/@.% 'allow-duplicates'; upgrade-insecure-requests", |
89 | 92 | }, |
90 | 93 | } |
91 | 94 | for _, tt := range tests { |
@@ -101,6 +104,10 @@ func TestContentSecurityPolicyBuilder_Build_MultipleDirectives(t *testing.T) { |
101 | 104 | return |
102 | 105 | } |
103 | 106 |
|
| 107 | + if got != tt.wantFull { |
| 108 | + t.Errorf("ContentSecurityPolicyBuilder.Build() full = %v, but wanted %v", got, tt.wantFull) |
| 109 | + } |
| 110 | + |
104 | 111 | { |
105 | 112 | startsWithDirective := false |
106 | 113 |
|
|