|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2026 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import pytest |
| 19 | +from unittest import mock |
| 20 | +from google.cloud.aiplatform.prediction.xgboost.predictor import ( |
| 21 | + XgboostPredictor, |
| 22 | +) |
| 23 | +from google.cloud.aiplatform.prediction.sklearn.predictor import ( |
| 24 | + SklearnPredictor, |
| 25 | +) |
| 26 | + |
| 27 | +from google.cloud.aiplatform.utils import prediction_utils |
| 28 | +from google.cloud.aiplatform.prediction.xgboost import ( |
| 29 | + predictor as xgboost_predictor, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +class TestPredictorSecurity: |
| 34 | + @pytest.mark.parametrize("predictor_class", [XgboostPredictor, SklearnPredictor]) |
| 35 | + def test_load_warns_no_allowed_extensions(self, predictor_class): |
| 36 | + """Verifies UserWarning is issued when allowed_extensions is missing.""" |
| 37 | + predictor = predictor_class() |
| 38 | + with mock.patch.object( |
| 39 | + prediction_utils, "download_model_artifacts" |
| 40 | + ), mock.patch("os.path.exists", return_value=True), mock.patch( |
| 41 | + "joblib.load" |
| 42 | + ), mock.patch( |
| 43 | + "pickle.load" |
| 44 | + ), mock.patch.object( |
| 45 | + xgboost_predictor.xgb, "Booster" |
| 46 | + ), mock.patch( |
| 47 | + "builtins.open", mock.mock_open() |
| 48 | + ): |
| 49 | + with pytest.warns(UserWarning, match="No 'allowed_extensions' provided"): |
| 50 | + predictor.load("gs://test-bucket") |
| 51 | + |
| 52 | + def test_xgboost_load_warns_on_joblib(self): |
| 53 | + """Verifies RuntimeWarning is issued when loading a .joblib file.""" |
| 54 | + predictor = XgboostPredictor() |
| 55 | + with mock.patch.object( |
| 56 | + prediction_utils, "download_model_artifacts" |
| 57 | + ), mock.patch( |
| 58 | + "os.path.exists", side_effect=lambda p: p.endswith(".joblib") |
| 59 | + ), mock.patch( |
| 60 | + "joblib.load" |
| 61 | + ), mock.patch.object( |
| 62 | + xgboost_predictor.xgb, "Booster" |
| 63 | + ): |
| 64 | + with pytest.warns( |
| 65 | + RuntimeWarning, match="using joblib pickle, which is unsafe" |
| 66 | + ): |
| 67 | + predictor.load("gs://test-bucket", allowed_extensions=[".joblib"]) |
| 68 | + |
| 69 | + def test_xgboost_load_raises_not_allowed(self): |
| 70 | + """Verifies ValueError is raised if the file exists but is not allowed.""" |
| 71 | + predictor = XgboostPredictor() |
| 72 | + with mock.patch.object( |
| 73 | + prediction_utils, "download_model_artifacts" |
| 74 | + ), mock.patch.object(xgboost_predictor.xgb, "Booster"), mock.patch( |
| 75 | + "os.path.exists", side_effect=lambda p: p.endswith(".pkl") |
| 76 | + ): |
| 77 | + with pytest.raises(ValueError, match="must be provided and allowed"): |
| 78 | + predictor.load("gs://test-bucket", allowed_extensions=[".bst"]) |
| 79 | + |
| 80 | + def test_sklearn_load_warns_on_pickle(self): |
| 81 | + """Verifies RuntimeWarning is issued when loading a .pkl file.""" |
| 82 | + predictor = SklearnPredictor() |
| 83 | + with mock.patch.object( |
| 84 | + prediction_utils, "download_model_artifacts" |
| 85 | + ), mock.patch( |
| 86 | + "os.path.exists", side_effect=lambda p: p.endswith(".pkl") |
| 87 | + ), mock.patch( |
| 88 | + "builtins.open", mock.mock_open() |
| 89 | + ), mock.patch( |
| 90 | + "pickle.load" |
| 91 | + ): |
| 92 | + |
| 93 | + with pytest.warns(RuntimeWarning, match="using pickle, which is unsafe"): |
| 94 | + predictor.load("gs://test-bucket", allowed_extensions=[".pkl"]) |
| 95 | + |
| 96 | + def test_sklearn_load_warns_on_joblib(self): |
| 97 | + """Verifies RuntimeWarning is issued when loading a .joblib file in Scikit-learn.""" |
| 98 | + predictor = SklearnPredictor() |
| 99 | + with mock.patch.object( |
| 100 | + prediction_utils, "download_model_artifacts" |
| 101 | + ), mock.patch( |
| 102 | + "os.path.exists", side_effect=lambda p: p.endswith(".joblib") |
| 103 | + ), mock.patch( |
| 104 | + "joblib.load" |
| 105 | + ): |
| 106 | + with pytest.warns( |
| 107 | + RuntimeWarning, match=r"using joblib pickle, which is unsafe" |
| 108 | + ): |
| 109 | + predictor.load("gs://test-bucket", allowed_extensions=[".joblib"]) |
0 commit comments