pc2ccs / pc2v9

Version 9 of the PC^2 Programming Contest Control System
Eclipse Public License 2.0
47 stars 23 forks source link

Backslash in Remote Team Name throws Exception in Shadow Scoreboard Comparison Generator #576

Open clevengr opened 2 years ago

clevengr commented 2 years ago

Describe the issue:

If a team puts a backslash character in their team name, this causes the ShadowScoreboardComparisonGenerator class to throw an exception instead of computing a proper scoreboard comparison for display.

To Reproduce:

Expected behavior:

The client should display a scoreboard comparison.

Actual behavior: No scoreboard comparison is displayed; the shadow code throws an exception.

Environment:

This bug was seen during the BAPC 2022 contest while running a shadow on a Windows 10.1 system with Java 1.8_201. However, it's likely that it happens on all platforms.

Log Info: BAPC2022.Shadow.Log.Exception.excerpt.txt

Screenshots: N/A

Additional context:

The place where the exception occurs is at line 239 in ShadowScoreboardComparisonGenerator

            teamList = mapper.readValue(teamsJson, new TypeReference<List<Team>>() {});

Here, "mapper" is an instance of com.fasterxml.jackson.databind.ObjectMapper; apparently the Jackson ObjectMapper cannot handle having a backslash in its input JSON.

One possible solution would be to apply a filter to the teamsJson string being passed to the ObjectMapper, removing all "backslash" characters. (This would have the side effect of "changing" a team's name, which means it would have to also be changed in the local PC2 team description in order to obtain matches).

A more robust solution would be to figure out how (if it's even possible) to get the Jackson ObjectMapper to accept backslash characters (perhaps with some "escape" mechanism?)

johnbrvc commented 2 years ago

A question was asked: Why when the offending backslash is removed from the team name on PC2, doesn't the compare routine still crash when it processes the same team name from the remote "teams" API endpoint?

The reason is: the remote endpoint escapes the backslash with another backslash in the JSON response to the "teams" endpoint. eg: "name":"Duck-Team\\%" PC2 fetches its teams JSON internally by getting the account list, and hand-crafting a JSON "teams" String. The problem is, the team name is stored in PC2 as: Duck-Team\%, correctly with the single backslash (since it was escaped when the team names were added). So, PC2 creates the "teams" JSON like this: "name":"Duck-Team\%" (which is incorrect) This causes the JSON parser to cause the exception.

To address this, the shadowController.getPC2TeamsJSON()routine should be fixed to insert escapes into the JSON it generates for any necessary characters, such as backslash before it generates the "name" attribute value.

NOTE: this should issue should probably be checked any place that PC2 generates JSON (like its API endpoints), to make sure it properly escapes things such as backslash.