Skip to content
Merged
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
1 change: 1 addition & 0 deletions addons/recorder/XEH_prep.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ PREP(addEventIntegrations);
PREP(getStaticObjects);
PREP(trackSectors);
PREP(handleChatMessage);
PREP(redactLoginCommand);
PREP(radioEvent);

PREP(installPlayerTrackingClients);
Expand Down
3 changes: 3 additions & 0 deletions addons/recorder/fnc_handleChatMessage.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ if (parseNumber _strID > 1) then {

private _senderOcapId = _person getVariable [QGVARMAIN(id), -1];

// A #login command carries the server admin password: record the command only.
_text = [_text] call FUNC(redactLoginCommand);

[":EVENT:CHAT:", [
GVAR(captureFrameNo),
_senderOcapId,
Expand Down
56 changes: 56 additions & 0 deletions addons/recorder/fnc_redactLoginCommand.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* ----------------------------------------------------------------------------
FILE: fnc_redactLoginCommand.sqf

FUNCTION: OCAP_recorder_fnc_redactLoginCommand

Description:
Masks the server admin password in a `#login` chat command before
<OCAP_recorder_fnc_handleChatMessage> records it. The recording keeps the
command, so playback still shows that someone logged in, but everything
typed after `#login` becomes a fixed mask that does not reveal its length.

Matches the command in any case, ignoring surrounding whitespace, like the
OCAP web server, which masks the same way for recordings from older addons.
Whitespace is every Unicode White_Space character plus U+FEFF, the set the
web server trims, so a leading non-breaking space cannot hide the command.
`trim` alone would not do: it removes only spaces, tabs and line breaks.

Parameters:
_text - The chat message [String]

Returns:
[String] - The message, unchanged unless it is a `#login` command

Examples:
> ["#login hunter2"] call FUNC(redactLoginCommand); // "#login ********"
> ["radio check"] call FUNC(redactLoginCommand); // "radio check"

Public:
No

Author:
OCAP contributors
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_text", "", [""]]];

// Unicode code points of Go's unicode.IsSpace, and U+FEFF, which JavaScript's
// trim removes as well.
private _spaces = [
9, 10, 11, 12, 13, 32, 133, 160, 5760,
8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202,
8232, 8233, 8239, 8287, 12288, 65279
];

private _chars = toArray _text;
private _start = _chars findIf {!(_x in _spaces)};
if (_start < 0) exitWith {_text};

// Slices pass an explicit count: the count became optional only in Arma 2.14,
// and the recorder still supports 2.10.
private _command = toString (_chars select [_start, 6]);
if (toLower _command isNotEqualTo "#login") exitWith {_text};
private _rest = _start + 6;
if ((_chars select [_rest, count _chars - _rest]) findIf {!(_x in _spaces)} < 0) exitWith {_command};
_command + " ********"
13 changes: 13 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ or saved. The suite replaces recording state and resets per-unit recording
variables, and a recording start it simulates also satisfies the auto-start
waiter; restart the mission afterwards. It is an opt-in test artifact, not
included in the release PBOs.

`chat_redaction_regressions.sqf` checks that a `#login` chat command reaches the
extension with everything after `#login` masked, in any case and behind any
Unicode whitespace, a non-breaking space included, while ordinary chat and
`#login` inside a sentence are recorded as typed. It needs no
players and runs in a moment. Execute it on the server of the test build:

```sqf
execVM "tests\chat_redaction_regressions.sqf";
```

The RPT must end with `[OCAP chat redaction] ALL TESTS PASSED`. Extension writes
are intercepted and restored afterwards.
74 changes: 74 additions & 0 deletions tests/chat_redaction_regressions.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Run on the server in a disposable mission; see tests/README.md. */
if (!isServer) exitWith {throw "Run the tests on the server"};

private _assert = {
params ["_condition", "_description"];
if (!_condition) then {throw _description};
diag_log format ["[OCAP chat redaction] PASS: %1", _description];
};
private _savedSendData = missionNamespace getVariable "OCAP_extension_fnc_sendData";
private _savedFrame = missionNamespace getVariable "OCAP_recorder_captureFrameNo";
private _error = "";

// Intercept extension output, never upload a test recording.
OCAP_test_commands = [];
OCAP_extension_fnc_sendData = {
params ["_command", ["_args", []]];
if (_command isEqualTo ":EVENT:CHAT:") then {OCAP_test_commands pushBack +_this};
""
};
OCAP_recorder_captureFrameNo = 7;

// Characters the tests cannot type literally.
private _nbsp = toString [160];
private _ideographicSpace = toString [12288];
private _bom = toString [65279];
private _nel = toString [133];

try {
{
_x params ["_text", "_expected", "_description"];
[([_text] call OCAP_recorder_fnc_redactLoginCommand) isEqualTo _expected, _description] call _assert;
} forEach [
["#login hunter2", "#login ********", "the password after #login is masked"],
[" #LOGIN Hunter2 ", "#LOGIN ********", "the command matches in any case and around whitespace"],
["#loginhunter2", "#login ********", "a password typed without a space is masked too"],
["#login", "#login", "a bare #login has nothing to mask"],
["#login ", "#login", "trailing whitespace alone is not a password"],
["#login ********", "#login ********", "masking an already masked command changes nothing"],
["#logout", "#logout", "#logout carries no password"],
["radio check", "radio check", "ordinary chat is unchanged"],
["say #login hunter2", "say #login hunter2", "#login inside a sentence is not a command"],
["", "", "an empty message stays empty"],
[_nbsp + "#login hunter2", "#login ********", "a leading non-breaking space does not hide the command"],
[_bom + _ideographicSpace + _nel + "#LOGIN hunter2", "#LOGIN ********", "any leading Unicode whitespace is skipped"],
["#login" + _nbsp + "hunter2", "#login ********", "a non-breaking space before the password still masks it"],
["#login" + _nbsp + _ideographicSpace, "#login", "trailing Unicode whitespace alone is not a password"],
[_nbsp + "#LOGIN", "#LOGIN", "a command at the very end of the message has nothing after it to scan"],
[_nbsp + "say #login hunter2", _nbsp + "say #login hunter2", "other messages keep their whitespace"],
[_nbsp, _nbsp, "a message of whitespace alone is unchanged"]
];

[0, 3, "Synthetic Admin", " #login hunter2", objNull, "Synthetic Admin", "0", false, true, 0, 0] call OCAP_recorder_fnc_handleChatMessage;
[1, 3, "Synthetic Player", "radio check", objNull, "Synthetic Player", "0", false, true, 0, 0] call OCAP_recorder_fnc_handleChatMessage;
[0, 3, "Synthetic Admin", _nbsp + "#login hunter2", objNull, "Synthetic Admin", "0", false, true, 0, 0] call OCAP_recorder_fnc_handleChatMessage;
[count OCAP_test_commands == 3, "every chat message reaches the extension"] call _assert;
private _login = (OCAP_test_commands select 0) select 1;
[(_login select 5) isEqualTo "#login ********", "the recorded #login command carries no password"] call _assert;
[(str OCAP_test_commands) find "hunter2" == -1, "the password reaches the extension nowhere"] call _assert;
[(_login select 0) == 7 && {(_login select 2) == 0} && {(_login select 3) isEqualTo "Synthetic Admin"}, "the masked message keeps its frame, channel and sender"] call _assert;
[((OCAP_test_commands select 1) select 1 select 5) isEqualTo "radio check", "ordinary chat is recorded as typed"] call _assert;
[((OCAP_test_commands select 2) select 1 select 5) isEqualTo "#login ********", "a #login behind a non-breaking space reaches the extension masked"] call _assert;
} catch {
_error = str _exception;
};

OCAP_extension_fnc_sendData = _savedSendData;
OCAP_recorder_captureFrameNo = _savedFrame;
OCAP_test_commands = nil;

if (_error != "") then {
diag_log format ["[OCAP chat redaction] FAIL: %1", _error];
throw _error;
};
diag_log "[OCAP chat redaction] ALL TESTS PASSED";
Loading