mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-02 17:57:57 -05:00
refac: limit size of version history diffs (#33865)
Diffing two versions of a skill, tool, function or prompt now refuses inputs above 5000 lines per version. All four history diff views share the same check.
This commit is contained in:
1 parent
0df6058503
commit
77959080e3
5 files changed
+19
-34
No files matched your search
@@ -1,12 +1,12 @@
|
||||
"""Immutable snapshots of function configuration; the live row remains Production."""
|
||||
|
||||
import difflib
|
||||
import time
|
||||
import uuid
|
||||
from copy import deepcopy
|
||||
|
||||
from fastapi import HTTPException
|
||||
from open_webui.internal.db import Base, get_async_db_context
|
||||
from open_webui.utils.misc import unified_text_diff
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from sqlalchemy import JSON, BigInteger, Column, Text, select, update
|
||||
|
||||
@@ -147,12 +147,7 @@ def function_diff(before, after):
|
||||
if left.get(key) != right.get(key)
|
||||
}
|
||||
old, new = left.get('content') or '', right.get('content') or ''
|
||||
# splitlines handles a missing final newline and CRLF without breaking the renderer.
|
||||
patch = '\n'.join(
|
||||
difflib.unified_diff(
|
||||
old.splitlines(), new.splitlines(), fromfile='selected.py', tofile='production.py', lineterm=''
|
||||
)
|
||||
)
|
||||
patch = '\n'.join(unified_text_diff(old, new, 'selected.py', 'production.py'))
|
||||
return {
|
||||
'from_id': before.id,
|
||||
'to_id': after.id,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
"""Prompt history model for version tracking."""
|
||||
|
||||
import difflib
|
||||
import time
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
from open_webui.internal.db import Base, get_async_db_context
|
||||
from open_webui.models.users import UserResponse, Users
|
||||
from open_webui.utils.misc import unified_text_diff
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from sqlalchemy import JSON, BigInteger, Column, Index, Text, delete, func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -172,15 +172,7 @@ class PromptHistoryTable:
|
||||
from_content = from_snapshot.get('content', '')
|
||||
to_content = to_snapshot.get('content', '')
|
||||
|
||||
diff_lines = list(
|
||||
difflib.unified_diff(
|
||||
from_content.splitlines(),
|
||||
to_content.splitlines(),
|
||||
fromfile=f'v{from_id[:8]}',
|
||||
tofile=f'v{to_id[:8]}',
|
||||
lineterm='',
|
||||
)
|
||||
)
|
||||
diff_lines = unified_text_diff(from_content, to_content, f'v{from_id[:8]}', f'v{to_id[:8]}')
|
||||
|
||||
return {
|
||||
'from_id': from_id,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
"""Immutable snapshots of tool configuration; the live row remains Production."""
|
||||
|
||||
import difflib
|
||||
import time
|
||||
import uuid
|
||||
from copy import deepcopy
|
||||
|
||||
from fastapi import HTTPException
|
||||
from open_webui.internal.db import Base, get_async_db_context
|
||||
from open_webui.utils.misc import unified_text_diff
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from sqlalchemy import JSON, BigInteger, Column, Text, select, update
|
||||
|
||||
@@ -145,12 +145,7 @@ def tool_diff(before, after):
|
||||
if left.get(key) != right.get(key)
|
||||
}
|
||||
old, new = left.get('content') or '', right.get('content') or ''
|
||||
# splitlines handles a missing final newline and CRLF without breaking the renderer.
|
||||
patch = '\n'.join(
|
||||
difflib.unified_diff(
|
||||
old.splitlines(), new.splitlines(), fromfile='selected.py', tofile='production.py', lineterm=''
|
||||
)
|
||||
)
|
||||
patch = '\n'.join(unified_text_diff(old, new, 'selected.py', 'production.py'))
|
||||
return {
|
||||
'from_id': before.id,
|
||||
'to_id': after.id,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import difflib
|
||||
import json
|
||||
import logging
|
||||
import mimetypes
|
||||
@@ -30,6 +29,7 @@ from open_webui.models.skills import (
|
||||
)
|
||||
from open_webui.utils.access_control import filter_allowed_access_grants, has_permission, validate_access_grants
|
||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||
from open_webui.utils.misc import unified_text_diff
|
||||
from open_webui.utils.skill_files import (
|
||||
MAX_IMPORT_BYTES,
|
||||
file_bytes,
|
||||
@@ -635,15 +635,7 @@ async def diff_skill_file(
|
||||
if any(f and f.get('encoding') for f in files):
|
||||
return {'binary': True}
|
||||
before, after = ((file or {}).get('content', '') for file in files)
|
||||
diff = '\n'.join(
|
||||
difflib.unified_diff(
|
||||
before.splitlines(),
|
||||
after.splitlines(),
|
||||
fromfile=f'{from_id[:7]}/{path}',
|
||||
tofile=f'{to_id[:7]}/{path}',
|
||||
lineterm='',
|
||||
)
|
||||
)
|
||||
diff = '\n'.join(unified_text_diff(before, after, f'{from_id[:7]}/{path}', f'{to_id[:7]}/{path}'))
|
||||
return {
|
||||
'binary': False,
|
||||
'diff': diff,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import collections.abc
|
||||
import difflib
|
||||
import hashlib
|
||||
import ipaddress
|
||||
import logging
|
||||
@@ -15,12 +16,14 @@ from typing import Callable, Optional, Sequence, Union
|
||||
|
||||
import aiohttp
|
||||
import mimeparse
|
||||
from fastapi import HTTPException
|
||||
from open_webui.env import CHAT_STREAM_RESPONSE_CHUNK_MAX_BUFFER_SIZE
|
||||
from open_webui.utils.json_codec import JSONCodec
|
||||
from open_webui.utils.tool_calling import prepare_tool_call
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
SURROGATE_RE = re.compile('[\ud800-\udfff]')
|
||||
MAX_DIFF_LINES = 5000
|
||||
|
||||
|
||||
def deep_update(d, u):
|
||||
@@ -1336,3 +1339,11 @@ def stream_chunks_handler(stream: aiohttp.StreamReader):
|
||||
yield bytes(buffer)
|
||||
|
||||
return yield_safe_stream_chunks()
|
||||
|
||||
|
||||
def unified_text_diff(old: str, new: str, fromfile: str, tofile: str) -> list[str]:
|
||||
# splitlines handles a missing final newline and CRLF without breaking the renderer.
|
||||
old_lines, new_lines = old.splitlines(), new.splitlines()
|
||||
if max(len(old_lines), len(new_lines)) > MAX_DIFF_LINES:
|
||||
raise HTTPException(status_code=413, detail=f'Diff is limited to {MAX_DIFF_LINES} lines per version')
|
||||
return list(difflib.unified_diff(old_lines, new_lines, fromfile=fromfile, tofile=tofile, lineterm=''))
|
||||
Reference in new issue
Block a user