Skip to content

Commit

Permalink
Use r2.log/error instead of console in the plugin script
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jun 14, 2024
1 parent 9355b45 commit 7af8b7f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- name: Building
run: make
- name: Running tests
run: r2r test/db
run: make citest
# do not upload twice
# - uses: actions/upload-artifact@v4
# with:
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ sarif.r2.js: sarif-ts/plugin.r2.ts
test:
R2R_OFFLINE=1 r2r -i test/db

citest:
R2R_OFFLINE=1 r2r test/db

user-install: sarif.r2.js
mkdir -p $(PLUGDIR)
rm -f $(PLUGDIR)/sarif.r2.js
Expand Down
3 changes: 1 addition & 2 deletions sarif-ts/examples/verify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readFileSync } from "fs";
import { verifySarif } from "../sarif/utils.js";
import { verifySarif, readFileSync } from "../sarif/utilsverify.js";

const data: any = JSON.parse(readFileSync("test-sarif.json", "utf-8"));
const result = verifySarif(data);
Expand Down
52 changes: 31 additions & 21 deletions sarif-ts/plugin.r2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface R2Pipe {
plugin(module: string, def: any): void;
cmd(command: string): string;
log(msg: string);
error(msg: string);
}

declare global {
Expand Down Expand Up @@ -83,7 +84,7 @@ class R2Sarif {
let count = 0;
for (const doc of this.docs) {
if (count++ === documentIndex) {
console.log("Document " + count + " unloaded. Run 'sarif select' again please.");
r2.error("Document " + count + " unloaded. Run 'sarif select' again please.");
} else {
newDocs.push(doc);
newPaths.push(this.paths[count]);
Expand Down Expand Up @@ -111,7 +112,7 @@ class R2Sarif {
for (const loc of res.locations) {
if ((loc as BinaryLocation).properties) {
const bloc = loc as BinaryLocation;
// console.log(loc.physicalLocation.artifactLocation.uri);
// r2.log(loc.physicalLocation.artifactLocation.uri);
const addr = bloc.properties.memoryAddress;
const size = bloc.physicalLocation.region.byteLength;
const rule = res.ruleId;
Expand Down Expand Up @@ -164,19 +165,19 @@ class R2Sarif {
const [fileName] = args;
const data = readFileSync(fileName)
if (data instanceof Error) {
console.log("Error: " + data);
r2.error("Error: " + data);
return false;
}
const doc = this.sf.parse(data);
if (doc instanceof Error) {
console.log("Error parsing "+ doc);
r2.error("Error parsing "+ doc);
return false;
}
this.docs.push(doc);
this.paths.push(fileName);
console.log("Document loaded. Use 'sarif list'")
r2.error("Document loaded. Use 'sarif list'")
} else {
console.log("Usage: sarif load <sarif file>");
r2.error("Usage: sarif load <sarif file>");
}
return true;
}
Expand All @@ -190,7 +191,7 @@ class R2Sarif {
if (index < drivers.length) {
this.currentDriver = drivers[index];
this.currentDriverIndex = index;
console.log("Selected driver:")
r2.log("Selected driver:")
this.listDriver(index, this.currentDriver);
return true;
}
Expand All @@ -200,16 +201,18 @@ class R2Sarif {

listDriver(index: number, driver: Driver) {
const sel = (index === this.currentDriverIndex)? "* ": " ";
console.log(sel, index, driver.name, driver.semanticVersion)
const ver = driver.semanticVersion? driver.semanticVersion: driver.version? driver.version: ""
const text = tabulateText([sel, index.toString(), driver.name, ver], [2, 4, 20, 20]);
r2.log(text);
}

listDocs() {
let count = 0;
for (const doc of this.docs) {
const docPath = this.paths[count];
console.log(count + " " + docPath);
r2.log(count + " " + docPath);
for (const run of doc.runs) {
console.log(" + " + run.tool.driver.name + " " + run.tool.driver.semanticVersion);
r2.log(" + " + run.tool.driver.name + " " + run.tool.driver.semanticVersion);
}
count++;
}
Expand All @@ -230,7 +233,7 @@ class R2Sarif {
}
listJson() {
for (const doc of this.docs) {
console.log(JSON.stringify(doc));
r2.log(JSON.stringify(doc));
}
}
listResults() : Result[] {
Expand All @@ -239,7 +242,7 @@ class R2Sarif {
for (const run of doc.runs) {
if (run.results) {
for (const res of run.results) {
console.log(res.ruleId)
r2.log(res.ruleId)
}
}
}
Expand All @@ -258,7 +261,7 @@ class R2Sarif {
}
listRulesForDriver(driver: Driver) : Rule[] {
var res: Rule[] = [];
console.log("# Rules for Driver: " + driver.name + " (" + driver.semanticVersion + ")")
// r2.log("# Rules for Driver: " + driver.name + " (" + driver.semanticVersion + ")")
for (const rule of driver.rules) {
this.listRule(rule);
res.push(rule);
Expand Down Expand Up @@ -307,13 +310,13 @@ class R2Sarif {
return true;
}
}
console.log("TODO ADD");
r2.log("TODO ADD");
return true;
}
}

function showHelp() {
const println = console.log;
const println = r2.log;
println(`Usage: sarif [action] [args...]
sarif add [L] [id] [M] - add a new result with selected driver
sarif alias [newalias] - create an alias for the sarif command
Expand Down Expand Up @@ -355,8 +358,8 @@ function sarifCommand(r2s: R2Sarif, cmd: string): boolean {
r2s.add(levelType, ruleId, textMessage);
}
} else {
console.log("sarif add [type] [id] [message]")
console.log("type = warning, error, note")
r2.log("sarif add [type] [id] [message]")
r2.log("type = warning, error, note")
//showHelp();
}
break;
Expand All @@ -367,7 +370,7 @@ function sarifCommand(r2s: R2Sarif, cmd: string): boolean {
const textMessage = args.slice(2).join (" ");
r2s.add("warning", ruleId, textMessage);
} else {
console.log("sarif addw [id] [message]")
r2.error("sarif addw [id] [message]")
}
break;
case 'adde':
Expand All @@ -377,7 +380,7 @@ function sarifCommand(r2s: R2Sarif, cmd: string): boolean {
const textMessage = args.slice(2).join (" ");
r2s.add("error", ruleId, textMessage);
} else {
console.log("sarif adde [id] [message]")
r2.log("sarif adde [id] [message]")
}
break;
case 'addn':
Expand All @@ -387,7 +390,7 @@ function sarifCommand(r2s: R2Sarif, cmd: string): boolean {
const textMessage = args.slice(2).join (" ");
r2s.add("note", ruleId, textMessage);
} else {
console.log("sarif addn [id] [message]")
r2.log("sarif addn [id] [message]")
}
break;
case '-A':
Expand All @@ -400,7 +403,11 @@ function sarifCommand(r2s: R2Sarif, cmd: string): boolean {
r2s.alias = args[1];
}
} else {
console.log(r2s.alias);
if (r2s.alias) {
r2.log(r2s.alias);
} else {
r2.error("Alias not defined");
}
}
break;
case '-c':
Expand Down Expand Up @@ -461,6 +468,9 @@ function sarifCommand(r2s: R2Sarif, cmd: string): boolean {
case "-V":
r2.log(r2s.version);
break;
case "dump":
r2.log(r2s.toString());
break;
case "r2":
r2.log(r2s.toScript());
break;
Expand Down
2 changes: 1 addition & 1 deletion sarif-ts/sarif/utilsgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ export function tabulateText(columns: string[], columnWidths: number[]) : string
const columnWidth = columnWidths[col++];
resultingText += column.padEnd(columnWidth);
}
return resultingText;
return resultingText.replace(/\s+$/, '');
}
4 changes: 3 additions & 1 deletion sarif-ts/sarif/utilsverify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { SourceLineLocation, SourceLocation, BinaryLocation } from "./types";

import { readFileSync } from "fs";
import { readFileSync as rfs } from "fs";
import { Ajv2020, ErrorObject, ValidateFunction } from "ajv/dist/2020"
import ajv from "ajv"
import addFormats from "ajv-formats";
Expand All @@ -12,6 +12,8 @@ import addFormats from "ajv-formats";
const sarifSchema210 = "sarif-schema-2.1.0.json";
const sarifSchema220 = "sarif-schema-2.2.0.json";

export const readFileSync = rfs;

export class SarifVerifier {
private validateDocument: ValidateFunction;
private draft06validator() : ValidateFunction{
Expand Down
15 changes: 8 additions & 7 deletions test/db/cmd/cmd_sarif
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ sarif load bins/files/sarif.rules.mastg.json
sarif list docs
EOF
EXPECT=<<EOF
Document loaded. Use 'sarif list'
0 bins/files/sarif.rules.mastg.json
+ mastg 1.0.0
EOF
EXPECT_ERR=
EXPECT_ERR=<<EOF
Document loaded. Use 'sarif list'
EOF
RUN

NAME=sarif drivers
Expand All @@ -68,20 +69,20 @@ EOF
EXPECT=<<EOF
---
drivers
0 SARIF Findings 0.0.1
---
docs
0 bins/files/sarif.driver.ns.json
+ SARIF Findings 0.0.1
---
rules
SF00001 Context Registered Broadcast Receivers Not Protected with Permissions
SF00002 Manifest Declared Broadcast Receivers Not Protected With Permissions Can Leak Data to Other Apps
SF00003 Implicitly Exported Components Block Installation to Android 12
EOF
EXPECT_ERR=<<EOF
Document loaded. Use 'sarif list'
0 SARIF Findings 0.0.1
0 bins/files/sarif.driver.ns.json
+ SARIF Findings 0.0.1
# Rules for Driver: SARIF Findings (0.0.1)
EOF
EXPECT_ERR=
RUN

NAME=sarif version
Expand Down

0 comments on commit 7af8b7f

Please sign in to comment.