Skip to content

Security helper unit tests - #2103

Open
duderoot wants to merge 3 commits into
DependencyTrack:mainfrom
duderoot:security-helper-unit-tests
Open

duderoot wants to merge 3 commits into
DependencyTrack:mainfrom
duderoot:security-helper-unit-tests

Conversation

@duderoot

Copy link
Copy Markdown

Description

Adds unit tests for the security-relevant pure helpers and an optional, report-only coverage script. No production code is changed.

  • src/shared/utils.test.js: isUrlSaveForRedirect(), the open-redirect guard for the post-login redirect parameter. Covers allowed same-origin paths (including under a non-root context path) and rejection of other origins, protocol-relative and backslash URLs, javascript:/data:/blob:/file: schemes, percent-encoded and whitespace-prefixed variants, disallowed paths and empty input.
  • src/shared/permissions.test.js: hasPermission(), storePermissions() and getPermissions(), including absent, corrupt and non-array stored data.
  • src/shared/common.test.js: the formatters that build HTML with xss-filters. Hostile input (<script>, attribute break-outs, javascript: links) must come out escaped, and ordinary input must render as before.
  • npm run test-coverage: @vitest/coverage-v8 with text and lcov reports in coverage/, and no enforced threshold. coverage/ is now ignored by git, ESLint and Prettier, so a local report no longer breaks npm run eslint. This is a separate commit, so it is easy to drop if unwanted.

Addressed Issue

Closes #2102

Follows up on #1731, which was closed after Vitest, Playwright and the test CI workflows covered most of it.

Additional Details

Effectiveness. Tests for existing code pass immediately, so each group was checked by temporarily breaking the guarded logic and confirming the tests fail. Examples: dropping the origin check, the protocol check or the path allowlist in isUrlSaveForRedirect; inverting or prefix-matching permission checks; removing each xssFilters call or swapping uriInDoubleQuotedAttr for a weaker filter. 28 of 30 such changes are caught. The other two cannot change the result: removing the empty-storage guard in getPermissions (JSON.parse(null) already yields null, which the array guard handles), and loosening the protocol regex (the origin and path checks already restrict the result).

Behaviour documented, not changed:

  • /login and /change-password are accepted redirect targets, because they are in acceptableRootContextPaths.
  • An absolute same-origin URL (https://<host>/dashboard) is rejected, because the allowlist compares the raw input against path prefixes.
  • hasPermission() returns undefined, not false, for arguments that are neither a string nor an array.
  • Umbrella permissions do not imply granular ones (PORTFOLIO_MANAGEMENT does not imply PORTFOLIO_MANAGEMENT_READ) or the other way round.

Test setup. Tests run in Vitest's default node environment without a DOM library. window.location and sessionStorage are stubbed with vi.stubGlobal. @/i18n and @/plugins/toastr are mocked because they access browser globals at import time. common.js link formatters are exercised with a real vue-router using createMemoryHistory.

176 unit tests pass. npm run eslint, npm run prettier and npm run build are clean.

Checklist

Cover isUrlSaveForRedirect against open-redirect inputs (other
origins, protocol-relative and backslash URLs, javascript:, data:, blob:
and file: schemes, encoded and whitespace-prefixed variants, non-root
context paths) and the permission helpers in permissions.js (present,
absent, array, corrupt and non-array session data).

Part of DependencyTrack#1731.

Signed-off-by: duderoot <[email protected]>
Assert that the formatters in common.js that build HTML with
xss-filters escape hostile input in element content and quoted
attributes, neutralise dangerous URL schemes in generated links, and
render ordinary input as before.

Part of DependencyTrack#1731.

Signed-off-by: duderoot <[email protected]>
Add @vitest/coverage-v8, a test-coverage npm script and v8 coverage
configuration that emits text and lcov reports into coverage/. No
thresholds are enforced. Ignore coverage/ in git, eslint and prettier so
a local report does not break the lint and format checks.

Signed-off-by: duderoot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for security-relevant helpers and report test coverage

1 participant