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

fix: add some basic url validation #8

Merged
merged 1 commit into from
Dec 13, 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
15 changes: 14 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { describe, expect, it } from 'vitest';
import { isDBFile } from '../src/utils';
import { isDBFile, validateGitUrl } from '../src/utils';

describe('utils', () => {
it('matches db files', () => {
expect(isDBFile('__tests__/data/normal_2.db', ['*.db'])).toBe(true);
});

it('should allow a proper repo', () => {
expect(() => {
validateGitUrl('https://github.com/rotki/action-sqldiff.git');
validateGitUrl('[email protected]:rotki/action-sqldiff.git');
}).to.not.throw();
});

it('should not allow a non repo', () => {
expect(() => {
validateGitUrl('smb://rotki');
}).to.throw(Error, 'Invalid repository URL: \'smb://rotki\'');
});
});
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import * as github from '@actions/github';
import * as core from '@actions/core';
import { getFiles, getGithubToken } from './input';
import { NotAPullRequestError } from './errors';
import { isDBFile } from './utils';
import { isDBFile, validateGitUrl } from './utils';
import { createDBDir } from './fs';
import type { ComparedDatabases, FileVersions } from './types';
import type { PullRequest } from '@octokit/webhooks-types/schema';

function cloneRepo(target: 'head' | 'base', url: string, ref: string, sha: string): string {
let cloneUrl = url;

validateGitUrl(cloneUrl);

if (cloneUrl.startsWith('git'))
cloneUrl = cloneUrl.replace('git', 'https');

Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ import mm from 'micromatch';
export function isDBFile(filename: string, patterns: string[]): boolean {
return mm.isMatch(filename, patterns, { basename: true });
}

export function validateGitUrl(url: string): void {
const urlRegex = /^(https?:\/\/|git@[\w.-]+:)[\w#%+./:=@~-]+(.git)?$/;
if (!urlRegex.test(url))
throw new Error(`Invalid repository URL: '${url}'`);
}
Loading