Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ Fixed
* Fixed generation of `st2.conf.sample` to show correct syntax for `[sensorcontainer].partition_provider` (space separated `key:value` pairs). #5710
Contributed by @cognifloyd

* A new output schema using full JSON schema was introduced and secrets previously masked using
the legacy output schema are now being displayed as plain text. To prevent security relative
issues, add backward compatibility to secret masking. Full output schema validation will need
to be migrated to the new schema.

Contributed by @m4dcoder

Added
~~~~~

Expand Down
42 changes: 39 additions & 3 deletions st2common/st2common/util/output_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,39 @@ def _output_schema_is_valid(_schema):
if not isinstance(_schema, Mapping):
# malformed schema
return False

if "type" not in _schema:
# legacy schema format
return False

try:
# the validator is smart enough to handle
# schema that is similar to the input schema
schema.validate(
_schema,
schema.get_action_output_schema(),
cls=schema.get_validator("custom"),
)
except jsonschema.ValidationError as e:
LOG.debug("output_schema not valid: %s", e)
# likely a legacy partial object schema (only defines properties)
return False

return True


def _normalize_legacy_output_schema(_schema):
if not isinstance(_schema, Mapping):
return _schema

_normalized_schema = {
"type": "object",
"properties": _schema,
"additionalProperties": True,
}

return _normalized_schema


def _validate_runner(runner_schema, result):
LOG.debug("Validating runner output: %s", runner_schema)

Expand Down Expand Up @@ -183,15 +203,31 @@ def mask_secret_output(ac_ex, output_value):
or output_key not in output_value
# no action output_schema defined
or not output_schema
# malformed action output_schema
or not _output_schema_is_valid(output_schema)
):
# nothing to mask
return output_value

# backward compatibility for legacy output_schema so secrets stay masked
if not _output_schema_is_valid(output_schema):
# normalized the legacy schema to a full JSON schema and check if it is valid
normalized_output_schema = _normalize_legacy_output_schema(output_schema)

if not _output_schema_is_valid(normalized_output_schema):
# nothing to mask
return output_value

# mask secret for the legacy output schema
output_value[output_key] = _get_masked_value(
normalized_output_schema, output_value[output_key]
)

return output_value

# mask secret for the output schema
output_value[output_key] = _get_masked_value(
output_schema, output_value[output_key]
)

return output_value


Expand Down
11 changes: 9 additions & 2 deletions st2common/tests/unit/test_util_output_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,15 @@ def test_mask_secret_output_noop_legacy_schema(self):
"output_schema": RUNNER_OUTPUT_SCHEMA,
},
}
ac_ex_result = {"output_1": "foobar"}
expected_masked_output = {"output_1": "foobar"}

ac_ex_result = {OUTPUT_KEY: {"output_1": "foobar", "output_3": "fubar"}}

expected_masked_output = {
OUTPUT_KEY: {
"output_1": "foobar",
"output_3": MASKED_ATTRIBUTE_VALUE,
}
}

# Legacy schemas should be ignored since they aren't full json schemas.
masked_output = output_schema.mask_secret_output(ac_ex, ac_ex_result)
Expand Down