Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix better output on help page #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/java/ysoserial/GeneratePayload.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ysoserial;

import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import ysoserial.payloads.ObjectPayload;
Expand All @@ -13,7 +14,7 @@ public class GeneratePayload {
private static final int INTERNAL_ERROR_CODE = 70;
private static final int USAGE_CODE = 64;

public static void main(final String[] args) {
public static void main(final String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (args.length != 2) {
printUsage();
System.exit(USAGE_CODE);
Expand Down Expand Up @@ -43,7 +44,7 @@ public static void main(final String[] args) {
System.exit(0);
}

private static void printUsage() {
private static void printUsage() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
System.err.println("Y SO SERIAL?");
System.err.println("Usage: java -jar ysoserial-[version]-all.jar [payload] '[command]'");
System.err.println(" Available payload types:");
Expand All @@ -56,10 +57,10 @@ private static void printUsage() {
rows.add(new String[] {"Payload", "Authors", "Dependencies"});
rows.add(new String[] {"-------", "-------", "------------"});
for (Class<? extends ObjectPayload> payloadClass : payloadClasses) {
rows.add(new String[] {
rows.add(new String[]{
payloadClass.getSimpleName(),
Strings.join(Arrays.asList(Authors.Utils.getAuthors(payloadClass)), ", ", "@", ""),
Strings.join(Arrays.asList(Dependencies.Utils.getDependenciesSimple(payloadClass)),", ", "", "")
Strings.join(Arrays.asList(Dependencies.Utils.getDependenciesSimple(payloadClass)), ", ", "", "")
});
}

Expand Down
23 changes: 20 additions & 3 deletions src/main/java/ysoserial/Strings.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ysoserial;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;

import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -29,9 +29,25 @@ public static String repeat(String str, int num) {

public static List<String> formatTable(List<String[]> rows) {
final Integer[] maxLengths = new Integer[rows.get(0).length];
for (String[] row : rows) {
// Max column width, will try to split on spaces if possible
// Uses 210 as default terminal width, if not specified as system property
final int maxColumnWidth = Integer.getInteger("terminalWidth", 210) / maxLengths.length;
for(int index = 0; index < rows.size(); index++) {
String[] row = rows.get(index);
if (maxLengths.length != row.length) throw new IllegalStateException("mismatched columns");
for (int i = 0; i < maxLengths.length; i++) {
if (row[i].length() > maxColumnWidth) {
row[i] = WordUtils.wrap(row[i], maxColumnWidth);
String[] split = row[i].split("\n");
rows.get(index)[i] = split[0];
for (int ii = 1; ii < split.length; ii++) {
if (rows.size() <= index + ii || rows.get(index + ii)[i].length() > 0) {
rows.add(index + ii, new String[maxLengths.length]);
Arrays.fill(rows.get(index + ii), "");
}
rows.get(index + ii)[i] = split[ii];
}
}
if (maxLengths[i] == null || maxLengths[i] < row[i].length()) {
maxLengths[i] = row[i].length();
}
Expand All @@ -40,7 +56,8 @@ public static List<String> formatTable(List<String[]> rows) {

final List<String> lines = new LinkedList<String>();
for (String[] row : rows) {
for (int i = 0; i < maxLengths.length; i++) {
// No need to fill last line with spaces
for (int i = 0; i < maxLengths.length - 1; i++) {
final String pad = repeat(" ", maxLengths[i] - row[i].length());
row[i] = row[i] + pad;
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/ysoserial/payloads/annotation/Dependencies.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package ysoserial.payloads.annotation;

import ysoserial.payloads.DynamicDependencies;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Dependencies {
String[] value() default {};

public static class Utils {
public static String[] getDependencies(AnnotatedElement annotated) {
Dependencies deps = annotated.getAnnotation(Dependencies.class);
public static String[] getDependencies(Class<?> clazz) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
if ( DynamicDependencies.class.isAssignableFrom(clazz) ) {
Method method = clazz.getMethod("getDependencies");
return (String[]) method.invoke(null);
}
Dependencies deps = clazz.getAnnotation(Dependencies.class);
if (deps != null && deps.value() != null) {
return deps.value();
} else {
return new String[0];
}
}

public static String[] getDependenciesSimple(AnnotatedElement annotated) {
String[] deps = getDependencies(annotated);
public static String[] getDependenciesSimple(Class<?> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
String[] deps = getDependencies(clazz);
String[] simple = new String[deps.length];
for (int i = 0; i < simple.length; i++) {
simple[i] = deps[i].split(":", 2)[1];
Expand Down
9 changes: 1 addition & 8 deletions src/test/java/ysoserial/test/payloads/PayloadsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,7 @@ private static boolean checkPrecondition ( Class<? extends ObjectPayload<?>> pc,


private static String[] buildDeps ( final Class<? extends ObjectPayload<?>> payloadClass ) throws Exception {
String[] baseDeps;
if ( DynamicDependencies.class.isAssignableFrom(payloadClass) ) {
Method method = payloadClass.getMethod("getDependencies");
baseDeps = (String[]) method.invoke(null);
}
else {
baseDeps = Dependencies.Utils.getDependencies(payloadClass);
}
String[] baseDeps = Dependencies.Utils.getDependencies(payloadClass);
if ( System.getProperty("properXalan") != null ) {
baseDeps = Arrays.copyOf(baseDeps, baseDeps.length + 1);
baseDeps[ baseDeps.length - 1 ] = "xalan:xalan:2.7.2";
Expand Down