Skip to content

Commit 65f0268

Browse files
authored
Block invalid identifier (#895)
* block leading and trailing whitespace * block leading and trailing whitespace, tabs and line feeds, and include code comments explaining restrictions * update tests for updated invalid value message, add test for invalid padding
1 parent da0e9e2 commit 65f0268

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

app/apollo/models/const.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ const DIRECTIVE_LIMITS = {
8181
MAX_JSON_ITEMS: config.has('directive_limits.max_json_items') ? config.get('directive_limits.max_json_items') : 128,
8282
MAX_CLUSTER_ARRAY_LENGTH: CLUSTER_MAX_TOTAL_LIMIT,
8383
MAX_GROUP_ARRAY_LENGTH: config.has('directive_limits.max_group_array_length') ? config.get('directive_limits.max_group_array_length') : 32,
84-
INVALID_PATTERN: /[<>$%&!@()}{"#]{1,}/,
84+
/*
85+
A string is invalid if starts with whitespace, OR contains an invalid character from the list, OR ends with whitespace
86+
Currently the same restriction is applied to all fields (see directives.js), but not all attributes need to restrict the characterset the same way.
87+
Additional code refactoring would be required to explicitly test identifiers with different patterns than other fields.
88+
The error messages emitted suggest only "alphabets, numbers, underscore and hyphen" are allowed, but this pattern does not accurately enforce that.
89+
Consider adding '`\[\]\\\/*^. as additional invalid chars for identifiers, but until refactored thoroughly this will negatively affect other values.
90+
E.g. ConfigurationVersion "type" attribute value "application/yaml" needs to contain a "/" and "description" attributes can be more freeform.
91+
*/
92+
INVALID_PATTERN: /^\s|[<>$%&!@()}{"#\t\n\r]{1,}|\s$/,
8593
};
8694

8795
// console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV') + `, DIRECTIVE_LIMITS: ${JSON.stringify(DIRECTIVE_LIMITS)}`);

app/apollo/test/channel.spec.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,25 @@ describe('channel graphql test suite', () => {
320320
name: 'a_illegal_char#',
321321
});
322322
console.log(`${JSON.stringify(data.data)}`);
323-
expect(data.data.errors[0].message).to.have.string('should only contain alphabets, numbers, underscore and hyphen');
323+
expect(data.data.errors[0].message).to.have.string('should avoid leading or trailing whitespace and only contain alphabets, numbers, underscore and hyphen');
324+
} catch (error) {
325+
if (error.response) {
326+
console.error('error encountered: ', error.response.data);
327+
} else {
328+
console.error('error encountered: ', error);
329+
}
330+
throw error;
331+
}
332+
});
333+
334+
it('add a channel with illegal whitespace', async () => {
335+
try {
336+
const data = await channelApi.addChannel(adminToken, {
337+
orgId: org01._id,
338+
name: ' a_illegal_pad ',
339+
});
340+
console.log(`${JSON.stringify(data.data)}`);
341+
expect(data.data.errors[0].message).to.have.string('should avoid leading or trailing whitespace and only contain alphabets, numbers, underscore and hyphen');
324342
} catch (error) {
325343
if (error.response) {
326344
console.error('error encountered: ', error.response.data);

app/apollo/test/group.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ describe('groups graphql test suite', () => {
603603
uuid: group_02_uuid,
604604
clusters: ['cluster_01', 'cluster_04$']
605605
});
606-
expect(data.data.errors[0].message).to.have.string('should only contain alphabets, numbers, underscore and hyphen');
606+
expect(data.data.errors[0].message).to.have.string('should avoid leading or trailing whitespace and only contain alphabets, numbers, underscore and hyphen');
607607
} catch (error) {
608608
if (error.response) {
609609
console.error('error encountered: ', error.response.data);

app/apollo/utils/directives.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class IdentifierSanitizer extends Sanitizer {
7272
}
7373
if (this.arg !== 'content') {
7474
if (DIRECTIVE_LIMITS.INVALID_PATTERN.test(value)) {
75-
throw new ValidationError(`The ${this.arg}'s value '${value}' should only contain alphabets, numbers, underscore and hyphen`);
75+
throw new ValidationError(`The ${this.arg}'s value '${value}' should avoid leading or trailing whitespace and only contain alphabets, numbers, underscore and hyphen`);
7676
}
7777
}
7878
}
@@ -137,7 +137,7 @@ class JsonSanitizer extends Sanitizer {
137137
throw new ValidationError(`The json object element ${child} exceeded the value length ${DIRECTIVE_LIMITS.MAX_JSON_VALUE_LENGTH}`);
138138
}
139139
if (DIRECTIVE_LIMITS.INVALID_PATTERN.test(parent[child])) {
140-
throw new ValidationError(`The ${this.arg} value ${parent[child]} should only contain alphabets, numbers, underscore and hyphen`);
140+
throw new ValidationError(`The ${this.arg} value ${parent[child]} should avoid leading or trailing whitespace and only contain alphabets, numbers, underscore and hyphen`);
141141
}
142142
}
143143
}
@@ -185,4 +185,4 @@ class JsonDirective extends SchemaDirectiveVisitor {
185185
}
186186
}
187187

188-
module.exports = { IdentifierDirective, JsonDirective };
188+
module.exports = { IdentifierDirective, JsonDirective };

0 commit comments

Comments
 (0)