Skip to content

Commit

Permalink
Extra security
Browse files Browse the repository at this point in the history
NarhwalDashboard is now the only class that can access ConstantsInt in a meaningful way(HOPEFULLY).
  • Loading branch information
Mason-Lam committed Jul 1, 2022
1 parent 40ae198 commit 4c2b48c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/main/java/frc/team3128/ConstantsInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;

import frc.team3128.common.utility.Log;

Expand All @@ -20,7 +21,7 @@ public class ConstantsInt {

private static volatile Hashtable<String, Class<?>> categories; // HashMap storing each class in the Constants class

public static volatile Hashtable<String, ArrayList<Field>> editConstants;
private static volatile Hashtable<String, ArrayList<Field>> editConstants;

//Members of the Field class used to change the finality of a field
private static Method getRoot;
Expand Down Expand Up @@ -133,6 +134,8 @@ private static Object parseData(String value) {

//Return each field of a constants class
public static synchronized ArrayList<Field> getConstantInfo(String category) {
String callerClass = Thread.currentThread().getStackTrace()[2].getClassName();
if(!callerClass.equals("frc.team3128.common.narwhaldashboard.NarwhalDashboard")) throw new IllegalArgumentException("Caller class is not valid!");
return editConstants.get(category);
}

Expand All @@ -141,7 +144,7 @@ public static synchronized void addConstant(String category, String name) throws
try {
Field field = categories.get(category).getField(name);
try {
if(editConstants.get(category).contains(field)) return;
if(editConstants.get(category).contains(field) && !Modifier.isFinal(field.getModifiers())) return;
removeFinal(field); //Make the field non-final
editConstants.get(category).add(field); //Make the field editable by the UI
}
Expand All @@ -159,4 +162,8 @@ private static void removeFinal(Field field) throws Exception {
field = (Field) getRoot.invoke(field); //get the root of the field
modifiers.setInt(field, modifiers.getInt(field) & ~Modifier.FINAL); //Remove the final modifier
}

public static Set<String> getCategories() {
return categories.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
}

JSONObject constantsObj = new JSONObject();
for(String category : ConstantsInt.editConstants.keySet()) {
for(String category : ConstantsInt.getCategories()) {
JSONArray catArr = new JSONArray();
ArrayList<Field> fields = ConstantsInt.getConstantInfo(category);
for(Field field : fields) {
Expand All @@ -170,6 +170,10 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
Log.info("Narwhal Dashboard", "Constant Of "+newConstant.toJSONString());
}
catch(IllegalAccessException e) {
JSONObject error = new JSONObject();
error.put("key","Private");
error.put("value","Constant");
catArr.add(error);
continue;
}
}
Expand Down

1 comment on commit 4c2b48c

@mika-okamoto
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably delete if(!callerClass.equals("frc.team3128.common.narwhaldashboard.NarwhalDashboard")) throw new IllegalArgumentException("Caller class is not valid!"); because its file location is moving once common repo gets set up

Please sign in to comment.