From 4e356ee7b8e0059e43643cc2e603685893dc92f3 Mon Sep 17 00:00:00 2001 From: goose Date: Thu, 1 Oct 2026 09:02:23 -0400 Subject: [PATCH] fix: harden S4 audio verification --- README.md | 2 +- scripts/infrastructure/audio_library.gd | 50 ++++++++++++++++++++++++- scripts/test-e2e.sh | 13 ++++++- scripts/test-unit.sh | 13 ++++++- tests/e2e/test_no_audio_startup_e2e.gd | 12 +++++- tests/unit/test_audio_library.gd | 18 +++++++++ 6 files changed, 102 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 47027e5..1cad89d 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Audio is optional presentation. Its absence, unknown requests, missing folders, └── death/ ``` -Admitted file extensions are `.mp3`, `.ogg`, `.oga`, `.wav`, `.flac`, `.m4a`, and `.opus`. Godot-native playback is attempted for compatible `.wav`, `.ogg`/`.oga`/`.opus`, and `.mp3` files; other admitted extensions are discovered but silently skipped if no native loader is available. The game selects `exploration` at startup, changes to `combat` for a combat result, and dispatches SFX for player attacks, enemy attacks, and death. +Admitted file extensions are `.mp3`, `.ogg`, `.oga`, `.wav`, `.flac`, `.m4a`, and `.opus`. Before invoking an external Godot loader, `AudioLibrary` fails closed unless a `.wav` has a `RIFF`/`WAVE` signature, an `.ogg`/`.oga`/`.opus` has `OggS`, or an `.mp3` has `ID3` or a valid MPEG sync word. `.flac` and `.m4a` remain silent because this library has no native external loader for them. Thus extension-only or malformed external files are never handed to `load_from_file`. The game selects `exploration` at startup, changes to `combat` for a combat result, and dispatches SFX for player attacks, enemy attacks, and death. Example: diff --git a/scripts/infrastructure/audio_library.gd b/scripts/infrastructure/audio_library.gd index 7bc466d..1b2f2a3 100644 --- a/scripts/infrastructure/audio_library.gd +++ b/scripts/infrastructure/audio_library.gd @@ -69,13 +69,59 @@ func _find_first_audio_file(kind: String, name: String) -> String: directory.list_dir_begin() var entry := directory.get_next() while not entry.is_empty(): - if not directory.current_is_dir() and entry.get_extension().to_lower() in ADMITTED_EXTENSIONS: + var path := directory_path.path_join(entry) + if not directory.current_is_dir() and _is_loadable_external_audio(path): directory.list_dir_end() - return directory_path.path_join(entry) + return path entry = directory.get_next() directory.list_dir_end() return "" +func _is_loadable_external_audio(path: String) -> bool: + var extension := path.get_extension().to_lower() + if not extension in ADMITTED_EXTENSIONS: + return false + # Do not call Godot's external loaders unless the bytes identify a codec they load. + # Other contract-admitted extensions (flac/m4a) intentionally have no native loader. + match extension: + "wav": + return _has_wav_signature(path) + "ogg", "oga", "opus": + return _has_prefix(path, PackedByteArray([79, 103, 103, 83])) # OggS + "mp3": + return _has_mp3_signature(path) + _: + return false + +func _has_wav_signature(path: String) -> bool: + var file := FileAccess.open(path, FileAccess.READ) + if file == null: + return false + var header := file.get_buffer(12) + file.close() + return header.size() == 12 and header.slice(0, 4) == PackedByteArray([82, 73, 70, 70]) and header.slice(8, 12) == PackedByteArray([87, 65, 86, 69]) # RIFF....WAVE + +func _has_prefix(path: String, prefix: PackedByteArray) -> bool: + var file := FileAccess.open(path, FileAccess.READ) + if file == null: + return false + var header := file.get_buffer(prefix.size()) + file.close() + return header == prefix + +func _has_mp3_signature(path: String) -> bool: + var file := FileAccess.open(path, FileAccess.READ) + if file == null: + return false + var header := file.get_buffer(3) + file.close() + if header.size() < 2: + return false + if header.size() == 3 and header == PackedByteArray([73, 68, 51]): # ID3 + return true + # MPEG frame sync: 11 set bits, MPEG version not reserved, layer not reserved. + return header[0] == 255 and (header[1] & 0xE0) == 0xE0 and (header[1] & 0x18) != 0x08 and (header[1] & 0x06) != 0 + func _play_ambience(path: String) -> void: var stream := _load_audio_stream(path) if stream == null or _ambience_player == null: diff --git a/scripts/test-e2e.sh b/scripts/test-e2e.sh index 3d7c30e..fed1347 100755 --- a/scripts/test-e2e.sh +++ b/scripts/test-e2e.sh @@ -5,4 +5,15 @@ if [[ ! -x "$GODOT_BIN" ]]; then GODOT_BIN="$(command -v godot4 || command -v godot || true)" fi : "${GODOT_BIN:?Set GODOT_BIN or install godot4/godot}" -exec "$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/e2e -gexit + +log_path="$(mktemp)" +trap 'rm -f "$log_path"' EXIT +set +e +"$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/e2e -gexit 2>&1 | tee "$log_path" +godot_status=${PIPESTATUS[0]} +set -e +if grep -Eq 'SCRIPT ERROR|Parse Error' "$log_path"; then + echo "E2E tests failed: Godot reported a script or parse error" >&2 + exit 1 +fi +exit "$godot_status" diff --git a/scripts/test-unit.sh b/scripts/test-unit.sh index 810bc08..dfa88df 100755 --- a/scripts/test-unit.sh +++ b/scripts/test-unit.sh @@ -5,4 +5,15 @@ if [[ ! -x "$GODOT_BIN" ]]; then GODOT_BIN="$(command -v godot4 || command -v godot || true)" fi : "${GODOT_BIN:?Set GODOT_BIN or install godot4/godot}" -exec "$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/unit -gexit + +log_path="$(mktemp)" +trap 'rm -f "$log_path"' EXIT +set +e +"$GODOT_BIN" --headless --path . -s res://addons/gut/gut_cmdln.gd -gdir=res://tests/unit -gexit 2>&1 | tee "$log_path" +godot_status=${PIPESTATUS[0]} +set -e +if grep -Eq 'SCRIPT ERROR|Parse Error' "$log_path"; then + echo "unit tests failed: Godot reported a script or parse error" >&2 + exit 1 +fi +exit "$godot_status" diff --git a/tests/e2e/test_no_audio_startup_e2e.gd b/tests/e2e/test_no_audio_startup_e2e.gd index d3c0fef..346d78b 100644 --- a/tests/e2e/test_no_audio_startup_e2e.gd +++ b/tests/e2e/test_no_audio_startup_e2e.gd @@ -1,19 +1,29 @@ extends GutTest const GameScene = preload("res://scenes/Game.tscn") +const AudioLibrary = preload("res://scripts/infrastructure/audio_library.gd") var empty_audio_home := "" +var previous_audio_home := "" var game: Game func before_each() -> void: + previous_audio_home = OS.get_environment("DCW_AUDIO_HOME") empty_audio_home = ProjectSettings.globalize_path("user://s4-empty-audio-home") DirAccess.make_dir_recursive_absolute(empty_audio_home) OS.set_environment("DCW_AUDIO_HOME", empty_audio_home) game = GameScene.instantiate() + # Replace the scene library before ready to isolate both production search roots. + var scene_audio := game.get_node("AudioLibrary") + var isolated_audio := AudioLibrary.new(empty_audio_home) + isolated_audio.name = "AudioLibrary" + game.remove_child(scene_audio) + scene_audio.free() + game.add_child(isolated_audio) add_child_autofree(game) await get_tree().process_frame func after_each() -> void: - OS.set_environment("DCW_AUDIO_HOME", "") + OS.set_environment("DCW_AUDIO_HOME", previous_audio_home) func test_real_game_resolves_combat_with_empty_audio_home() -> void: var audio = game.get_node("AudioLibrary") diff --git a/tests/unit/test_audio_library.gd b/tests/unit/test_audio_library.gd index e96612f..00e6763 100644 --- a/tests/unit/test_audio_library.gd +++ b/tests/unit/test_audio_library.gd @@ -25,6 +25,24 @@ func test_unknown_requests_are_silent() -> void: assert_eq(library.get_last_ambience_path(), "") assert_eq(library.get_last_sfx_path(), "") +func test_invalid_wav_is_not_selected_or_played_and_does_not_block_requests() -> void: + var root := ProjectSettings.globalize_path("user://s4-invalid-audio-library") + var invalid_path := root.path_join("audio/exploration/not-a-wave.wav") + DirAccess.make_dir_recursive_absolute(invalid_path.get_base_dir()) + var invalid_file := FileAccess.open(invalid_path, FileAccess.WRITE) + invalid_file.store_string("this is not a WAV file") + invalid_file.close() + var library := AudioLibrary.new(root) + add_child_autofree(library) + + library.set_ambience_context("exploration") + library.play_sfx("attack_given") + + assert_eq(library.get_last_ambience_path(), "") + assert_eq(library.get_last_sfx_path(), "") + assert_false(library._ambience_player.playing) + assert_false(library._sfx_player.playing) + func test_admitted_wav_file_is_discovered() -> void: var root := ProjectSettings.globalize_path("user://s4-audio-library") var sample_path := root.path_join("audio/exploration/silence.wav")