harden: validate view database names in mapreduce cleanup#9215
harden: validate view database names in mapreduce cleanup#9215l3tchupkt wants to merge 4 commits intoapache:masterfrom
Conversation
|
Heya, thanks for this. I think the correct fix for this is not allowing the creation of such databases in the first place, i.e. validate during Also could you clarify what the regex is designed to match? |
|
@janl Thanks for the feedback. I agree that validating at In this PR, I added validation at Regarding the regex:
Happy to adjust the scope (e.g. limit validation to cleanup only or refine the regex) based on your preference. Best regards, |
Is this applicable to all adapters? |
|
The regex ^[^<>:"|?*]+$ was intended to block dangerous filesystem
characters, but I realize it doesn't sufficiently prevent path traversal
(e.g., ../). A more robust fix will require explicit checks to constrain
names to a safe namespace. Since risks vary between filesystem and
browser-based adapters, I will also look into adapter-specific validation.
I am happy to adjust the PR accordingly.
|
|
@l3tchupkt can you include a test that fails on |
|
Excuse the detour, but I want to put all the pieces on the board to make sure we solve this correctly:
*we’ll keep this in mind for the ongoing SQLite adapter work, cc @Realtin @AlbaHerrerias. |
- Add strict whitelist validation for database names (alphanumeric, underscore, hyphen, max 100 chars) - Prevent path traversal and injection attacks in view operations - Apply validation at both createView and localViewCleanup - Non-breaking: log warnings and skip invalid entries - Add comprehensive test coverage
6b9d31b to
0608b33
Compare
|
@janl Thanks for the detailed explanation, that clarifies the scope a lot. I see that the issue is specific to filesystem-backed adapters, so it makes sense to move the validation into pouchdb-adapter-leveldb rather than handling it in mapreduce. I’ll refactor the fix accordingly by:
I’ll update the PR shortly with these changes. |
- Add strict validation using /^[a-z][a-z0-9_+/-]*$/ - Prevent path traversal and unsafe database identifiers - Fix callback handling to avoid runtime crashes - Separate database name from filesystem path via databaseDir - Add adapter-level tests for validation coverage
Summary of ChangesThis PR adds secure database name validation to the LevelDB adapter, aligned with CouchDB rules. Security
Implementation
Tests
Breaking Change |
Overview
This PR adds validation for database names derived from
info.db_nameand used in mapreduce view cleanup operations.In environments where database names may originate from external input (e.g., multi-tenant applications constructing database names from user identifiers), the current implementation allows potentially unsafe database names to flow through PouchDB's internal metadata system and eventually be used in destructive operations.
Changes:
createView.js: Added validation before constructing
depDbNamefrominfo.db_name. Invalid database names are logged with a warning and skipped.index.js (localViewCleanup): Added validation for view database names before processing them during cleanup. Invalid entries are logged with a warning and skipped.
Both validations use a regex pattern
^[^<>:"|?*]+$that blocks dangerous characters commonly used in path traversal attacks while still allowing valid paths like./data/mydb.This hardening improves safety without introducing breaking changes - existing code continues to work, but potentially unsafe database names are now handled defensively.
Testing recommendations
Existing functionality: Create views and run view cleanup on databases with standard names (alphanumeric, underscores, paths like
./data/mydb). Verify normal operation continues.Edge cases: Test with database names containing the blocked characters
<>:"|?*. These should trigger warnings and be skipped rather than processed.Multi-tenant scenario: In a server environment, verify that database names originating from user input are handled safely during view operations.
Related Issues or Pull Requests
N/A - This is a proactive hardening improvement based on security review feedback.
Checklist
docsfolder