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

Submit by String (master) #1083

Merged
merged 3 commits into from
May 27, 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
113 changes: 113 additions & 0 deletions src/helpers/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { describe, expect, it } from '@jest/globals';
import { ConfigurationDefinition } from 'components/hooks/useMyUser';
import {
bytesToSize,
getFileName,
getProvider,
getSubmitType,
getValueFromPath,
getVersionQuery,
humanReadableNumber,
Expand Down Expand Up @@ -557,3 +559,114 @@ describe('Test `matchURL`', () => {
// expect(matchURL('http://blah.com:abc')).toBe(null);
});
});

describe('Test `getSubmitType`', () => {
it('Should return null', () => {
expect(getSubmitType(undefined, undefined)).toBe(null);
expect(getSubmitType(null, null)).toBe(null);
});

const configuration: ConfigurationDefinition = {
auth: {
allow_2fa: false,
allow_apikeys: false,
allow_extended_apikeys: false,
allow_security_tokens: false
},
core: {
archiver: {
alternate_dtl: 0,
metadata: {},
use_metadata: false
}
},
datastore: {
archive: {
enabled: false
}
},
retrohunt: {
enabled: false,
dtl: 0,
max_dtl: 0
},
submission: {
file_sources: {
md5: { pattern: '^[a-f0-9]{32}$', sources: [] },
sha1: { pattern: '^[a-f0-9]{40}$', sources: [] },
sha256: { pattern: '^[a-f0-9]{64}$', sources: [] }
},
dtl: 0,
max_dtl: 0,
verdicts: {
info: 0,
suspicious: 0,
highly_suspicious: 0,
malicious: 0
}
},
system: {
organisation: '',
type: '',
version: ''
},
ui: {
ai: {
enabled: false
},
alerting_meta: {
important: [],
subject: [],
url: []
},
allow_malicious_hinting: false,
allow_raw_downloads: false,
allow_replay: false,
allow_url_submissions: false,
allow_zip_downloads: false,
apps: [],
banner: {},
banner_level: 'info',
external_links: {
tag: {},
hash: {},
metadata: {}
},
external_sources: [],
external_source_tags: {},
fqdn: '',
read_only: false,
rss_feeds: [],
services_feed: '',
community_feed: '',
tos: false,
tos_lockout: false,
tos_lockout_notify: false,
url_submission_auto_service_selection: []
},
user: {
api_priv_map: {},
priv_role_dependencies: {},
roles: [],
role_dependencies: {},
types: []
}
};

it('Should not match the input string with any type', () => {
expect(getSubmitType('', configuration)).toBe(null);
expect(getSubmitType('test', configuration)).toBe(null);
expect(getSubmitType('qwerty1234567890qwerty1234567890', configuration)).toBe(null);
expect(getSubmitType('qwerty1234567890qwerty1234567890qwerty12', configuration)).toBe(null);
expect(getSubmitType('qwerty1234567890qwerty1234567890qwerty1234567890qwerty1234567890', configuration)).toBe(null);
});

it('Should match the input string with its corresponding type', () => {
expect(getSubmitType('abcdef1234567890abcdef1234567890', configuration)).toBe('md5');
expect(getSubmitType('abcdef1234567890abcdef1234567890abcdef12', configuration)).toBe('sha1');
expect(getSubmitType('abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890', configuration)).toBe(
'sha256'
);
expect(getSubmitType('http://blah.com', configuration)).toBe('url');
});
});
16 changes: 7 additions & 9 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,17 +393,15 @@ export function filterObject(obj: Object, callback) {
*
*/
export function getSubmitType(input: string, configuration: ConfigurationDefinition): string | null {
// If we're trying to auto-detect the input type, iterate over file sources
if (!input || input === undefined) return null;
if (!configuration?.submission?.file_sources) return null;
// Return null if the parameters are invalid
if (!input || !configuration?.submission?.file_sources) return null;

let detectedHashType = Object.entries(configuration.submission.file_sources).find(
// If we're trying to auto-detect the input type, iterate over file sources
const detectedHashType = Object.entries(configuration.submission.file_sources).find(
([_, hashProps]) => hashProps && input.match(new RegExp(hashProps?.pattern))
)?.[0];

if (!detectedHashType && matchURL(input)) {
// Check to see if the input is a valid URL
detectedHashType = 'url';
}
return detectedHashType;
if (detectedHashType) return detectedHashType;
else if (!detectedHashType && matchURL(input)) return 'url';
else return null;
}