Compare commits

..
13 changed files with 168 additions and 82 deletions

No files matched your search

+4
View File
@@ -1940,6 +1940,10 @@
<context context-type="sourcefile">src/app/components/common/permissions-dialog/permissions-dialog.component.ts</context>
<context context-type="linenumber">45</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/save-view-config-dialog/save-view-config-dialog.component.ts</context>
<context context-type="linenumber">59</context>
</context-group>
</trans-unit>
<trans-unit id="3066660568529853846" datatype="html">
<source>Error retrieving groups</source>
@@ -16,7 +16,7 @@
</pngx-input-select>
<pngx-input-check i18n-title title="Show in sidebar" formControlName="showInSideBar"></pngx-input-check>
<pngx-input-check i18n-title title="Show on dashboard" formControlName="showOnDashboard"></pngx-input-check>
<pngx-permissions-form accordion="true" formControlName="permissions_form"></pngx-permissions-form>
<pngx-permissions-form [users]="users()" accordion="true" formControlName="permissions_form"></pngx-permissions-form>
@if (error()?.filter_rules) {
<div class="alert alert-danger" role="alert">
<h6 class="alert-heading" i18n>Filter rules error occurred while saving this view</h6>
@@ -3,6 +3,7 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { By } from '@angular/platform-browser'
import { NgbActiveModal, NgbModalModule } from '@ng-bootstrap/ng-bootstrap'
import { of } from 'rxjs'
import { User } from 'src/app/data/user'
import { GroupService } from 'src/app/services/rest/group.service'
import { UserService } from 'src/app/services/rest/user.service'
import { CheckComponent } from '../../common/input/check/check.component'
@@ -14,6 +15,7 @@ import { TextComponent } from '../../common/input/text/text.component'
import { SaveViewConfigDialogComponent } from './save-view-config-dialog.component'
describe('SaveViewConfigDialogComponent', () => {
const users = [{ id: 10, username: 'user10' }] as User[]
let component: SaveViewConfigDialogComponent
let fixture: ComponentFixture<SaveViewConfigDialogComponent>
let modal: NgbActiveModal
@@ -25,7 +27,7 @@ describe('SaveViewConfigDialogComponent', () => {
{
provide: UserService,
useValue: {
listAll: () => of({ results: [] }),
listAll: () => of({ results: users }),
},
},
{
@@ -124,6 +126,19 @@ describe('SaveViewConfigDialogComponent', () => {
})
})
it('should list users in the owner selector', () => {
fixture.debugElement
.query(By.css('button[ngbAccordionButton]'))
.nativeElement.click()
fixture.detectChanges()
const permissionsForm = fixture.debugElement.query(
By.directive(PermissionsFormComponent)
)
const ownerSelect = permissionsForm.query(By.directive(SelectComponent))
expect(ownerSelect.componentInstance.items).toEqual(users)
})
it('should support default name', () => {
const saveClickedSpy = jest.spyOn(component.saveClicked, 'emit')
const modalCloseSpy = jest.spyOn(modal, 'close')
@@ -6,6 +6,7 @@ import {
inject,
signal,
} from '@angular/core'
import { toSignal } from '@angular/core/rxjs-interop'
import {
FormControl,
FormGroup,
@@ -13,11 +14,14 @@ import {
ReactiveFormsModule,
} from '@angular/forms'
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { catchError, map, of } from 'rxjs'
import {
DEFAULT_SAVED_VIEW_ICON,
SAVED_VIEW_ICONS,
} from 'src/app/data/saved-view-icons'
import { User } from 'src/app/data/user'
import { UserService } from 'src/app/services/rest/user.service'
import { ToastService } from 'src/app/services/toast.service'
import { CheckComponent } from '../../common/input/check/check.component'
import { PermissionsFormComponent } from '../../common/input/permissions/permissions-form/permissions-form.component'
import { SelectComponent } from '../../common/input/select/select.component'
@@ -38,6 +42,8 @@ import { TextComponent } from '../../common/input/text/text.component'
})
export class SaveViewConfigDialogComponent implements OnInit {
private modal = inject(NgbActiveModal)
private userService = inject(UserService)
private toastService = inject(ToastService)
readonly error = signal(undefined)
readonly buttonsEnabled = signal(true)
readonly defaultName = signal('')
@@ -46,7 +52,16 @@ export class SaveViewConfigDialogComponent implements OnInit {
@Output()
public saveClicked = new EventEmitter()
users: User[]
readonly users = toSignal(
this.userService.listAll().pipe(
map((result) => result.results),
catchError((error) => {
this.toastService.showError($localize`Error retrieving users`, error)
return of([])
})
),
{ initialValue: undefined as User[] }
)
readonly savedViewIcons = SAVED_VIEW_ICONS
setDefaultName(value: string) {
+2 -1
View File
@@ -18,7 +18,8 @@
"es2022",
"dom"
],
"useDefineForClassFields": false
"useDefineForClassFields": false,
"types": ["jest", "node"]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
+45 -3
View File
@@ -3,7 +3,6 @@ from __future__ import annotations
import logging
import re
from fnmatch import fnmatch
from fnmatch import translate as fnmatch_translate
from typing import TYPE_CHECKING
from rest_framework import serializers
@@ -548,6 +547,48 @@ def existing_document_matches_workflow(
return (True, None)
def _glob_to_portable_regex(pattern: str) -> str:
"""
Translate an fnmatch-style glob into an anchored regex which is accepted by
Python, PostgreSQL (ARE), and MariaDB (PCRE).
fnmatch.translate() can't be used, as its output (`(?s:...)\\Z`) uses syntax
PostgreSQL rejects. Supports `*`, `?`, `[seq]` and `[!seq]`.
"""
out: list[str] = []
i = 0
n = len(pattern)
while i < n:
c = pattern[i]
i += 1
if c == "*":
out.append(".*")
elif c == "?":
out.append(".")
elif c == "[":
j = i
if j < n and pattern[j] == "!":
j += 1
if j < n and pattern[j] == "]":
j += 1
while j < n and pattern[j] != "]":
j += 1
if j >= n:
# No closing bracket, treat as a literal
out.append(r"\[")
else:
body = pattern[i:j]
i = j + 1
negate = body.startswith("!")
if negate:
body = body[1:]
body = "".join(f"\\{ch}" if ch in "\\[^" else ch for ch in body)
out.append(f"[{'^' if negate else ''}{body}]")
else:
out.append(re.escape(c))
return f"^{''.join(out)}$"
def prefilter_documents_by_workflowtrigger(
documents: QuerySet[Document],
trigger: WorkflowTrigger,
@@ -628,8 +669,9 @@ def prefilter_documents_by_workflowtrigger(
documents = documents.annotate(**annotations).filter(custom_field_q)
if trigger.filter_filename:
regex = fnmatch_translate(trigger.filter_filename).lstrip("^").rstrip("$")
documents = documents.filter(original_filename__iregex=regex)
documents = documents.filter(
original_filename__iregex=_glob_to_portable_regex(trigger.filter_filename),
)
return documents
@@ -0,0 +1,62 @@
import fnmatch
import re
import pytest
from documents.matching import _glob_to_portable_regex
@pytest.mark.parametrize(
("pattern", "expected"),
[
pytest.param("", r"^$", id="empty"),
pytest.param("abc", r"^abc$", id="literal"),
pytest.param("*", r"^.*$", id="star"),
pytest.param("?", r"^.$", id="question"),
pytest.param("*.pdf", r"^.*\.pdf$", id="star-escapes-dot"),
pytest.param("[abc]", r"^[abc]$", id="class"),
pytest.param("[a-z]", r"^[a-z]$", id="class-range"),
pytest.param("[!abc]", r"^[^abc]$", id="negated-class"),
pytest.param("[]a]", r"^[]a]$", id="leading-close-bracket-is-member"),
pytest.param("[!]a]", r"^[^]a]$", id="negated-leading-close-bracket"),
pytest.param("[a^b]", r"^[a\^b]$", id="caret-escaped-in-class"),
pytest.param("[a[b]", r"^[a\[b]$", id="open-bracket-escaped-in-class"),
pytest.param("[a\\b]", r"^[a\\b]$", id="backslash-escaped-in-class"),
pytest.param("[abc", r"^\[abc$", id="unclosed-class-literal"),
pytest.param("[", r"^\[$", id="lone-open-bracket"),
pytest.param("[!", r"^\[!$", id="unclosed-negation-literal"),
pytest.param("[]", r"^\[\]$", id="unclosed-with-only-close-bracket"),
pytest.param("a[0-9]?*", r"^a[0-9]..*$", id="mixed"),
],
)
def test_translation(pattern: str, expected: str) -> None:
"""
GIVEN:
- A glob pattern
WHEN:
- It is translated to a portable regex
THEN:
- The exact anchored regex is produced
"""
assert _glob_to_portable_regex(pattern) == expected
@pytest.mark.parametrize(
"pattern",
["*", "?", "*.pdf", "a?c", "[abc]x", "[!abc]x", "[]a]", "[!]a]", "[a-c]*", "[a"],
)
@pytest.mark.parametrize(
"name",
["", "a", "abc", "x", "]", "]x", "file.pdf", "ax", "bx", "dx", "[a"],
)
def test_agrees_with_fnmatch(pattern: str, name: str) -> None:
"""
GIVEN:
- A glob pattern and a name
WHEN:
- Matched via the portable regex and via stdlib fnmatch
THEN:
- Both agree
"""
regex = _glob_to_portable_regex(pattern)
assert bool(re.search(regex, name)) == fnmatch.fnmatchcase(name, pattern)
-23
View File
@@ -1,23 +0,0 @@
from rest_framework import serializers
OBFUSCATED_PASSWORD_MASK = "*" * 10
def is_obfuscated_password(value: str) -> bool:
"""
True if the value is only mask characters (or empty), meaning a client echoed
the placeholder back rather than supplying a new secret
"""
return value.replace("*", "") == ""
class ObfuscatedPasswordField(serializers.CharField):
"""
Sends a constant mask string instead of the password in the clear
"""
def to_representation(self, value) -> str:
return OBFUSCATED_PASSWORD_MASK
def to_internal_value(self, data):
return data
+3 -4
View File
@@ -20,19 +20,18 @@ from rest_framework import serializers
from rest_framework.authtoken.serializers import AuthTokenSerializer
from paperless.config import OCR_RESERVED_USER_ARGS
from paperless.fields import ObfuscatedPasswordField
from paperless.fields import is_obfuscated_password
from paperless.models import ApplicationConfiguration
from paperless.network import validate_outbound_http_url
from paperless.validators import reject_dangerous_svg
from paperless.validators import validate_raster_image
from paperless_mail.serialisers import ObfuscatedPasswordField
logger = logging.getLogger("paperless.settings")
class PasswordValidationMixin:
def _has_real_password(self, value: str | None) -> bool:
return bool(value) and not is_obfuscated_password(value)
return bool(value) and value.replace("*", "") != ""
def validate_password(self, value: str) -> str:
if not self._has_real_password(value):
@@ -286,7 +285,7 @@ class ApplicationConfigurationSerializer(
if data[field] == "":
data[field] = None
# Not a real value, don't overwrite the stored one
elif is_obfuscated_password(data[field]):
elif len(data[field].replace("*", "")) == 0:
del data[field]
return super().run_validation(data)
@@ -1,39 +0,0 @@
import pytest
from paperless.fields import OBFUSCATED_PASSWORD_MASK
from paperless.fields import ObfuscatedPasswordField
from paperless.fields import is_obfuscated_password
class TestObfuscatedPasswordField:
@pytest.mark.parametrize(
"secret",
[
"a",
"short",
"exactly10!",
"a-much-longer-secret-that-should-not-leak-its-length",
],
)
def test_representation_is_constant_regardless_of_length(self, secret: str) -> None:
assert (
ObfuscatedPasswordField().to_representation(secret)
== OBFUSCATED_PASSWORD_MASK
)
class TestIsObfuscatedPassword:
@pytest.mark.parametrize(
("value", "expected"),
[
("", True),
("*", True),
("******", True),
(OBFUSCATED_PASSWORD_MASK, True),
("real-secret", False),
("***x***", False),
(" ", False),
],
)
def test_detects_placeholder(self, value: str, *, expected: bool) -> None:
assert is_obfuscated_password(value) is expected
+1 -2
View File
@@ -41,7 +41,6 @@ from documents.models import PaperlessTask
from documents.permissions import PaperlessModelPermissions
from documents.permissions import PaperlessObjectPermissions
from documents.tasks import llmindex_index
from paperless.fields import is_obfuscated_password
from paperless.filters import GroupFilterSet
from paperless.filters import UserFilterSet
from paperless.models import ApplicationConfiguration
@@ -261,7 +260,7 @@ class ProfileView(GenericAPIView[Any]):
user = self.request.user if hasattr(self.request, "user") else None
password = serializer.validated_data.pop("password", None)
if password and not is_obfuscated_password(password):
if password and password.replace("*", ""):
user.set_password(password)
user.save()
+15 -4
View File
@@ -8,13 +8,23 @@ from documents.serialisers import CorrespondentField
from documents.serialisers import DocumentTypeField
from documents.serialisers import OwnedObjectSerializer
from documents.serialisers import TagsField
from paperless.fields import ObfuscatedPasswordField
from paperless.fields import is_obfuscated_password
from paperless_mail.models import MailAccount
from paperless_mail.models import MailRule
from paperless_mail.models import ProcessedMail
class ObfuscatedPasswordField(serializers.CharField):
"""
Sends *** string instead of password in the clear
"""
def to_representation(self, value) -> str:
return "*" * max(10, len(value))
def to_internal_value(self, data):
return data
class MailAccountSerializer(OwnedObjectSerializer):
password = ObfuscatedPasswordField()
imap_port = serializers.IntegerField(required=True, allow_null=False)
@@ -40,8 +50,9 @@ class MailAccountSerializer(OwnedObjectSerializer):
]
def update(self, instance, validated_data):
if "password" in validated_data and is_obfuscated_password(
validated_data["password"],
if (
"password" in validated_data
and len(validated_data.get("password").replace("*", "")) == 0
):
validated_data.pop("password")
super().update(instance, validated_data)
+3 -3
View File
@@ -30,7 +30,6 @@ from documents.permissions import PaperlessObjectPermissions
from documents.permissions import has_perms_owner_aware
from documents.permissions import permitted_object_ids
from documents.views import PassUserMixin
from paperless.fields import is_obfuscated_password
from paperless.views import StandardPagination
from paperless_mail.filters import ProcessedMailFilterSet
from paperless_mail.mail import MailError
@@ -127,8 +126,9 @@ class MailAccountViewSet(PassUserMixin, ModelViewSet[MailAccount]):
return HttpResponseForbidden("Insufficient permissions")
# account exists, use the password from there instead of ***
if existing_account is not None and is_obfuscated_password(
serializer.validated_data.get("password"),
if (
len(serializer.validated_data.get("password").replace("*", "")) == 0
and existing_account is not None
):
serializer.validated_data["password"] = existing_account.password
serializer.validated_data["account_type"] = existing_account.account_type