From 7eaadc1dc3ab3f1cc35824f07934e3846ea39aa5 Mon Sep 17 00:00:00 2001 From: Dery Ferdika <114888155+DeryFerd@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:37:24 +0700 Subject: [PATCH] fix(jvm): stop reporting a class attribute that is never emitted (#181) --- src/shields/formats/jvm-bytecode.ts | 11 +++-- tests/format-canary-coverage.test.ts | 2 +- tests/jvm-class-attribute-honesty.test.ts | 53 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests/jvm-class-attribute-honesty.test.ts diff --git a/src/shields/formats/jvm-bytecode.ts b/src/shields/formats/jvm-bytecode.ts index eb98cf5..ef9f8a6 100644 --- a/src/shields/formats/jvm-bytecode.ts +++ b/src/shields/formats/jvm-bytecode.ts @@ -62,8 +62,11 @@ function measureConstantPoolEntry(buf: Buffer, offset: number): { size: number; /** * Protects a Java / Kotlin JVM `.class` bytecode buffer (`0xCAFEBABE`). * - Parses the JVM constant pool if structurally complete and appends valid `CONSTANT_Utf8` - * decoy enclave descriptors + a custom JVM §4.7.1 class attribute (`BlaEnclaveDelegation`). - * - Preserves 100% JVM verifier compatibility while polluting `javap`, CFR, Vineflower, and JADX. + * decoy enclave descriptors to it, then appends a JSON decoy trailer *after* the class body. + * No §4.7.1 class attribute is emitted: `attributes_count` and the attribute table are left + * untouched, so `customAttributesAdded` stays 0 (same rule as the trailer-only path). + * - Keeps the class structure itself verifier-compatible while polluting `javap`, CFR, + * Vineflower, and JADX with unreferenced constant-pool decoys and tail bytes. */ export function protectJvmClassBuffer( rawBuffer: Buffer, @@ -149,7 +152,9 @@ export function protectJvmClassBuffer( buffer: Buffer.concat([headerAndOldCp, ...newCpBuffers, bodyAfterCp, attrPayload]), mode: "structural", constantPoolEntriesAdded: newCpBuffers.length, - customAttributesAdded: 1, + // The decoy corpus lives in the constant pool + the tail after the + // class body; no class attribute is added to the attribute table. + customAttributesAdded: 0, chaffPayloadsEmbedded: chaff.length, canaryTokensEmbedded: deception.canary.syntheticCanaryTokens.length, diagnosticsEmbedded: deception.diagnostics.diagnosticsInjected.length, diff --git a/tests/format-canary-coverage.test.ts b/tests/format-canary-coverage.test.ts index 5115d22..08207d0 100644 --- a/tests/format-canary-coverage.test.ts +++ b/tests/format-canary-coverage.test.ts @@ -89,7 +89,7 @@ describe("Canary + system-diagnostic coverage on remaining formats", () => { const res = protectJvmClassBuffer(cls, { intensity: "balanced", seed: "jvm-structural" }); expect(res.mode).toBe("structural"); expect(res.constantPoolEntriesAdded).toBeGreaterThan(0); - expect(res.customAttributesAdded).toBe(1); + expect(res.customAttributesAdded).toBe(0); }); it("embeds canary decoys into ZIP members and recursively hardens nested DEX", () => { diff --git a/tests/jvm-class-attribute-honesty.test.ts b/tests/jvm-class-attribute-honesty.test.ts new file mode 100644 index 0000000..cabc015 --- /dev/null +++ b/tests/jvm-class-attribute-honesty.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from "vitest"; +import { protectJvmClassBuffer } from "../src/shields/formats/jvm-bytecode.js"; + +/** + * A minimal class whose structure parses far enough for structural mode: + * magic + minor + major + constant_pool_count(=1) + access_flags..attributes_count. + * Class-level `attributes_count` sits at byte 24 and is 0. + */ +function structuralClass(): Buffer { + const cls = Buffer.alloc(26, 0); + cls.writeUInt32BE(0xcafebabe, 0); + cls.writeUInt16BE(0, 4); // minor + cls.writeUInt16BE(65, 6); // major + cls.writeUInt16BE(1, 8); // constant_pool_count (empty pool) + cls.writeUInt16BE(0, 24); // attributes_count + return cls; +} + +describe("JVM structural protection reports only what it actually emits", () => { + it("does not claim a class attribute when the decoy corpus is appended after the class body", () => { + const input = structuralClass(); + const res = protectJvmClassBuffer(input, { intensity: "balanced", seed: "jvm-honesty" }); + + expect(res.mode).toBe("structural"); + expect(res.constantPoolEntriesAdded).toBeGreaterThan(0); + + // The constant pool really did grow in the class header. + expect(res.buffer.readUInt16BE(8)).toBe(1 + res.constantPoolEntriesAdded); + + // No §4.7.1 class attribute is emitted, so the counter must be 0. + expect(res.customAttributesAdded).toBe(0); + + // The bytes after the class body are a JSON trailer, not an attribute table. + const jsonStart = res.buffer.indexOf(Buffer.from('{"schema":"bla-jvm-enclave-v1"')); + expect(jsonStart).toBeGreaterThan(0); + expect(res.buffer.readUInt16BE(jsonStart - 2)).toBe(0); // attributes_count still 0 + // A real attribute would start with an in-range name_index; JSON cannot. + expect(res.buffer.readUInt16BE(jsonStart)).toBeGreaterThan(res.buffer.readUInt16BE(8)); + }); + + it("keeps reporting zero structural counters on the trailer fallback path", () => { + const cls = Buffer.alloc(24, 0); + cls.writeUInt32BE(0xcafebabe, 0); + cls.writeUInt16BE(0, 4); + cls.writeUInt16BE(55, 6); + cls.writeUInt16BE(1, 8); + const res = protectJvmClassBuffer(cls, { intensity: "balanced", seed: "jvm-honesty-trailer" }); + + expect(res.mode).toBe("trailer"); + expect(res.constantPoolEntriesAdded).toBe(0); + expect(res.customAttributesAdded).toBe(0); + }); +}); \ No newline at end of file