Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
baed947
Secure CLI history files with restrictive permissions on multi-user s…
AndrewAsseily Jan 21, 2026
5d1bb38
Add test coverage for history file permission changes
AndrewAsseily Jan 21, 2026
d827d8a
Restrict permissions for telemetry cache directory and session database
AndrewAsseily Jan 21, 2026
4c5b866
Restrict permissions for autoprompt history directory and file
AndrewAsseily Jan 21, 2026
d7ab3bc
Use pytest marker for skip_if_windows in test_history.py
AndrewAsseily Jan 22, 2026
a0f35ff
Skip permission tightening on Windows (relies on ACL inheritance)
AndrewAsseily Jan 22, 2026
050e6f6
Fix Windows test failures for permission changes
AndrewAsseily Jan 22, 2026
184ce33
Simplify DatabaseConnection guard to check only for :memory:
AndrewAsseily Jan 22, 2026
89650f0
Skip permission-checking test on Windows to avoid os.getuid() error
AndrewAsseily Jan 22, 2026
b724196
Add Changelog entry
AndrewAsseily Jan 22, 2026
d1ad68c
Merge branch 'v2' into fix/history-file-permissions
AndrewAsseily Jan 23, 2026
5ce67cf
Address review feedback: simplify Windows handling, fix test patterns…
AndrewAsseily Jan 23, 2026
4548f1e
Address review feedback: remove UID checks, add logging to botocore
AndrewAsseily Jan 23, 2026
55a6248
Merge branch 'v2' into fix/history-file-permissions
AndrewAsseily Jan 23, 2026
6f072ba
Remove AttributeError from exception handling after removing getuid c…
AndrewAsseily Jan 26, 2026
1f45997
Remove os.stat mock from test after removing UID check from code
AndrewAsseily Jan 26, 2026
9c3863d
Merge branch 'v2' into fix/history-file-permissions
AndrewAsseily Jan 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/enhancement-clihistory-87056.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "enhancement",
"category": "cli-history",
"description": "Create local history files with specific permissions"
}
13 changes: 11 additions & 2 deletions awscli/autoprompt/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ def load_history_strings(self):
def store_string(self, string):
history = {'version': self.HISTORY_VERSION, 'commands': []}
try:
dir_path = os.path.dirname(self.filename)
if os.path.exists(self.filename):
with open(self.filename) as f:
history = json.load(f)
elif not os.path.exists(os.path.dirname(self.filename)):
os.makedirs(os.path.dirname(self.filename))
if not os.path.exists(dir_path):
os.makedirs(dir_path)
try:
os.chmod(dir_path, 0o700)
except OSError as e:
LOG.debug('Unable to set directory permissions: %s', e)
history['commands'].append(string)
history['commands'] = history['commands'][-self._max_commands :]
with open(self.filename, 'w') as f:
json.dump(history, f)
try:
os.chmod(self.filename, 0o600)
except OSError as e:
LOG.debug('Unable to set file permissions: %s', e)
except Exception:
LOG.debug('Exception on loading prompt history:', exc_info=True)

Expand Down
4 changes: 4 additions & 0 deletions awscli/botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3943,6 +3943,10 @@ def __setitem__(self, cache_key, value):
)
if not os.path.isdir(self._working_dir):
os.makedirs(self._working_dir)
try:
os.chmod(self._working_dir, 0o700)
except OSError as e:
logger.debug('Unable to set directory permissions: %s', e)
with os.fdopen(
os.open(full_key, os.O_WRONLY | os.O_CREAT, 0o600), 'w'
) as f:
Expand Down
10 changes: 8 additions & 2 deletions awscli/customizations/history/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ def attach_history_handler(session, parsed_args, **kwargs):
history_filename = os.environ.get(
HISTORY_FILENAME_ENV_VAR, DEFAULT_HISTORY_FILENAME
)
if not os.path.isdir(os.path.dirname(history_filename)):
os.makedirs(os.path.dirname(history_filename))

history_dir = os.path.dirname(history_filename)
if not os.path.isdir(history_dir):
os.makedirs(history_dir)
try:
os.chmod(history_dir, 0o700)
except OSError as e:
LOG.debug('Unable to set directory permissions: %s', e)

connection = DatabaseConnection(history_filename)
writer = DatabaseRecordWriter(connection)
Expand Down
10 changes: 10 additions & 0 deletions awscli/customizations/history/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import datetime
import json
import logging
import os
import threading
import time
import uuid
Expand All @@ -37,6 +38,15 @@ class DatabaseConnection:
_ENABLE_WAL = 'PRAGMA journal_mode=WAL'

def __init__(self, db_filename):
# Skip file operations for in-memory databases
if db_filename != ':memory:':
if not os.path.exists(db_filename):
# Create file so we can set permissions before sqlite opens it
open(db_filename, 'a').close()
try:
os.chmod(db_filename, 0o600)
except OSError as e:
LOG.debug('Unable to set file permissions: %s', e)
self._connection = sqlite3.connect(
db_filename, check_same_thread=False, isolation_level=None
)
Expand Down
31 changes: 25 additions & 6 deletions awscli/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import io
import logging
import os
import sqlite3
import sys
Expand All @@ -28,6 +29,8 @@
from awscli.compat import is_windows
from awscli.utils import add_component_to_user_agent_extra

LOG = logging.getLogger(__name__)

_CACHE_DIR = Path.home() / '.aws' / 'cli' / 'cache'
_DATABASE_FILENAME = 'session.db'
_SESSION_LENGTH_SECONDS = 60 * 30
Expand Down Expand Up @@ -77,12 +80,15 @@ class CLISessionDatabaseConnection:
_ENABLE_WAL = 'PRAGMA journal_mode=WAL'

def __init__(self, connection=None):
self._connection = connection or sqlite3.connect(
_CACHE_DIR / _DATABASE_FILENAME,
check_same_thread=False,
isolation_level=None,
)
self._ensure_cache_dir()
self._connection = connection
if self._connection is None:
self._ensure_cache_dir()
self._ensure_database_file()
self._connection = sqlite3.connect(
_CACHE_DIR / _DATABASE_FILENAME,
check_same_thread=False,
isolation_level=None,
)
self._ensure_database_setup()

def execute(self, query, *parameters):
Expand All @@ -96,6 +102,19 @@ def execute(self, query, *parameters):

def _ensure_cache_dir(self):
_CACHE_DIR.mkdir(parents=True, exist_ok=True)
try:
os.chmod(_CACHE_DIR, 0o700)
except OSError as e:
LOG.debug('Unable to set directory permissions: %s', e)

def _ensure_database_file(self):
db_path = _CACHE_DIR / _DATABASE_FILENAME
if not db_path.exists():
open(db_path, 'a').close()
try:
os.chmod(db_path, 0o600)
except OSError as e:
LOG.debug('Unable to set file permissions: %s', e)

def _ensure_database_setup(self):
self._create_session_table()
Expand Down
69 changes: 69 additions & 0 deletions tests/functional/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import os
import sqlite3
import stat
from pathlib import Path
from unittest.mock import MagicMock, PropertyMock, patch

import pytest
Expand All @@ -27,6 +30,7 @@
CLISessionOrchestrator,
add_session_id_component_to_user_agent_extra,
)
from awscli.testutils import FileCreator
from tests.markers import skip_if_windows


Expand Down Expand Up @@ -130,6 +134,71 @@ def execute(self, query, *parameters):
assert cursor.fetchall() == []


@skip_if_windows
class TestCLISessionDatabaseConnectionPermissions:
def setup_method(self):
self.files = FileCreator()

def teardown_method(self):
self.files.remove_all()

def test_create_directory_with_secure_permissions(self):
cache_dir = self.files.full_path('cache')
cache_path = Path(cache_dir)

with patch('awscli.telemetry._CACHE_DIR', cache_path):
with patch('awscli.telemetry._DATABASE_FILENAME', 'session.db'):
conn = CLISessionDatabaseConnection()
conn._connection.close()

assert os.path.isdir(cache_dir)
dir_mode = stat.S_IMODE(os.stat(cache_dir).st_mode)
assert dir_mode == 0o700

def test_tighten_existing_directory_permissions(self):
cache_dir = self.files.full_path('cache')
os.makedirs(cache_dir, mode=0o755)
cache_path = Path(cache_dir)

with patch('awscli.telemetry._CACHE_DIR', cache_path):
with patch('awscli.telemetry._DATABASE_FILENAME', 'session.db'):
conn = CLISessionDatabaseConnection()
conn._connection.close()

dir_mode = stat.S_IMODE(os.stat(cache_dir).st_mode)
assert dir_mode == 0o700

def test_create_database_file_with_secure_permissions(self):
cache_dir = self.files.full_path('cache')
db_file = os.path.join(cache_dir, 'session.db')
cache_path = Path(cache_dir)

with patch('awscli.telemetry._CACHE_DIR', cache_path):
with patch('awscli.telemetry._DATABASE_FILENAME', 'session.db'):
conn = CLISessionDatabaseConnection()
conn._connection.close()

assert os.path.isfile(db_file)
file_mode = stat.S_IMODE(os.stat(db_file).st_mode)
assert file_mode == 0o600

def test_tighten_existing_database_file_permissions(self):
cache_dir = self.files.full_path('cache')
os.makedirs(cache_dir, mode=0o700)
db_file = os.path.join(cache_dir, 'session.db')
open(db_file, 'a').close()
os.chmod(db_file, 0o644)
cache_path = Path(cache_dir)

with patch('awscli.telemetry._CACHE_DIR', cache_path):
with patch('awscli.telemetry._DATABASE_FILENAME', 'session.db'):
conn = CLISessionDatabaseConnection()
conn._connection.close()

file_mode = stat.S_IMODE(os.stat(db_file).st_mode)
assert file_mode == 0o600


class TestCLISessionDatabaseWriter:
def test_write(self, session_writer, session_reader, session_sweeper):
session_writer.write(
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/autoprompt/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import json
import os
import shutil
import stat
import tempfile

import pytest
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.completion import Completion
from prompt_toolkit.document import Document
Expand All @@ -23,6 +25,7 @@

from awscli.autoprompt.history import HistoryCompleter, HistoryDriver
from awscli.testutils import mock, unittest
from tests.markers import skip_if_windows


class TestHistoryCompleter(unittest.TestCase):
Expand Down Expand Up @@ -190,3 +193,60 @@ def test_handle_io_errors(self, file_history_mock):
history_driver = HistoryDriver(self.filename)
file_history_mock.store_string.side_effect = IOError
history_driver.store_string('aws dynamodb create-table')


@pytest.fixture
def temp_dir():
dirname = tempfile.mkdtemp()
yield dirname
shutil.rmtree(dirname)


@skip_if_windows
def test_create_directory_with_secure_permissions(temp_dir):
subdir = os.path.join(temp_dir, 'newdir')
filename = os.path.join(subdir, 'prompt_history.json')
history_driver = HistoryDriver(filename)
history_driver.store_string('aws ec2 describe-instances')

assert os.path.isdir(subdir)
dir_mode = stat.S_IMODE(os.stat(subdir).st_mode)
assert dir_mode == 0o700


@skip_if_windows
def test_tighten_existing_directory_permissions(temp_dir):
subdir = os.path.join(temp_dir, 'existingdir')
os.makedirs(subdir, mode=0o755)
filename = os.path.join(subdir, 'prompt_history.json')

history_driver = HistoryDriver(filename)
history_driver.store_string('aws ec2 describe-instances')

dir_mode = stat.S_IMODE(os.stat(subdir).st_mode)
assert dir_mode == 0o700


@skip_if_windows
def test_create_file_with_secure_permissions(temp_dir):
filename = os.path.join(temp_dir, 'prompt_history.json')
history_driver = HistoryDriver(filename)
history_driver.store_string('aws ec2 describe-instances')

assert os.path.isfile(filename)
file_mode = stat.S_IMODE(os.stat(filename).st_mode)
assert file_mode == 0o600


@skip_if_windows
def test_tighten_existing_file_permissions(temp_dir):
filename = os.path.join(temp_dir, 'prompt_history.json')
with open(filename, 'w') as f:
json.dump({'version': 1, 'commands': []}, f)
os.chmod(filename, 0o644)

history_driver = HistoryDriver(filename)
history_driver.store_string('aws ec2 describe-instances')

file_mode = stat.S_IMODE(os.stat(filename).st_mode)
assert file_mode == 0o600
35 changes: 33 additions & 2 deletions tests/unit/customizations/history/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import numbers
import os
import re
import stat
import threading

from awscli.compat import queue
Expand All @@ -28,6 +29,7 @@
)
from awscli.testutils import FileCreator, mock, unittest
from tests import CaseInsensitiveDict
from tests.markers import skip_if_windows


class FakeDatabaseConnection:
Expand All @@ -48,8 +50,20 @@ def tearDown(self):


class TestDatabaseConnection(unittest.TestCase):
def setUp(self):
self.files = FileCreator()

def tearDown(self):
self.files.remove_all()

@skip_if_windows
@mock.patch('awscli.customizations.history.db.os.chmod')
@mock.patch('awscli.customizations.history.db.os.path.exists')
@mock.patch('awscli.compat.sqlite3.connect')
def test_can_connect_to_argument_file(self, mock_connect):
def test_can_connect_to_argument_file(
self, mock_connect, mock_exists, mock_chmod
):
mock_exists.return_value = True
expected_location = os.path.expanduser(
os.path.join('~', 'foo', 'bar', 'baz.db')
)
Expand All @@ -64,7 +78,7 @@ def test_does_try_to_enable_wal(self, mock_connect):
conn._connection.execute.assert_any_call('PRAGMA journal_mode=WAL')

def test_does_ensure_table_created_first(self):
db = DatabaseConnection(":memory:")
db = DatabaseConnection(':memory:')
cursor = db.execute('PRAGMA table_info(records)')
schema = [col[:3] for col in cursor.fetchall()]
expected_schema = [
Expand All @@ -85,6 +99,23 @@ def test_can_close(self, mock_connect):
conn.close()
self.assertTrue(connection.close.called)

@skip_if_windows
def test_create_new_file_with_secure_permissions(self):
db_filename = os.path.join(self.files.rootdir, 'new_secure.db')
DatabaseConnection(db_filename)
file_mode = stat.S_IMODE(os.stat(db_filename).st_mode)
self.assertEqual(file_mode, 0o600)

@skip_if_windows
def test_tighten_existing_file_permissions(self):
db_filename = os.path.join(self.files.rootdir, 'existing.db')
open(db_filename, 'a').close()
os.chmod(db_filename, 0o644)

DatabaseConnection(db_filename)
file_mode = stat.S_IMODE(os.stat(db_filename).st_mode)
self.assertEqual(file_mode, 0o600)


class TestDatabaseHistoryHandler(unittest.TestCase):
UUID_PATTERN = re.compile(
Expand Down
Loading
Loading