Skip to content

Commit

Permalink
Add a configuration option 'sharingKeyLength' under the hood. Use thi…
Browse files Browse the repository at this point in the history
…s setting to generate a more complex sharing key.
  • Loading branch information
VirtualGit committed Apr 10, 2024
1 parent 2e6bff1 commit e305215
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/backend/middlewares/SharingMWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ export class SharingMWs {
);
}

let sharingKey = SharingMWs.generateKey();
let sharingKey = SharingMWs.generateKey(Config.Sharing.sharingKeyLength);

// create one not yet used
// eslint-disable-next-line no-constant-condition
while (true) {
try {
await ObjectManagers.getInstance().SharingManager.findOne(sharingKey);
sharingKey = this.generateKey();
sharingKey = this.generateKey(Config.Sharing.sharingKeyLength);
} catch (err) {
break;
}
Expand Down Expand Up @@ -279,13 +279,14 @@ export class SharingMWs {
}
}

private static generateKey(): string {
function s4(): string {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
private static generateKey(length:number): string {
function randomInt(max:number): number {
return Math.floor(Math.random() * max);
}

return s4() + s4();
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
return [...Array(length).keys()].reduce(
(result) => result + characters.charAt(randomInt(characters.length)),
""
);
}
}
10 changes: 10 additions & 0 deletions src/common/config/public/ClientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ export class ClientSharingConfig {
description: $localize`Requires password protected sharing links.`,
})
passwordRequired: boolean = false;
@ConfigProperty({
type: 'unsignedInt', min: 8, max: 64,
tags:
{
name: $localize`Sharing key length`,
priority: ConfigPriority.underTheHood
},
description: $localize`The longer the keys are, the more secure the share links will be.`,
})
sharingKeyLength: number = 32;
}

@SubConfigClass({tags: {client: true}, softReadonly: true})
Expand Down

0 comments on commit e305215

Please sign in to comment.