Skip to content

Commit

Permalink
Ensuring restricted access to the /learn index directory (#887)
Browse files Browse the repository at this point in the history
* Add a test ensuring correct mode of index dir

* Ensure dir exists with correct mode
  • Loading branch information
krassowski authored Jul 10, 2024
1 parent bc082b5 commit 6b21c03
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/jupyter-ai/jupyter_ai/chat_handlers/learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
IndexMetadata,
)
from jupyter_core.paths import jupyter_data_dir
from jupyter_core.utils import ensure_dir_exists
from langchain.schema import BaseRetriever, Document
from langchain.text_splitter import (
LatexTextSplitter,
Expand Down Expand Up @@ -97,11 +98,12 @@ def __init__(self, *args, **kwargs):
self.metadata = IndexMetadata(dirs=[])
self.prev_em_id = None

if not os.path.exists(INDEX_SAVE_DIR):
os.makedirs(INDEX_SAVE_DIR)

self._ensure_dirs()
self._load()

def _ensure_dirs(self):
ensure_dir_exists(INDEX_SAVE_DIR, mode=0o700)

def _load(self):
"""Loads the vector store."""
if self.index is not None:
Expand Down
21 changes: 21 additions & 0 deletions packages/jupyter-ai/jupyter_ai/tests/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
import os
import stat
from unittest import mock

from jupyter_ai.chat_handlers import learn


class MockLearnHandler(learn.LearnChatHandler):
def __init__(self):
pass


def test_learn_index_permissions(tmp_path):
test_dir = tmp_path / "test"
with mock.patch.object(learn, "INDEX_SAVE_DIR", new=test_dir):
handler = MockLearnHandler()
handler._ensure_dirs()
mode = os.stat(test_dir).st_mode
assert stat.filemode(mode) == "drwx------"


# TODO
# import json

Expand Down

0 comments on commit 6b21c03

Please sign in to comment.