Skip to content
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

Feat/deep merge #17

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ jobs:
name: package-dist
retention-days: 30
path: packages/vite-plugin-hash-csp/dist
- name: Run Unit Tests
run: pnpm p:test

- name: Install Playwright Browsers
run: npx playwright install --with-deps chromium

- name: Run tests
run: pnpm test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 5

publish:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: npx playwright install --with-deps chromium

- name: Run tests
run: npm run test
run: pnpm test
- uses: actions/upload-artifact@v4
if: always()
with:
Expand Down
24 changes: 21 additions & 3 deletions packages/vite-plugin-hash-csp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,28 @@ export const htmlFilter = createFilter("**.html");
export const mergePolicies = (
defaultPolicy: CSPPolicy,
userPolicy: CSPPolicy | undefined
) => {
): CSPPolicy => {
if (!userPolicy) return defaultPolicy;
// Simple object merge; we might be able to get away without deep merge
return { ...defaultPolicy, ...userPolicy };

const mergedPolicy: CSPPolicy = { ...defaultPolicy };

for (const key in userPolicy as CSPPolicy) {
const _key = key as keyof CSPPolicy;
if (userPolicy.hasOwnProperty(key)) {
const defaultValues = defaultPolicy[_key] || [];
const userValues = userPolicy[_key] || [];

if (Array.isArray(userValues)) {
mergedPolicy[_key] = Array.from(
new Set([...defaultValues, ...userValues])
);
} else {
mergedPolicy[_key] = userValues;
}
}
}

return mergedPolicy;
};

export const parseOutliers = (outliers: Array<Outlier>) => {
Expand Down
36 changes: 36 additions & 0 deletions packages/vite-plugin-hash-csp/tests/policies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, test } from "vitest";
import { CSPPolicy } from "../src/types";
import { mergePolicies } from "../src/utils";
import { DEFAULT_POLICY } from "../src/policy/constants";

describe("Policy Tests", () => {
test("Simple Policy Merge", () => {
const policy: CSPPolicy = {
"frame-src": ["example.com"],
};
const mergedPolicy = mergePolicies(DEFAULT_POLICY, policy);

expect(mergedPolicy).toEqual({
"default-src": ["'self'"],
"img-src": ["'self'", "data:"],
"script-src-elem": ["'self'"],
"style-src-elem": ["'self'"],
"frame-src": ["example.com"],
});
});

test("Deep Policy Merge", () => {
const policy: CSPPolicy = {
"img-src": ["example.com"],
};

const mergedPolicy = mergePolicies(DEFAULT_POLICY, policy);

expect(mergedPolicy).toEqual({
"default-src": ["'self'"],
"img-src": ["'self'", "data:", "example.com"],
"script-src-elem": ["'self'"],
"style-src-elem": ["'self'"],
});
});
});