Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CDF-23957] 🧐 Robustify hash calculation #1439

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions CHANGELOG.cdf-tk.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Changes are grouped as follows:
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## TBD

### Fixed

- Deploying functions containing files that are not `utf-8` encoded no longer raises a `UnicodeDecodeError`.

## [0.4.3] - 2025-02-04

### Fixed
Expand Down
5 changes: 4 additions & 1 deletion cognite_toolkit/_cdf_tk/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,10 @@ def _replace_variables(
self.console(f"Processing file {source_path.name}...")

content = safe_read(source_path)
source = SourceLocationEager(source_path, calculate_str_or_file_hash(content, shorten=True))
# We cannot use the content as the basis for hash as this have been encoded.
# Instead, we use the source path, which will hash the bytes of the file directly,
# which is what we do in the deploy step to verify that the source file has not changed.
source = SourceLocationEager(source_path, calculate_str_or_file_hash(source_path, shorten=True))

content = variables.replace(content, source_path.suffix)

Expand Down
14 changes: 5 additions & 9 deletions cognite_toolkit/_cdf_tk/utils/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,11 @@ def calculate_secure_hash(item: dict[str, Any]) -> str:


def calculate_str_or_file_hash(content: str | Path, shorten: bool = False) -> str:
sha256_hash = hashlib.sha256()
if isinstance(content, Path):
content = content.read_text("utf-8")
# Get rid of Windows line endings to make the hash consistent across platforms.
sha256_hash.update(content.encode("utf-8").replace(b"\r\n", b"\n"))
calculated = sha256_hash.hexdigest()
if shorten:
return calculated[:8]
return calculated
if isinstance(content, str):
byte_content = content.encode("utf-8")
else:
byte_content = content.read_bytes()
Copy link
Collaborator Author

@doctrino doctrino Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is much safer to just hash the bytes directly and not care about the encoding of the file only the hash. Encoding can lead to UnicodeDecodeError

return calculate_bytes_or_file_hash(byte_content, shorten)


def calculate_bytes_or_file_hash(content: bytes | Path, shorten: bool = False) -> str:
Expand Down
1 change: 1 addition & 0 deletions tests/test_unit/test_cdf_tk/test_commands/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def test_build_valid_read_int_version(self) -> None:
source_filepath = MagicMock(spec=Path)
source_filepath.read_text.return_value = raw_yaml
source_filepath.suffix = ".yaml"
source_filepath.read_bytes.return_value = raw_yaml.encode("utf-8")

source_files = cmd._replace_variables(
[source_filepath], BuildVariables([]), TransformationLoader.folder_name, Path("my_module"), verbose=False
Expand Down