✅♻️🩹 backend: add company_id to editing_session_key (#525)
This commit is contained in:
@@ -4,6 +4,7 @@ import { Column, Entity } from "../../../core/platform/services/database/service
|
||||
import { DriveFileAccessLevel, publicAccessLevel } from "../types";
|
||||
import { FileVersion } from "./file-version";
|
||||
import search from "./drive-file.search";
|
||||
import * as UUIDTools from "../../../utils/uuid";
|
||||
|
||||
export const TYPE = "drive_files";
|
||||
export type DriveScope = "personal" | "shared";
|
||||
@@ -96,7 +97,8 @@ export class DriveFile {
|
||||
* If this field is non-null, then an editing session is in progress (probably in OnlyOffice).
|
||||
* Use {@see EditingSessionKeyFormat} to generate and interpret it.
|
||||
* Values should ensure that sorting lexicographically is chronological (assuming perfect clocks everywhere),
|
||||
* and that the application and user that started the edit session are retrievable.
|
||||
* and that the application, company and user that started the edit session are retrievable.
|
||||
* It is not encrypted.
|
||||
*/
|
||||
@Type(() => String)
|
||||
@Column("editing_session_key", "string")
|
||||
@@ -122,32 +124,45 @@ export class DriveFile {
|
||||
scope: DriveScope;
|
||||
}
|
||||
|
||||
const OnlyOfficeSafeDocKeyBase64 = {
|
||||
// base64 uses `+/`, but base64url uses `-_` instead. Both use `=` as padding,
|
||||
// which conflicts with EditingSessionKeyFormat so using `.` instead.
|
||||
fromBuffer(buffer: Buffer) {
|
||||
return buffer.toString("base64url").replace(/=/g, ".");
|
||||
},
|
||||
toBuffer(base64: string) {
|
||||
return Buffer.from(base64.replace(/\./g, "="), "base64url");
|
||||
},
|
||||
};
|
||||
|
||||
/** Reference implementation for generating then parsing the {@link DriveFile.editing_session_key} field */
|
||||
export const EditingSessionKeyFormat = {
|
||||
// OnlyOffice key limits: 128 chars, [0-9a-zA-z=_-]
|
||||
// OnlyOffice key limits: 128 chars, [0-9a-zA-Z.=_-]
|
||||
// See https://api.onlyoffice.com/editors/config/document#key
|
||||
// This is specific to it, but the constraint seems strict enough
|
||||
// that any other system needing such a unique identifier would find
|
||||
// this compatible. This value must be ensured to be the strictest
|
||||
// common denominator to all plugin/interop systems. Plugins that
|
||||
// require something even stricter have the option of maintaining
|
||||
// a look up table to an acceptable value.
|
||||
generate(applicationId: string, userId: string) {
|
||||
generate(applicationId: string, companyId: string, userId: string, overrideTimeStamp?: Date) {
|
||||
if (!/^[0-9a-zA-Z_-]+$/m.test(applicationId))
|
||||
throw new Error(
|
||||
`Invalid applicationId string (${JSON.stringify(
|
||||
applicationId,
|
||||
)}). Must be short and only alpha numeric`,
|
||||
);
|
||||
const isoUTCDateNoSpecialCharsNoMS = new Date()
|
||||
const isoUTCDateNoSpecialCharsNoMS = (overrideTimeStamp ?? new Date())
|
||||
.toISOString()
|
||||
.replace(/\..+$/, "")
|
||||
.replace(/[ZT:-]/g, "");
|
||||
const newKey = [
|
||||
isoUTCDateNoSpecialCharsNoMS,
|
||||
applicationId,
|
||||
userId.replace(/-+/g, ""),
|
||||
randomUUID().replace(/-+/g, ""),
|
||||
].join("=");
|
||||
const userIdBuffer = UUIDTools.bufferFromUUIDString(userId) as unknown as Uint8Array;
|
||||
const companyIdBuffer = UUIDTools.bufferFromUUIDString(companyId) as unknown as Uint8Array;
|
||||
const entropyBuffer = UUIDTools.bufferFromUUIDString(randomUUID()) as unknown as Uint8Array;
|
||||
const idsString = OnlyOfficeSafeDocKeyBase64.fromBuffer(
|
||||
Buffer.concat([companyIdBuffer, userIdBuffer, entropyBuffer]),
|
||||
);
|
||||
const newKey = [isoUTCDateNoSpecialCharsNoMS, applicationId, idsString].join("=");
|
||||
if (newKey.length > 128 || !/^[0-9a-zA-Z=_-]+$/m.test(newKey))
|
||||
throw new Error(
|
||||
`Invalid generated editingSessionKey (${JSON.stringify(
|
||||
@@ -159,14 +174,14 @@ export const EditingSessionKeyFormat = {
|
||||
|
||||
parse(editingSessionKey: string) {
|
||||
const parts = editingSessionKey.split("=");
|
||||
const expectedParts = 4;
|
||||
const expectedParts = 3;
|
||||
if (parts.length !== expectedParts)
|
||||
throw new Error(
|
||||
`Invalid editingSessionKey (${JSON.stringify(
|
||||
editingSessionKey,
|
||||
)}). Expected ${expectedParts} parts`,
|
||||
);
|
||||
const [timestampStr, appId, userId, _random] = parts;
|
||||
const [timestampStr, appId, idsOOBase64String] = parts;
|
||||
const timestampMatch = timestampStr.match(
|
||||
/^(?<year>\d{4})(?<month>\d\d)(?<day>\d\d)(?<hour>\d\d)(?<minute>\d\d)(?<second>\d\d)$/,
|
||||
);
|
||||
@@ -177,22 +192,16 @@ export const EditingSessionKeyFormat = {
|
||||
)}). Didn't start with valid timestamp`,
|
||||
);
|
||||
const { year, month, day, hour, minute, second } = timestampMatch.groups!;
|
||||
const userIdMatch = userId.match(
|
||||
/^([a-z0-f]{8})([a-z0-f]{4})([a-z0-f]{4})([a-z0-f]{4})([a-z0-f]{12})$/i,
|
||||
);
|
||||
if (!userIdMatch)
|
||||
throw new Error(
|
||||
`Invalid editingSessionKey (${JSON.stringify(
|
||||
editingSessionKey,
|
||||
)}). UserID has wrong number of digits`,
|
||||
);
|
||||
const [, userIdPart1, userIdPart2, userIdPart3, userIdPart4, userIdPart5] = userIdMatch;
|
||||
const idsBuffer = OnlyOfficeSafeDocKeyBase64.toBuffer(idsOOBase64String);
|
||||
const companyId = UUIDTools.formattedUUIDInBufferArray(idsBuffer, 0);
|
||||
const userId = UUIDTools.formattedUUIDInBufferArray(idsBuffer, 1);
|
||||
return {
|
||||
timestamp: new Date(
|
||||
Date.parse(`${[year, month, day].join("-")}T${[hour, minute, second].join(":")}Z`),
|
||||
),
|
||||
applicationId: appId,
|
||||
userId: [userIdPart1, userIdPart2, userIdPart3, userIdPart4, userIdPart5].join("-"),
|
||||
companyId,
|
||||
userId,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
@@ -976,7 +976,11 @@ export class DocumentsService {
|
||||
|
||||
let newKey: string;
|
||||
try {
|
||||
newKey = EditingSessionKeyFormat.generate(editorApplicationId, context.user.id);
|
||||
newKey = EditingSessionKeyFormat.generate(
|
||||
editorApplicationId,
|
||||
context.company.id,
|
||||
context.user.id,
|
||||
);
|
||||
} catch (e) {
|
||||
CrudException.throwMe(e, new CrudException("Error generating new editing_session_key", 500));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user