Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd9d828
commit 008df06
Showing
11 changed files
with
144 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
ExecutionContext, | ||
ForbiddenException, | ||
Injectable, | ||
NotFoundException, | ||
} from "@nestjs/common"; | ||
import { Request } from "express"; | ||
import * as moment from "moment"; | ||
import { PrismaService } from "src/prisma/prisma.service"; | ||
import { ShareSecurityGuard } from "src/share/guard/shareSecurity.guard"; | ||
import { ShareService } from "src/share/share.service"; | ||
|
||
@Injectable() | ||
export class FileSecurityGuard extends ShareSecurityGuard { | ||
constructor( | ||
private _shareService: ShareService, | ||
private _prisma: PrismaService | ||
) { | ||
super(_shareService, _prisma); | ||
} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
const request: Request = context.switchToHttp().getRequest(); | ||
|
||
const shareId = Object.prototype.hasOwnProperty.call( | ||
request.params, | ||
"shareId" | ||
) | ||
? request.params.shareId | ||
: request.params.id; | ||
|
||
const shareToken = request.cookies[`share_${shareId}_token`]; | ||
|
||
const share = await this._prisma.share.findUnique({ | ||
where: { id: shareId }, | ||
include: { security: true }, | ||
}); | ||
|
||
// If there is no share token the user requests a file directly | ||
if (!shareToken) { | ||
if ( | ||
!share || | ||
(moment().isAfter(share.expiration) && | ||
!moment(share.expiration).isSame(0)) | ||
) { | ||
throw new NotFoundException("File not found"); | ||
} | ||
|
||
if (share.security?.password) | ||
throw new ForbiddenException("This share is password protected"); | ||
|
||
if (share.security?.maxViews && share.security.maxViews <= share.views) { | ||
throw new ForbiddenException( | ||
"Maximum views exceeded", | ||
"share_max_views_exceeded" | ||
); | ||
} | ||
|
||
await this._shareService.increaseViewCount(share); | ||
return true; | ||
} else { | ||
return super.canActivate(context); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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