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

Community name validation #9159

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions libs/model/src/community/GetCommunities.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ export function GetCommunities(): Query<typeof schemas.GetCommunities> {
}
FROM "Communities" AS "Community"
WHERE "Community"."active" = true
${
relevance_by === 'membership'
? `
AND name NOT LIKE '%<%'
AND name NOT LIKE '%>%'
AND name NOT LIKE '%\`%'
AND name NOT LIKE '%"%'
AND name NOT LIKE '%''%'
`
: ''
}
${
base
? `
Expand Down
6 changes: 5 additions & 1 deletion libs/schemas/src/commands/community.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import {
ALL_COMMUNITIES,
ChainBase,
ChainType,
COMMUNITY_NAME_REGEX,
MAX_SCHEMA_INT,
MIN_SCHEMA_INT,
} from '@hicommonwealth/shared';
import { z } from 'zod';
import { Community, Group, StakeTransaction } from '../entities';
import { PG_INT, checkIconSize } from '../utils';
import { checkIconSize, PG_INT } from '../utils';

export const CreateCommunity = {
input: z.object({
id: z.string(),
name: z
.string()
.max(255)
.regex(COMMUNITY_NAME_REGEX, {
message: `Invalid name, only a-z, A-Z, 0-9, !, @, #, &, (, ), :, _, $, /, \\, |, . and single spaces are allowed`,
})
.refine((data) => !data.includes(ALL_COMMUNITIES), {
message: `String must not contain '${ALL_COMMUNITIES}'`,
}),
Expand Down
1 change: 1 addition & 0 deletions libs/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './canvas';
export * as commonProtocol from './commonProtocol';
export * from './constants';
export * from './regex';
export * from './types';
export * from './utils';
3 changes: 3 additions & 0 deletions libs/shared/src/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const COMMUNITY_NAME_REGEX =
// eslint-disable-next-line no-useless-escape
/^(?!.* {2})[a-zA-Z0-9!@#&():_$\/\\|\.]+(?: [a-zA-Z0-9!@#&():_$\/\\|\.]+)*$/;
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { COMMUNITY_NAME_REGEX } from '@hicommonwealth/shared';
import { VALIDATION_MESSAGES } from 'helpers/formValidations/messages';
import z from 'zod';

export const communityProfileValidationSchema = z.object({
communityName: z
.string({ invalid_type_error: VALIDATION_MESSAGES.NO_INPUT })
.nonempty({ message: VALIDATION_MESSAGES.NO_INPUT })
.max(255, { message: VALIDATION_MESSAGES.MAX_CHAR_LIMIT_REACHED }),
.max(255, { message: VALIDATION_MESSAGES.MAX_CHAR_LIMIT_REACHED })
.regex(COMMUNITY_NAME_REGEX, {
message: `Invalid name, only a-z, A-Z, 0-9, !, @, #, &, (, ), :, _, $, /, \\, |, . and single spaces are allowed`,
}),
communityDescription: z
.string({ invalid_type_error: VALIDATION_MESSAGES.NO_INPUT })
.nonempty({ message: VALIDATION_MESSAGES.NO_INPUT }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { COMMUNITY_NAME_REGEX } from '@hicommonwealth/shared';
import { VALIDATION_MESSAGES } from 'helpers/formValidations/messages';
import z from 'zod';

export const basicInformationFormValidationSchema = z.object({
communityName: z
.string({ invalid_type_error: VALIDATION_MESSAGES.NO_INPUT })
.nonempty({ message: VALIDATION_MESSAGES.NO_INPUT })
.max(255, { message: VALIDATION_MESSAGES.MAX_CHAR_LIMIT_REACHED }),
.max(255, { message: VALIDATION_MESSAGES.MAX_CHAR_LIMIT_REACHED })
.regex(COMMUNITY_NAME_REGEX, {
message: `Invalid name, only a-z, A-Z, 0-9, !, @, #, &, (, ), :, _, $, /, \\, |, . and single spaces are allowed`,
}),
communityDescription: z
.string({ invalid_type_error: VALIDATION_MESSAGES.NO_INPUT })
.nonempty({ message: VALIDATION_MESSAGES.NO_INPUT })
Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, actually i move the same logic to get communities query https://github.com/hicommonwealth/commonwealth/pull/9159/files#diff-b60b043e77bd821e2ae44ad3a9ff88df3c70dda2f138c77d9568ec4ebeac3e38
to handle these existing communities cases

Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ export const TrendingCommunitiesPreview = () => {
const trendingCommunities = (
paginatedTrendingCommunities?.pages?.[0]?.results || []
)
.filter((community) => {
// TODO: This XSS logic should be moved to API + we shouldn't allow users to choose community
// names with invalid chars
const name = community.name.toLowerCase();
//this filter is meant to not include any de facto communities that are actually xss attempts.
//It's a way of keeping the front facing parts of the app clean looking for users
return (
!['"', '>', '<', "'", '/', '`'].includes(name[0]) &&
!['"', '>', '<', "'", '/', '`'].includes(name[1])
);
})
.map((community) => ({
community,
isMember: Permissions.isCommunityMember(community.id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {
CommunityAttributes,
UserInstance,
} from '@hicommonwealth/model';
import { ChainBase } from '@hicommonwealth/shared';
import { ChainBase, COMMUNITY_NAME_REGEX } from '@hicommonwealth/shared';
import { MixpanelCommunityInteractionEvent } from '../../../shared/analytics/types';
import { urlHasValidHTTPPrefix } from '../../../shared/utils';
import { ALL_COMMUNITIES } from '../../middleware/databaseValidationService';
import { TrackOptions } from '../server_analytics_controller';
import { ServerCommunitiesController } from '../server_communities_controller';

export const Errors = {
InvalidCommunityName:
'Invalid name, only a-z, A-Z, 0-9, !, @, #, &, (, ), :, _, $, /, \\, |, . and single spaces are allowed',
mzparacha marked this conversation as resolved.
Show resolved Hide resolved
NotLoggedIn: 'Not signed in',
NoCommunityId: 'Must provide community ID',
ReservedId: 'The id is reserved and cannot be used',
Expand Down Expand Up @@ -111,6 +113,10 @@ export async function __updateCommunity(
transactionHash,
} = rest;

if (name !== undefined && !COMMUNITY_NAME_REGEX.test(name)) {
throw new AppError(Errors.InvalidCommunityName);
}

// Handle single string case and undefined case
let { snapshot } = rest;
if (snapshot !== undefined && typeof snapshot === 'string') {
Expand Down
Loading