Skip to content

Commit

Permalink
fix: update appsync simulators ip address validation (#8375)
Browse files Browse the repository at this point in the history
* fix: update appsync simulators ip address validation

fix #8359

* address review comment
  • Loading branch information
yuth committed Oct 21, 2021
1 parent 8a413c9 commit 4b494fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
@@ -0,0 +1,15 @@
import { URL } from 'url';
import { scalars } from '../../schema/appsync-scalars';
describe('AWSIPAddress parse', () => {
it('should parse valid ip address', () => {
expect(scalars.AWSIPAddress.parseValue('127.0.0.1')).toEqual('127.0.0.1');
});

it('should parse IPV6 address', () => {
expect(scalars.AWSIPAddress.parseValue('::ffff:127.0.0.1')).toEqual('::ffff:127.0.0.1');
});

it('should throw error when its not a valid ip address', () => {
expect(() => scalars.AWSIPAddress.parseValue('not valid ip')).toThrowError('Value is not a valid IP address:');
});
});
@@ -1,12 +1,12 @@
import { URL } from 'url';
import { GraphQLInt, GraphQLScalarType, GraphQLError, Kind, StringValueNode, ValueNode } from 'graphql';
import { isValidNumber } from 'libphonenumber-js';
import * as net from 'net';

import { GraphQLDate, GraphQLTime, GraphQLDateTime } from 'graphql-iso-date';

const EMAIL_ADDRESS_REGEX = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
const IPV4_REGEX = /^(?:(?:(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(?:\/(?:[0-9]|[1-2][0-9]|3[0-2]))?)$/;
const IPV6_REGEX = /^(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:0?0?[0-9]|0?[1-9][0-9]|1[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}|(?:(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)(?:\/(?:0?0?[0-9]|0?[1-9][0-9]|1[01][0-9]|12[0-8]))?)$/;
const EMAIL_ADDRESS_REGEX =
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;

// Some of the custom scalars in this file are inspired by the graphql-scalars npm module.

Expand Down Expand Up @@ -118,13 +118,11 @@ const validateIPAddress = value => {
if (typeof value !== 'string') {
throw new TypeError(`Value is not string: ${value}`);
}
if (!IPV4_REGEX.test(value)) {
throw new TypeError(`Value is not a valid IPv4 address: ${value}`);
}
if (!IPV6_REGEX.test(value)) {
throw new TypeError(`Value is not a valid IPv6 address: ${value}`);
if (net.isIPv4(value) || net.isIPv6(value)) {
return value;
}
return value;

throw new TypeError(`Value is not a valid IP address: ${value}`);
};

const AWSIPAddress = new GraphQLScalarType({
Expand Down

0 comments on commit 4b494fc

Please sign in to comment.