Skip to content

Commit

Permalink
fix: check if uri scheme is correct. fixes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
oae committed Sep 11, 2022
1 parent 2cfbef5 commit 91e400f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/utils/panoItemFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import yaml from 'highlight.js/lib/languages/yaml';

import { Pixbuf } from '@gi-types/gdkpixbuf2';
import { File, FileCreateFlags } from '@gi-types/gio2';
import { ChecksumType, compute_checksum_for_bytes } from '@gi-types/glib2';
import { ChecksumType, compute_checksum_for_bytes, UriFlags, uri_parse } from '@gi-types/glib2';
import { CodePanoItem } from '@pano/components/codePanoItem';
import { ColorPanoItem } from '@pano/components/colorPanoItem';
import { FilePanoItem } from '@pano/components/filePanoItem';
Expand Down Expand Up @@ -93,6 +93,14 @@ const SUPPORTED_LANGUAGES = [

const debug = logger('pano-item-factory');

const isValidUrl = (text: string) => {
try {
return isUrl(text) && uri_parse(text, UriFlags.NONE) !== null;
} catch (err) {
return false;
}
};

const findOrCreateDbItem = async (clip: ClipboardContent): Promise<DBItem | null> => {
const { value, type } = clip.content;
const queryBuilder = new ClipboardQueryBuilder();
Expand Down Expand Up @@ -156,7 +164,7 @@ const findOrCreateDbItem = async (clip: ClipboardContent): Promise<DBItem | null
}),
});
case ContentType.TEXT:
if (value.trim().toLowerCase().startsWith('http') && isUrl(value)) {
if (value.trim().toLowerCase().startsWith('http') && isValidUrl(value)) {
const { description, imageUrl, title } = await getDocument(value);
const [checksum] = await getImage(imageUrl);

Expand Down

0 comments on commit 91e400f

Please sign in to comment.