mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-30 08:35:24 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e33debdf70 |
No files matched your search
@@ -1036,6 +1036,14 @@ class DocumentViewSet(
|
||||
# Bound the IN clauses used for search results and through-table lookups.
|
||||
selection_data_batch_size = 10_000
|
||||
|
||||
def initial(self, request, *args, **kwargs):
|
||||
super().initial(request, *args, **kwargs)
|
||||
# Only ASCII digits can be a document id, reject anything else here, before
|
||||
# any action calls int() on it
|
||||
pk = kwargs.get("pk")
|
||||
if pk is not None and not (pk.isascii() and pk.isdigit()):
|
||||
raise Http404
|
||||
|
||||
def _get_selection_data_for_queryset(
|
||||
self,
|
||||
queryset: QuerySet[Document],
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
import pytest
|
||||
from django.test import Client
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
|
||||
class TestUnmatchedApiPaths:
|
||||
@pytest.mark.parametrize("method", ["get", "post", "put", "patch", "delete"])
|
||||
def test_unmatched_api_path_is_a_json_404(
|
||||
self,
|
||||
rest_api_client: APIClient,
|
||||
method: str,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- An unauthenticated client
|
||||
WHEN:
|
||||
- Any method is sent to an /api/ URL that no route matches
|
||||
THEN:
|
||||
- The response is a JSON 404, not a redirect to the login page
|
||||
"""
|
||||
response = getattr(rest_api_client, method)("/api/this_does_not_exist/")
|
||||
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
assert response["Content-Type"].startswith("application/json")
|
||||
|
||||
def test_unmatched_api_path_is_a_json_404_when_authenticated(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- An authenticated client
|
||||
WHEN:
|
||||
- An /api/ URL that no route matches is requested
|
||||
THEN:
|
||||
- The response is a JSON 404, not the frontend page
|
||||
"""
|
||||
response = admin_client.get("/api/this/is/nested/and/missing/")
|
||||
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
assert response["Content-Type"].startswith("application/json")
|
||||
|
||||
def test_other_paths_still_reach_the_frontend_catch_all(
|
||||
self,
|
||||
client: Client,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- An anonymous browser request
|
||||
WHEN:
|
||||
- A non-API path is requested
|
||||
THEN:
|
||||
- The frontend catch-all still applies, which sends the user to log in
|
||||
"""
|
||||
response = client.get("/documents/")
|
||||
|
||||
assert response.status_code == status.HTTP_302_FOUND
|
||||
assert "login" in response["Location"]
|
||||
|
||||
|
||||
class TestDocumentRouteIds:
|
||||
@pytest.mark.parametrize(
|
||||
"action",
|
||||
[
|
||||
"download",
|
||||
"history",
|
||||
"metadata",
|
||||
"notes",
|
||||
"preview",
|
||||
"root",
|
||||
"share_links",
|
||||
"suggestions",
|
||||
"thumb",
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("pk", ["null", "true", "abc", "0.5", "%20"])
|
||||
def test_non_numeric_document_id_is_a_json_404(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
action: str,
|
||||
pk: str,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- An authenticated client
|
||||
WHEN:
|
||||
- A document sub-route is requested with an id that is not a number
|
||||
THEN:
|
||||
- The response is a JSON 404, not a server error
|
||||
"""
|
||||
response = admin_client.get(f"/api/documents/{pk}/{action}/")
|
||||
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
assert response["Content-Type"].startswith("application/json")
|
||||
@@ -57,6 +57,7 @@ from documents.views import WorkflowTriggerViewSet
|
||||
from documents.views import WorkflowViewSet
|
||||
from documents.views import serve_logo
|
||||
from paperless.consumers import StatusConsumer
|
||||
from paperless.views import ApiNotFoundView
|
||||
from paperless.views import ApplicationConfigurationViewSet
|
||||
from paperless.views import DisconnectSocialAccountView
|
||||
from paperless.views import FaviconView
|
||||
@@ -309,6 +310,8 @@ urlpatterns = [
|
||||
RedirectView.as_view(url="schema/view/"),
|
||||
),
|
||||
*api_router.urls,
|
||||
# Must stay last, anything unmatched under /api/ is a JSON 404
|
||||
re_path(r"^.*$", ApiNotFoundView.as_view(), name="api_not_found"),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -27,6 +27,7 @@ from drf_spectacular.utils import extend_schema_view
|
||||
from rest_framework.authtoken.models import Token
|
||||
from rest_framework.authtoken.views import ObtainAuthToken
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.exceptions import NotFound
|
||||
from rest_framework.exceptions import ValidationError
|
||||
from rest_framework.fields import BooleanField
|
||||
from rest_framework.filters import OrderingFilter
|
||||
@@ -35,6 +36,7 @@ from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.throttling import ScopedRateThrottle
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
from documents.models import PaperlessTask
|
||||
@@ -557,3 +559,18 @@ class SocialAccountProvidersView(GenericAPIView[Any]):
|
||||
]
|
||||
|
||||
return Response(sorted(resp, key=lambda p: p["name"]))
|
||||
|
||||
|
||||
@extend_schema(exclude=True)
|
||||
class ApiNotFoundView(APIView):
|
||||
"""
|
||||
A JSON 404 for any /api/ URL that no other route matches. Without this, those
|
||||
URLs fall through to the frontend catch-all, which answers a redirect to the
|
||||
login page or the single page app, never an error an API client can read.
|
||||
"""
|
||||
|
||||
authentication_classes = ()
|
||||
permission_classes = ()
|
||||
|
||||
def initial(self, request, *args, **kwargs):
|
||||
raise NotFound
|
||||
Reference in new issue
Block a user