Commit f704c88
3.12.1 cherry pick changes for patch release. (#22081)
* Fix DoS via malicious HDF5 dataset metadata in KerasFileEditor (#21880)
* Fix DoS via malicious HDF5 dataset metadata in KerasFileEditor
* Refactor: move MAX_BYTES constant outside loop per review feedback
* Fix: harden HDF5 dataset metadata validation in KerasFileEditor
* Do not allow external links in HDF5 files. (#22057)
Keras never uses this feature.
- verify that we get H5 Groups when expected, otherwise, merely by doing `[key]` we may be loading an external Dataset.
- verify that the H5 Datasets are not external links and fail if they are.
- remove unused methods `items` and `values` in `H5IOStore` and `ShardedH5IOStore`. They are not used, the implementation of `MutableMapping` was incomplete anyway and these methods we return unverified Datasets.
- fixed logic related to `failed_saveables` in `load_state`.
- preserve the order of keys in the implementation of `ShardedH5IOStore.keys()`.
* Disallow TFSMLayer deserialization in safe_mode to prevent external SavedModel execution (#22035)
* Implement safe mode checks in TFSMLayer
Added safe mode checks for loading TFSMLayer from external SavedModels.
* Update keras/src/export/tfsm_layer.py
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
* Align logic with __init__ method for robust checks
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
* Fix indentation and formatting in tfsm_layer.py
* Add setup method to enable unsafe deserialization
Enable unsafe deserialization for TFSM Layer tests.
* Update TFSMLayer initialization in tests
* Fix import for TFSMLayer in tfsm_layer_test.py
* Remove safe_mode check from TFSMLayer.__init__()
The safe_mode check should only be in from_config(), not __init__().
Direct instantiation (TFSMLayer(filepath=...)) is a legitimate use case
where the user explicitly creates the layer. The security concern is
only during deserialization of untrusted .keras files, which goes
through from_config().
This allows attackers to create malicious .keras files while still
blocking victims from loading them with safe_mode=True.
* Implement tests for TFSMLayer safe mode functionality
Add comprehensive tests for TFSMLayer safe_mode behavior:
- test_safe_mode_direct_instantiation_allowed: Verifies direct
TFSMLayer instantiation works as expected
- test_safe_mode_from_config_blocked: Verifies from_config() raises
ValueError when safe_mode=True
- test_safe_mode_from_config_allowed_when_disabled: Verifies
from_config() works with safe_mode=False
- test_safe_mode_model_loading_blocked: Tests the full attack scenario
where loading a .keras file with safe_mode=True is blocked
* Clarify test docstrings in tfsm_layer_test.py
Updated test docstrings for clarity on instantiation and loading behavior.
* Invoke model with random input in tfsm_layer tests
Added model invocation with random input to tests for TFSMLayer.
* Set safe_mode default to True in from_config method
* Update tfsm_layer_test.py
* Update tfsm_layer_test.py
* Update tfsm_layer_test.py to original
* New test case tfsm_layer_test.py
* Update Comments tfsm_layer.py
* Update tfsm_layer_test.py
* Update tfsm_layer.py
* Update tfsm_layer.py to remove ruff errors
* Update tfsm_layer.py
* Update tfsm_layer_test.py
* Update tfsm_layer.py
* Update tfsm_layer.py
* Update tfsm_layer_test.py
* Update tfsm_layer.py format fix
Changes in format
* Update tfsm_layer.py
* Update tfsm_layer_test.py
* Update tfsm_layer.py
* Update tfsm_layer_test.py
* Fixes unnecessary changes tfsm_layer.py
* Added new test case tfsm_layer_test.py
* Set `safe_mode=None` in `from_config`, which fixes the unit tests.
Also re-added empty lines.
* Remove unneeded `custom_objects` in unit tests.
---------
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Fabien Hertschuh <1091026+hertschuh@users.noreply.github.com>
* commit 3.12.1 cherry-pick changes
---------
Co-authored-by: sarvesh patil <103917093+HyperPS@users.noreply.github.com>
Co-authored-by: hertschuh <1091026+hertschuh@users.noreply.github.com>
Co-authored-by: Manan Patel <70314133+0xManan@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>1 parent adbfd13 commit f704c88
File tree
6 files changed
+200
-75
lines changed- keras/src
- export
- saving
6 files changed
+200
-75
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
118 | 120 | | |
119 | 121 | | |
120 | 122 | | |
121 | 123 | | |
122 | 124 | | |
123 | 125 | | |
124 | 126 | | |
125 | | - | |
126 | | - | |
| 127 | + | |
127 | 128 | | |
128 | 129 | | |
129 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
130 | 159 | | |
131 | 160 | | |
132 | 161 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
455 | 455 | | |
456 | 456 | | |
457 | 457 | | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
458 | 461 | | |
459 | 462 | | |
460 | 463 | | |
461 | 464 | | |
462 | 465 | | |
463 | 466 | | |
464 | 467 | | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
465 | 472 | | |
466 | | - | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
467 | 476 | | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
468 | 481 | | |
| 482 | + | |
469 | 483 | | |
470 | 484 | | |
| 485 | + | |
| 486 | + | |
471 | 487 | | |
472 | 488 | | |
473 | 489 | | |
474 | | - | |
| 490 | + | |
475 | 491 | | |
476 | 492 | | |
477 | | - | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
478 | 496 | | |
479 | 497 | | |
| 498 | + | |
480 | 499 | | |
481 | | - | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
482 | 503 | | |
483 | | - | |
484 | | - | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
485 | 566 | | |
486 | 567 | | |
487 | 568 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
796 | 796 | | |
797 | 797 | | |
798 | 798 | | |
799 | | - | |
| 799 | + | |
| 800 | + | |
800 | 801 | | |
801 | 802 | | |
802 | 803 | | |
| |||
807 | 808 | | |
808 | 809 | | |
809 | 810 | | |
810 | | - | |
| 811 | + | |
| 812 | + | |
811 | 813 | | |
812 | 814 | | |
813 | 815 | | |
| |||
855 | 857 | | |
856 | 858 | | |
857 | 859 | | |
858 | | - | |
| 860 | + | |
859 | 861 | | |
860 | 862 | | |
861 | 863 | | |
| |||
1035 | 1037 | | |
1036 | 1038 | | |
1037 | 1039 | | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
1038 | 1059 | | |
1039 | 1060 | | |
1040 | 1061 | | |
| |||
1094 | 1115 | | |
1095 | 1116 | | |
1096 | 1117 | | |
1097 | | - | |
| 1118 | + | |
1098 | 1119 | | |
1099 | | - | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
1100 | 1123 | | |
1101 | 1124 | | |
1102 | 1125 | | |
1103 | 1126 | | |
1104 | 1127 | | |
1105 | | - | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
1106 | 1131 | | |
1107 | 1132 | | |
1108 | 1133 | | |
| |||
1134 | 1159 | | |
1135 | 1160 | | |
1136 | 1161 | | |
1137 | | - | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | 1162 | | |
1144 | | - | |
| 1163 | + | |
1145 | 1164 | | |
1146 | 1165 | | |
1147 | 1166 | | |
1148 | 1167 | | |
1149 | 1168 | | |
1150 | 1169 | | |
1151 | | - | |
1152 | | - | |
1153 | | - | |
1154 | | - | |
1155 | | - | |
| 1170 | + | |
1156 | 1171 | | |
1157 | 1172 | | |
1158 | 1173 | | |
| |||
1355 | 1370 | | |
1356 | 1371 | | |
1357 | 1372 | | |
1358 | | - | |
1359 | | - | |
1360 | | - | |
1361 | | - | |
| 1373 | + | |
1362 | 1374 | | |
1363 | 1375 | | |
1364 | 1376 | | |
1365 | 1377 | | |
1366 | | - | |
| 1378 | + | |
| 1379 | + | |
1367 | 1380 | | |
1368 | 1381 | | |
1369 | 1382 | | |
1370 | 1383 | | |
1371 | 1384 | | |
1372 | 1385 | | |
1373 | 1386 | | |
1374 | | - | |
| 1387 | + | |
1375 | 1388 | | |
1376 | | - | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
1377 | 1392 | | |
1378 | 1393 | | |
1379 | 1394 | | |
| |||
1392 | 1407 | | |
1393 | 1408 | | |
1394 | 1409 | | |
1395 | | - | |
| 1410 | + | |
| 1411 | + | |
1396 | 1412 | | |
1397 | 1413 | | |
1398 | | - | |
1399 | | - | |
1400 | | - | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
1401 | 1418 | | |
1402 | 1419 | | |
1403 | 1420 | | |
1404 | | - | |
1405 | | - | |
1406 | | - | |
1407 | | - | |
1408 | | - | |
1409 | | - | |
1410 | | - | |
1411 | | - | |
1412 | | - | |
1413 | | - | |
1414 | | - | |
1415 | | - | |
1416 | | - | |
1417 | | - | |
1418 | | - | |
1419 | | - | |
1420 | | - | |
1421 | | - | |
1422 | 1421 | | |
1423 | 1422 | | |
1424 | 1423 | | |
| |||
0 commit comments