Skip to content

Commit

Permalink
Merge pull request #1083 from CybercentreCanada/submit_by_string
Browse files Browse the repository at this point in the history
Submit by String (master)
  • Loading branch information
cccs-nr committed May 27, 2024
2 parents 559350c + 53e013a commit a6f6ad0
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 9 deletions.
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 @@ -412,19 +412,17 @@ 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;
}

/**
Expand Down

0 comments on commit a6f6ad0

Please sign in to comment.