Skip to content

Commit d3c75f4

Browse files
authored
Merge pull request #1077 from GitGuardian/salomevoltz/scrt-5449-make-ggshield-able-to-scan-jar-files
feat(cmd_archive): Add support for .jar files
2 parents 22a8136 + 4ff1855 commit d3c75f4

File tree

12 files changed

+62
-8
lines changed

12 files changed

+62
-8
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
-->
6+
7+
<!--
8+
### Removed
9+
10+
- A bullet item for the Removed category.
11+
12+
-->
13+
14+
### Added
15+
16+
- ggshield can now scan .jar files using `ggshield secret scan archive`
17+
18+
<!--
19+
### Changed
20+
21+
- A bullet item for the Changed category.
22+
23+
-->
24+
<!--
25+
### Deprecated
26+
27+
- A bullet item for the Deprecated category.
28+
29+
-->
30+
<!--
31+
### Fixed
32+
33+
- A bullet item for the Fixed category.
34+
35+
-->
36+
<!--
37+
### Security
38+
39+
- A bullet item for the Security category.
40+
41+
-->

ggshield/cmd/secret/scan/archive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def archive_cmd(
3434
**kwargs: Any,
3535
) -> int: # pragma: no cover
3636
"""
37-
Scan an archive file. Supported archive formats are zip, tar, tar.gz, tar.bz2 and tar.xz.
37+
Scan an archive file. Supported archive formats are zip, whl, jar, tar, tar.gz, tar.bz2 and tar.xz.
3838
"""
3939
with tempfile.TemporaryDirectory(suffix="ggshield") as temp_dir:
4040
temp_path = Path(temp_dir)

ggshield/utils/archive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def safe_unpack(archive: Path, extract_dir: Path) -> None:
4646
check_archive_content(archive)
4747

4848
# unpack_archive does not know .whl files are zip files
49-
archive_format = "zip" if archive.suffix == ".whl" else None
49+
archive_format = "zip" if archive.suffix in {".whl", ".jar"} else None
5050

5151
shutil.unpack_archive(archive, extract_dir, format=archive_format)
5252

@@ -55,7 +55,7 @@ def check_archive_content(archive: Path) -> None:
5555
"""
5656
Check `archive` safety, raise `UnsafeArchive` if it is unsafe.
5757
"""
58-
if archive.suffix in {".zip", ".whl"}:
58+
if archive.suffix in {".zip", ".whl", ".jar"}:
5959
_check_zip_content(archive)
6060
else:
6161
_check_tar_content(archive)

tests/unit/data/archives/bad.jar

1.21 KB
Binary file not shown.

tests/unit/data/archives/bad.tar

0 Bytes
Binary file not shown.

tests/unit/data/archives/bad.zip

0 Bytes
Binary file not shown.

tests/unit/data/archives/generate-archives

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ set -euo pipefail
2020
cd "$(dirname "$0")"
2121
BAD_ZIP=$PWD/bad.zip
2222
BAD_TAR=$PWD/bad.tar
23+
BAD_JAR=$PWD/bad.jar
2324

2425
GOOD_ZIP=$PWD/good.zip
2526
GOOD_WHL=$PWD/good.whl
2627
GOOD_TAR=$PWD/good.tar
28+
GOOD_JAR=$PWD/good.jar
2729

28-
rm -f "$BAD_ZIP" "$BAD_TAR" "$GOOD_ZIP" "$GOOD_TAR" "$GOOD_WHL"
30+
rm -f "$BAD_ZIP" "$BAD_TAR" "$BAD_JAR" "$GOOD_ZIP" "$GOOD_TAR" "$GOOD_WHL" "$GOOD_JAR"
2931

3032
rm -rf work
3133
mkdir -p work/archive-root
@@ -52,6 +54,11 @@ mkdir -p work/archive-root/subdir
5254
ZIP_CMD="7z a"
5355
7z a -spf -snl "$BAD_ZIP" ../bad-relative /tmp/bad-absolute . > /dev/null
5456
7z a "$GOOD_ZIP" fine subdir/fine-symlink > /dev/null
57+
58+
# A .jar is a .zip with a different extension
59+
60+
cp "$BAD_ZIP" "$BAD_JAR"
61+
cp "$GOOD_ZIP" "$GOOD_JAR"
5562

5663
# A .whl is a .zip with a different extension
5764
cp "$GOOD_ZIP" "$GOOD_WHL"
@@ -65,6 +72,8 @@ rm -rf work
6572
echo "Generated:
6673
$BAD_ZIP
6774
$BAD_TAR
75+
$BAD_JAR
6876
$GOOD_ZIP
6977
$GOOD_WHL
70-
$GOOD_TAR"
78+
$GOOD_TAR
79+
$GOOD_JAR"

tests/unit/data/archives/good.jar

302 Bytes
Binary file not shown.

tests/unit/data/archives/good.tar

0 Bytes
Binary file not shown.

tests/unit/data/archives/good.whl

-2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)