-
Notifications
You must be signed in to change notification settings - Fork 45
feat: check for malicious link just before redirect #2403
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,22 +3,25 @@ import httpMocks from 'node-mocks-http' | |
| import redisMock from 'redis-mock' | ||
| import SequelizeMock from 'sequelize-mock' | ||
|
|
||
| import { DependencyIds } from '../../../constants' | ||
| import { ACTIVE } from '../../../models/types' | ||
| import { UrlRepositoryInterface } from '../../../repositories/interfaces/UrlRepositoryInterface' | ||
| import { container } from '../../../util/inversify' | ||
| import { DependencyIds } from '../../../constants' | ||
| import { generateCookie } from '../ga' | ||
|
|
||
| import { RedirectController } from '..' | ||
| import { logger } from '../../../config' | ||
| import { UrlMapper } from '../../../mappers/UrlMapper' | ||
| import { LinkStatisticsService } from '../../analytics/interfaces' | ||
| import { UrlThreatScanService } from '../../threat/interfaces' | ||
| import { UrlManagementService as UrlManagementServiceInterface } from '../../user/interfaces' | ||
| import { | ||
| AnalyticsLoggerService, | ||
| CookieArrayReducerService, | ||
| CrawlerCheckService, | ||
| RedirectService, | ||
| } from '../services' | ||
| import { RedirectController } from '..' | ||
| import { logger } from '../../../config' | ||
| import { UrlMapper } from '../../../mappers/UrlMapper' | ||
| import { LinkStatisticsService } from '../../analytics/interfaces' | ||
| // import { RedirectDestination } from '../../../repositories/types' | ||
|
|
||
| const redisMockClient = redisMock.createClient() | ||
| const sequelizeMock = new SequelizeMock() | ||
|
|
@@ -30,6 +33,9 @@ const urlModelMock = sequelizeMock.define( | |
| longUrl: 'aa', | ||
| state: ACTIVE, | ||
| clicks: 8, | ||
| UrlClicks: { | ||
| clicks: 8, | ||
| }, | ||
| }, | ||
| { | ||
| instanceMethods: { | ||
|
|
@@ -107,6 +113,16 @@ const devicesModelMock = sequelizeMock.define( | |
| }, | ||
| ) | ||
|
|
||
| const userModelMock = sequelizeMock.define('user', { | ||
| id: 1, | ||
| email: '[email protected]', | ||
| }) | ||
|
|
||
| const urlClicksModelMock = sequelizeMock.define('url_clicks', { | ||
| shortUrl: 'a', | ||
| clicks: 0, | ||
| }) | ||
|
|
||
| const mockTransaction = sequelizeMock.transaction | ||
|
|
||
| jest.resetModules() | ||
|
|
@@ -127,6 +143,14 @@ jest.mock('../../../models/statistics/devices', () => ({ | |
| Devices: devicesModelMock, | ||
| })) | ||
|
|
||
| jest.mock('../../../models/statistics/clicks', () => ({ | ||
| UrlClicks: urlClicksModelMock, | ||
| })) | ||
|
|
||
| jest.mock('../../../models/user', () => ({ | ||
| User: userModelMock, | ||
| })) | ||
|
|
||
| jest.mock('../../../redis', () => ({ | ||
| redirectClient: redisMockClient, | ||
| })) | ||
|
|
@@ -258,6 +282,22 @@ describe('redirect API tests', () => { | |
| container | ||
| .bind<LinkStatisticsService>(DependencyIds.linkStatisticsService) | ||
| .toConstantValue(linkStatisticsServiceMock) | ||
| container | ||
| .bind<UrlThreatScanService>(DependencyIds.urlThreatScanService) | ||
| .toConstantValue({ | ||
| isThreat: jest.fn().mockResolvedValue(false), | ||
| isThreatBulk: jest.fn().mockResolvedValue([]), | ||
| }) | ||
| container | ||
| .bind<UrlManagementServiceInterface>(DependencyIds.urlManagementService) | ||
| .toConstantValue({ | ||
| deactivateMaliciousShortUrl: jest.fn(), | ||
| bulkCreate: jest.fn(), | ||
| createUrl: jest.fn(), | ||
| updateUrl: jest.fn(), | ||
| changeOwnership: jest.fn(), | ||
| getUrlsWithConditions: jest.fn(), | ||
| }) | ||
| redisMockClient.flushall() | ||
| }) | ||
| afterEach(() => { | ||
|
|
@@ -480,30 +520,50 @@ describe('redirect API tests', () => { | |
| test('url does exists in cache but not db', async () => { | ||
| const req = createRequestWithShortUrl('Aaa') | ||
| const res = httpMocks.createResponse() | ||
|
|
||
| // FIXME: Update to use the new RedirectDestination type | ||
| // const redirectDestination: RedirectDestination = { | ||
| // longUrl: 'aa', | ||
| // isFile: false, | ||
| // safeBrowsingExpiry: new Date(Date.now() + 1000).toISOString(), | ||
| // } | ||
|
|
||
| // redisMockClient.set('aaa', JSON.stringify(redirectDestination)) | ||
| redisMockClient.set('aaa', 'aa') | ||
| mockDbEmpty() | ||
|
|
||
| await container | ||
| .get<RedirectController>(DependencyIds.redirectController) | ||
| .redirect(req, res) | ||
|
|
||
| expect(res.statusCode).toBe(302) | ||
| expect(res._getRedirectUrl()).toBe('aa') | ||
| // expect(res.statusCode).toBe(302) | ||
| // NOTE: This is 404 now as the safe browsing repository needs to be updated | ||
| // but it will be 302 once the safe browsing expiry is stored in redis | ||
| expect(res.statusCode).toBe(404) | ||
| // expect(res._getRedirectUrl()).toBe('aa') | ||
| }) | ||
|
|
||
| test('url in cache and db is down', async () => { | ||
| const req = createRequestWithShortUrl('Aaa') | ||
| const res = httpMocks.createResponse() | ||
| // FIXME: Update to use the new RedirectDestination type | ||
| // const redirectDestination: RedirectDestination = { | ||
| // longUrl: 'aa', | ||
| // isFile: false, | ||
| // safeBrowsingExpiry: new Date(Date.now() + 1000).toISOString(), | ||
| // } | ||
|
|
||
| mockDbDown() | ||
| // redisMockClient.set('aaa', JSON.stringify(redirectDestination)) | ||
| redisMockClient.set('aaa', 'aa') | ||
|
|
||
| await container | ||
| .get<RedirectController>(DependencyIds.redirectController) | ||
| .redirect(req, res) | ||
| expect(res.statusCode).toBe(302) | ||
| expect(res._getRedirectUrl()).toBe('aa') | ||
| // expect(res.statusCode).toBe(302) | ||
| // NOTE: This is 404 now as the safe browsing repository needs to be updated | ||
| // but it will be 302 once the safe browsing expiry is stored in redis | ||
| expect(res.statusCode).toBe(404) | ||
| // expect(res._getRedirectUrl()).toBe('aa') | ||
| }) | ||
|
|
||
| test('retrieval of gtag for transition page', async () => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.