mirror of
https://github.com/qybaihe/bla.git
synced 2026-11-20 08:20:59 +08:00
fix(jvm): stop reporting a class attribute that is never emitted (#181)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user