- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
WIP: rewrite using classes #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            mcansh
  wants to merge
  3
  commits into
  dev
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
logan/classes
  
      
      
   
  
    
  
  
  
 
  
      
    base: dev
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { createSecureHeaders } from "./helmet"; | ||
| import { mergeHeaders } from "./utils"; | ||
| import { SecurityHeaders } from "./v2"; | ||
|  | ||
| describe("mergeHeaders", () => { | ||
| it("merges headers", () => { | ||
| let secureHeaders = createSecureHeaders({ | ||
| "Content-Security-Policy": { "default-src": ["'self'"] }, | ||
| }); | ||
|  | ||
| let responseHeaders = new Headers({ | ||
| "Content-Type": "text/html", | ||
| "x-foo": "bar", | ||
| }); | ||
|  | ||
| let merged = mergeHeaders(responseHeaders, secureHeaders); | ||
|  | ||
| expect(merged.get("Content-Type")).toBe("text/html"); | ||
| expect(merged.get("x-foo")).toBe("bar"); | ||
| expect(merged.get("Content-Security-Policy")).toBe("default-src 'self'"); | ||
| }); | ||
|  | ||
| it("throws if the argument is not an object", () => { | ||
| // @ts-expect-error | ||
| expect(() => mergeHeaders("foo")).toThrowErrorMatchingInlineSnapshot( | ||
| `[TypeError: All arguments must be of type object]`, | ||
| ); | ||
| }); | ||
|  | ||
| it("overrides existing headers", () => { | ||
| let secureHeaders = createSecureHeaders({ | ||
| "Content-Security-Policy": { "default-src": ["'self'"] }, | ||
| }); | ||
|  | ||
| let responseHeaders = new Headers({ | ||
| "Content-Security-Policy": "default-src 'none'", | ||
| }); | ||
|  | ||
| let merged1 = mergeHeaders(responseHeaders, secureHeaders); | ||
| let merged2 = mergeHeaders(secureHeaders, responseHeaders); | ||
|  | ||
| expect(merged1.get("Content-Security-Policy")).toBe("default-src 'self'"); | ||
| expect(merged2.get("Content-Security-Policy")).toBe("default-src 'none'"); | ||
| }); | ||
|  | ||
| it('keeps all "Set-Cookie" headers', () => { | ||
| let headers1 = new Headers({ "Set-Cookie": "foo=bar" }); | ||
| let headers2 = new Headers({ "Set-Cookie": "baz=qux" }); | ||
|  | ||
| let merged = mergeHeaders(headers1, headers2); | ||
|  | ||
| expect(merged.getSetCookie()).toStrictEqual(["foo=bar", "baz=qux"]); | ||
| }); | ||
|  | ||
| it("allows using just one argument", () => { | ||
| let headers = new Headers({ "Content-Type": "text/plain" }); | ||
|  | ||
| let merged = mergeHeaders(headers); | ||
|  | ||
| expect(merged.get("Content-Type")).toBe("text/plain"); | ||
| }); | ||
|  | ||
| it("merges headers when using SecurityHeaders class", () => { | ||
| let secureHeaders = new SecurityHeaders({ | ||
| "Content-Security-Policy": { "default-src": ["'self'"] }, | ||
| }); | ||
|  | ||
| let responseHeaders = new Headers({ | ||
| "Content-Type": "text/html", | ||
| "x-foo": "bar", | ||
| }); | ||
|  | ||
| let merged = mergeHeaders(responseHeaders, secureHeaders.toHeaders()); | ||
|  | ||
| expect(merged.get("Content-Type")).toBe("text/html"); | ||
| expect(merged.get("x-foo")).toBe("bar"); | ||
| expect(merged.get("Content-Security-Policy")).toBe("default-src 'self'"); | ||
| }); | ||
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            101 changes: 101 additions & 0 deletions
          
          101 
        
  packages/http-helmet/src/v2/content-security-policy.spec.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { SELF } from "#src/utils.ts"; | ||
| import { expect, it } from "vitest"; | ||
| import { ContentSecurityPolicy } from "./content-security-policy"; | ||
|  | ||
| it("creates a CSP policy with a single directive", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| csp.set("default-src", [SELF]); | ||
| expect(csp.toString()).toBe("default-src 'self'"); | ||
| }); | ||
|  | ||
| it("creates a CSP policy with no directives", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| expect(csp.toString()).toBe(""); | ||
| }); | ||
|  | ||
| it("creates a CSP policy with multiple directives", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| csp.set("default-src", [SELF]); | ||
| csp.set("script-src", ["https://example.com"]); | ||
| expect(csp.toString()).toBe( | ||
| "default-src 'self'; script-src https://example.com", | ||
| ); | ||
| }); | ||
|  | ||
| it("creates a CSP policy with multiple values in a directive", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| csp.set("script-src", [SELF, "https://example.com"]); | ||
| expect(csp.toString()).toBe("script-src 'self' https://example.com"); | ||
| }); | ||
|  | ||
| it("can parse a CSP string", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| csp.parse("default-src 'self'; script-src https://example.com"); | ||
| expect(csp.get("default-src")).toEqual(["'self'"]); | ||
| expect(csp.get("script-src")).toEqual(["https://example.com"]); | ||
| }); | ||
|  | ||
| it("handles `upgrade-insecure-requests` directive", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| csp.set("upgrade-insecure-requests", []); | ||
| expect(csp.toString()).toBe("upgrade-insecure-requests"); | ||
| }); | ||
|  | ||
| it("handles upgradeInsecureRequests method", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| csp.upgradeInsecureRequests(); | ||
| expect(csp.toString()).toBe("upgrade-insecure-requests"); | ||
| }); | ||
|  | ||
| it("can call `append` multiple times for the same key", () => { | ||
| let csp = new ContentSecurityPolicy(); | ||
| csp.append("default-src", [SELF]); | ||
| csp.append("default-src", ["https://example.com"]); | ||
| expect(csp.toString()).toBe("default-src 'self' https://example.com"); | ||
| }); | ||
|  | ||
| it("can create csp with predefined directives", () => { | ||
| let csp = new ContentSecurityPolicy({ | ||
| "default-src": [SELF], | ||
| "script-src": ["https://example.com"], | ||
| }); | ||
| expect(csp.toString()).toBe( | ||
| "default-src 'self'; script-src https://example.com", | ||
| ); | ||
| }); | ||
|  | ||
| it("can create csp with predefined policy string", () => { | ||
| let csp = new ContentSecurityPolicy( | ||
| "default-src 'self'; script-src https://example.com", | ||
| ); | ||
| expect(csp.toString()).toBe( | ||
| "default-src 'self'; script-src https://example.com", | ||
| ); | ||
| }); | ||
|  | ||
| it("allows and filters out `undefined` values", () => { | ||
| let csp = new ContentSecurityPolicy({ | ||
| "connect-src": [undefined, "'self'", undefined], | ||
| }); | ||
|  | ||
| expect(csp.toString()).toMatchInlineSnapshot(`"connect-src 'self'"`); | ||
| }); | ||
|  | ||
| it("allows there to be no define values for a csp key", () => { | ||
| let csp = new ContentSecurityPolicy({ | ||
| "base-uri": [undefined], | ||
| "default-src": ["'none'"], | ||
| }); | ||
| expect(csp.toString()).toBe("default-src 'none'"); | ||
| }); | ||
|  | ||
| it("throws an error if the value is reserved, but not properly quoted", () => { | ||
| expect( | ||
| () => | ||
| new ContentSecurityPolicy({ | ||
| "default-src": ["self", "https://example.com"], | ||
| }), | ||
| ).toThrowErrorMatchingInlineSnapshot( | ||
| `[ContentSecurityPolicyError: reserved keyword self must be quoted.]`, | ||
| ); | ||
| }); | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import path
"#src/utils.ts"uses a path alias that doesn't seem to be configured for this package inpackage.json. While this might work in your test environment, it can cause issues with tooling and build processes that don't know how to resolve it.It's recommended to use a relative path to make the import resolution explicit and robust.