Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: only update view count when we validate the password if it's set

lemmyMwaura69fe5bf71d58
diff --git a/backend/src/services/secret-sharing/secret-sharing-service.ts b/backend/src/services/secret-sharing/secret-sharing-service.ts
index 2f01c67484..14a11a65e4 100644
--- a/backend/src/services/secret-sharing/secret-sharing-service.ts
+++ b/backend/src/services/secret-sharing/secret-sharing-service.ts
@@ -172,37 +172,17 @@ export const secretSharingServiceFactory = ({
message: "Shared secret not found"
});
- const { accessType, expiresAt, expiresAfterViews } = sharedSecret;
+ const { accessType } = sharedSecret;
const orgName = sharedSecret.orgId ? (await orgDAL.findOrgById(sharedSecret.orgId))?.name : "";
if (accessType === SecretSharingAccessType.Organization && orgId !== sharedSecret.orgId)
throw new UnauthorizedError();
- if (expiresAt !== null && expiresAt < new Date()) {
- // check lifetime expiry
- await secretSharingDAL.softDeleteById(sharedSecretId);
- throw new ForbiddenRequestError({
- message: "Access denied: Secret has expired by lifetime"
- });
- }
+ if (sharedSecret.password) return undefined;
- if (expiresAfterViews !== null && expiresAfterViews === 0) {
- // check view count expiry
- await secretSharingDAL.softDeleteById(sharedSecretId);
- throw new ForbiddenRequestError({
- message: "Access denied: Secret has expired by view count"
- });
- }
-
- if (expiresAfterViews) {
- // decrement view count if view count expiry set
- await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } });
- }
-
- await secretSharingDAL.updateById(sharedSecretId, {
- lastViewedAt: new Date()
- });
+ // we only update the view count when secret has no password, when password is set view count is updated when we validate the password.
+ manageSecretViewCount(sharedSecret, sharedSecretId);
return {
...sharedSecret,
@@ -219,14 +199,37 @@ export const secretSharingServiceFactory = ({
orgId,
password
}: TValidateActiveSharedSecretDTO) => {
- const sharedSecret = await getActiveSharedSecretById({ sharedSecretId, hashedHex, orgId });
+ const sharedSecret = await secretSharingDAL.findOne({
+ id: sharedSecretId,
+ hashedHex
+ });
+ if (!sharedSecret)
+ throw new NotFoundError({
+ message: "Shared secret not found"
+ });
- if (!sharedSecret || !sharedSecret.password) return undefined;
+ const { accessType } = sharedSecret;
- const isMatch = await bcrypt.compare(password, sharedSecret.password);
+ const orgName = sharedSecret.orgId ? (await orgDAL.findOrgById(sharedSecret.orgId))?.name : "";
+
+ if (accessType === SecretSharingAccessType.Organization && orgId !== sharedSecret.orgId)
+ throw new UnauthorizedError();
+
+ if (!sharedSecret.password) return undefined;
+ const isMatch = await bcrypt.compare(password, sharedSecret.password);
if (!isMatch) return undefined;
- return sharedSecret
+
+ // reduce view count when we are sure the password matches.
+ manageSecretViewCount(sharedSecret, sharedSecretId)
+
+ return {
+ ...sharedSecret,
+ orgName:
+ sharedSecret.accessType === SecretSharingAccessType.Organization && orgId === sharedSecret.orgId
+ ? orgName
+ : undefined
+ };
};
const deleteSharedSecretById = async (deleteSharedSecretInput: TDeleteSharedSecretDTO) => {
@@ -237,6 +240,35 @@ export const secretSharingServiceFactory = ({
return deletedSharedSecret;
};
+ const manageSecretViewCount = async (sharedSecret: any, sharedSecretId: string) => {
+ const { expiresAt, expiresAfterViews } = sharedSecret;
+
+ if (expiresAt !== null && expiresAt < new Date()) {
+ // check lifetime expiry
+ await secretSharingDAL.softDeleteById(sharedSecretId);
+ throw new ForbiddenRequestError({
+ message: "Access denied: Secret has expired by lifetime"
+ });
+ }
+
+ if (expiresAfterViews !== null && expiresAfterViews === 0) {
+ // check view count expiry
+ await secretSharingDAL.softDeleteById(sharedSecretId);
+ throw new ForbiddenRequestError({
+ message: "Access denied: Secret has expired by view count"
+ });
+ }
+
+ if (expiresAfterViews) {
+ // decrement view count if view count expiry set
+ await secretSharingDAL.updateById(sharedSecretId, { $decr: { expiresAfterViews: 1 } });
+ }
+
+ await secretSharingDAL.updateById(sharedSecretId, {
+ lastViewedAt: new Date()
+ });
+ }
+
return {
createSharedSecret,
createPublicSharedSecret,
diff --git a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx
index 3ff918f245..ceef5b291a 100644
--- a/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx
+++ b/frontend/src/views/ViewSecretPublicPage/components/PasswordContainer.tsx
@@ -90,9 +90,8 @@ export const PasswordContainer = ({ secretId, hashedHex, handleSecret }: Props)
)}
/>
</form>
-
<Button
- className="mt-4 w-full bg-mineshaft-700 py-3 text-bunker-200"
+ className="w-full bg-mineshaft-700 py-3 text-bunker-200"
colorSchema="primary"
variant="outline_bg"
size="sm"