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

Adding check to make sure customMetaTags are valid #208

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 6.3.4

- Adding check to make sure `customMetaTags` are valid
- Updating dependencies

## 6.3.3

- Updating dependencies
Expand Down
2 changes: 2 additions & 0 deletions dist/lib/openGraphScraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ async function setOptionsAndReturnOpenGraphResults(ogsOptions) {
const formattedUrl = (0, utils_1.validateAndFormatURL)(options.url || '', (options.urlValidatorSettings || utils_1.defaultUrlValidatorSettings));
if (!formattedUrl.url)
throw new Error('Invalid URL');
if (!(0, utils_1.isCustomMetaTagsValid)(options.customMetaTags || []))
throw new Error('Invalid Custom Meta Tags');
options.url = formattedUrl.url;
// trying to limit non html pages
if ((0, utils_1.isThisANonHTMLUrl)(options.url))
Expand Down
3 changes: 1 addition & 2 deletions dist/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type ValidatorSettings = {
* @typeParam {string} property - meta tag name/property attribute
* @typeParam {string} fieldName - name of the result variable
*/
type CustomMetaTags = {
export type CustomMetaTags = {
fieldName: string;
multiple: boolean;
property: string;
Expand Down Expand Up @@ -271,4 +271,3 @@ export type OgObjectInteral = {
updatedTime?: string;
};
export type OgObject = Omit<OgObjectInteral, 'musicSongDisc' | 'musicSongProperty' | 'musicSongTrack' | 'musicSongUrl' | 'ogImageHeight' | 'ogImageProperty' | 'ogImageSecureURL' | 'ogImageType' | 'ogImageURL' | 'ogImageWidth' | 'ogVideoHeight' | 'ogVideoProperty' | 'ogVideoType' | 'ogVideoWidth' | 'twitterImageAlt' | 'twitterImageHeight' | 'twitterImageProperty' | 'twitterImageSrc' | 'twitterImageWidth' | 'twitterPlayerHeight' | 'twitterPlayerProperty' | 'twitterPlayerStream' | 'twitterPlayerWidth'>;
export {};
10 changes: 9 additions & 1 deletion dist/lib/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ValidatorSettings, OpenGraphScraperOptions } from './types';
import type { CustomMetaTags, ValidatorSettings, OpenGraphScraperOptions } from './types';
export declare const defaultUrlValidatorSettings: {
allow_fragments: boolean;
allow_protocol_relative_urls: boolean;
Expand Down Expand Up @@ -79,3 +79,11 @@ export declare function removeNestedUndefinedValues(object: {
export declare function optionSetup(ogsOptions: OpenGraphScraperOptions): {
options: OpenGraphScraperOptions;
};
/**
* Checks if image type is valid
*
* @param {string} type - type to be checked
* @return {boolean} boolean value if type is value
*
*/
export declare function isCustomMetaTagsValid(customMetaTags: CustomMetaTags[]): boolean;
29 changes: 28 additions & 1 deletion dist/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.optionSetup = exports.removeNestedUndefinedValues = exports.isThisANonHTMLUrl = exports.isImageTypeValid = exports.findImageTypeFromUrl = exports.validateAndFormatURL = exports.isUrlValid = exports.defaultUrlValidatorSettings = void 0;
exports.isCustomMetaTagsValid = exports.optionSetup = exports.removeNestedUndefinedValues = exports.isThisANonHTMLUrl = exports.isImageTypeValid = exports.findImageTypeFromUrl = exports.validateAndFormatURL = exports.isUrlValid = exports.defaultUrlValidatorSettings = void 0;
const validator_1 = __importDefault(require("validator"));
exports.defaultUrlValidatorSettings = {
allow_fragments: true,
Expand Down Expand Up @@ -121,3 +121,30 @@ function optionSetup(ogsOptions) {
return { options };
}
exports.optionSetup = optionSetup;
/**
* Checks if image type is valid
*
* @param {string} type - type to be checked
* @return {boolean} boolean value if type is value
*
*/
function isCustomMetaTagsValid(customMetaTags) {
if (!Array.isArray(customMetaTags))
return false;
let result = true;
customMetaTags.forEach((customMetaTag) => {
if (typeof customMetaTag === 'object') {
if (!('fieldName' in customMetaTag) || typeof customMetaTag.fieldName !== 'string')
result = false;
if (!('multiple' in customMetaTag) || typeof customMetaTag.multiple !== 'boolean')
result = false;
if (!('property' in customMetaTag) || typeof customMetaTag.property !== 'string')
result = false;
}
else {
result = false;
}
});
return result;
}
exports.isCustomMetaTagsValid = isCustomMetaTagsValid;
3 changes: 3 additions & 0 deletions lib/openGraphScraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import extractMetaTags from './extract';
import requestAndResultsFormatter from './request';
import {
defaultUrlValidatorSettings,
isCustomMetaTagsValid,
isThisANonHTMLUrl,
optionSetup,
validateAndFormatURL,
Expand Down Expand Up @@ -30,6 +31,8 @@ export default async function setOptionsAndReturnOpenGraphResults(ogsOptions: Op

if (!formattedUrl.url) throw new Error('Invalid URL');

if (!isCustomMetaTagsValid(options.customMetaTags || [])) throw new Error('Invalid Custom Meta Tags');

options.url = formattedUrl.url;

// trying to limit non html pages
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type ValidatorSettings = {
* @typeParam {string} property - meta tag name/property attribute
* @typeParam {string} fieldName - name of the result variable
*/
type CustomMetaTags = {
export type CustomMetaTags = {
fieldName: string;
multiple: boolean;
property: string;
Expand Down
26 changes: 25 additions & 1 deletion lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import validator from 'validator';
import type { ValidatorSettings, OpenGraphScraperOptions } from './types';
import type { CustomMetaTags, ValidatorSettings, OpenGraphScraperOptions } from './types';

export const defaultUrlValidatorSettings = {
allow_fragments: true,
Expand Down Expand Up @@ -117,3 +117,27 @@ export function optionSetup(ogsOptions: OpenGraphScraperOptions): { options: Ope

return { options };
}

/**
* Checks if image type is valid
*
* @param {string} type - type to be checked
* @return {boolean} boolean value if type is value
*
*/
export function isCustomMetaTagsValid(customMetaTags: CustomMetaTags[]): boolean {
if (!Array.isArray(customMetaTags)) return false;

let result = true;
customMetaTags.forEach((customMetaTag) => {
if (typeof customMetaTag === 'object') {
if (!('fieldName' in customMetaTag) || typeof customMetaTag.fieldName !== 'string') result = false;
if (!('multiple' in customMetaTag) || typeof customMetaTag.multiple !== 'boolean') result = false;
if (!('property' in customMetaTag) || typeof customMetaTag.property !== 'string') result = false;
} else {
result = false;
}
});

return result;
}
Loading
Loading