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

Validate props in retrospective post (aider assisted) #1973

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

esarafianou
Copy link

Summary

Validate props in the retrospective post

Ticket Link

https://mattermost.atlassian.net/browse/MM-61164

Checklist

  • [ ] Telemetry updated
  • [ ] Gated by experimental feature flag
  • [ ] Unit tests updated

Copy link
Member

@jespino jespino left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

@larkox larkox left a comment

Choose a reason for hiding this comment

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

Sorry by the drive by review, but since I handled something similar not long ago, I thought I would jump in.

Comment on lines 36 to 37
const metricsConfigs: Array<Metric> = props.post.props?.metricsConfigs ? JSON.parse(props.post.props.metricsConfigs) : [];
const metricsData: Array<RunMetricData> = props.post.props?.metricsData ? JSON.parse(props.post.props.metricsData) : [];
Copy link

@larkox larkox Dec 18, 2024

Choose a reason for hiding this comment

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

This validation is probably not enough. The following inputs should break this:

  • A non JSON parseable string value like "{"
  • A non string value like {}
  • A non list JSON string like = "{}"

If we want to be 100% safe, we would need to add some try catch (or create a "safeJSONParse" function that does the try catch for us), and some type guard.

const parsedMetricsConfig = safeJSONParse(props.post.props?.metricsConfigs);
const parsedMetricsData = safeJSONParse(props.post.props?.metricsData);

const metricsConfigs = isArrayOf(parsedMetricsConfig, isMetric) ? parsedMetricsConfig : [];
const metricsData = isArrayOf(parsedMetricsData, isMetricsData) ? parsedMetricsData : [];

Copy link
Member

Choose a reason for hiding this comment

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

good catch

Copy link

@larkox larkox left a comment

Choose a reason for hiding this comment

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

LGTM, just a couple of nitpicks you can ignore.

@@ -132,3 +132,40 @@ export function runCallsSlashCommand(command: string, channelId: string, teamId:
},
}, window.origin);
}

export function safeJSONParse<T>(value: unknown): T | null {
Copy link

Choose a reason for hiding this comment

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

Nitpick: I like the semantics of undefined better than null, but it is just about taste.

@@ -132,3 +132,40 @@ export function runCallsSlashCommand(command: string, channelId: string, teamId:
},
}, window.origin);
}

export function safeJSONParse<T>(value: unknown): T | null {
if (!value || typeof value !== 'string') {
Copy link

Choose a reason for hiding this comment

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

I think this would be enough:

Suggested change
if (!value || typeof value !== 'string') {
if (typeof value !== 'string') {

We can let the case of empty string being handled by JSON.parse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants