Validate sparse tensor external file paths
Merged
AD
5mo ago+664+664+012345678901234567890123456789-12-12-01234567890123456789
Description
Adds path traversal validation for sparse tensors with external data, closing a gap where SparseTensorProtoToDenseTensorProto would read external files without checking whether the path escapes the model directory.
Bug fix (pre-existing)
CopySparseDataindices size check: Theraw_data().size()check was wrong for external data (whereraw_datais empty). Fixed by adding a pre-unpackraw_datasize guard for inline data and a post-unpackunpack_buffersize check for all data sources.
Tests
- Security tests (tensorutils_test.cc): Path traversal blocked (values, indices), absolute path blocked (values, indices), zero-element regression (zero dense elements, zero NNZ). All create escaping files and assert specifically for
"escapes"error. - Positive tests (sparse_kernels_test.cc): 7 end-to-end tests for legitimate sparse tensors with external data — external values, external indices (INT64/INT32/INT16/INT8), both external (rank-1 and rank-2 COO).
Known limitation (deferred)
ORT_MEM_ADDR in-memory external data for sparse tensors can trigger arbitrary memory reads. This is a separate issue from path validation — LoadSparseInitializerOrtFormat legitimately uses in-memory markers for ORT-format models, so blanket rejection would break functionality. Should be addressed in a separate PR.
Motivation and Context
A malicious ONNX model could use ../ path traversal in sparse tensor external data locations to read arbitrary files outside the model directory. Dense tensors already had this validation; sparse tensors did not.