Skip to content

Commit 5d84066

Browse files
authored
Merge pull request #1742 from hackmdio/develop
Release 2.4.2
2 parents 37b656d + b55bf97 commit 5d84066

35 files changed

+506
-327
lines changed

.devcontainer/Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# [Choice] Node.js version: 16, 14, 12
2+
ARG VARIANT=12-buster
3+
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}
4+
5+
# [Optional] Uncomment this section to install additional OS packages.
6+
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
7+
# && apt-get -y install --no-install-recommends <your-package-list-here>
8+
9+
# [Optional] Uncomment if you want to install an additional version of node using nvm
10+
# ARG EXTRA_NODE_VERSION=10
11+
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
12+
13+
# [Optional] Uncomment if you want to install more global node modules
14+
RUN su node -c "npm install -g npm@6"

.devcontainer/devcontainer.json

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "CodiMD",
3+
"dockerComposeFile": "docker-compose.yml",
4+
"service": "app",
5+
"workspaceFolder": "/workspace",
6+
7+
// Set *default* container specific settings.json values on container create.
8+
"settings": {
9+
"terminal.integrated.shell.linux": "/bin/zsh",
10+
"sqltools.connections": [{
11+
"name": "Container Database",
12+
"driver": "PostgreSQL",
13+
"previewLimit": 50,
14+
"server": "localhost",
15+
"port": 5432,
16+
"database": "codimd",
17+
"username": "codimd",
18+
"password": "codimd"
19+
}],
20+
},
21+
22+
// Add the IDs of extensions you want installed when the container is created.
23+
"extensions": [
24+
"dbaeumer.vscode-eslint",
25+
"visualstudioexptteam.vscodeintellicode",
26+
"christian-kohler.path-intellisense",
27+
"standard.vscode-standard",
28+
"mtxr.sqltools",
29+
"mtxr.sqltools-driver-pg",
30+
"eamodio.gitlens",
31+
"codestream.codestream",
32+
"github.vscode-pull-request-github",
33+
"cschleiden.vscode-github-actions",
34+
"hbenl.vscode-mocha-test-adapter",
35+
"hbenl.vscode-test-explorer"
36+
],
37+
38+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
39+
// "forwardPorts": [],
40+
41+
"portsAttributes": {
42+
"3000": {
43+
"label": "CodiMD server",
44+
"onAutoForward": "notify"
45+
},
46+
"5432": {
47+
"label": "PostgreSQL",
48+
"onAutoForward": "notify"
49+
}
50+
},
51+
52+
// Use 'postCreateCommand' to run commands after the container is created.
53+
// "postCreateCommand": "yarn install",
54+
"postCreateCommand": "sudo chown -R node:node node_modules && /workspace/bin/setup",
55+
56+
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
57+
"remoteUser": "node"
58+
}

.devcontainer/docker-compose.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: '3'
2+
3+
services:
4+
app:
5+
build:
6+
context: ..
7+
dockerfile: .devcontainer/Dockerfile
8+
args:
9+
VARIANT: 12-buster
10+
environment:
11+
- CMD_DB_URL=postgres://codimd:codimd@localhost/codimd
12+
- CMD_USECDN=false
13+
volumes:
14+
- ..:/workspace:cached
15+
- node_modules:/workspace/node_modules:cached
16+
17+
# Overrides default command so things don't shut down after the process ends.
18+
command: sleep infinity
19+
20+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
21+
network_mode: service:db
22+
23+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
24+
25+
# Uncomment the next line to use a non-root user for all processes.
26+
# user: vscode
27+
28+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
29+
# (Adding the "ports" property to this file will not forward from a Codespace.)
30+
31+
db:
32+
image: postgres:12.7-alpine
33+
restart: unless-stopped
34+
volumes:
35+
- postgres-data:/var/lib/postgresql/data
36+
environment:
37+
- POSTGRES_USER=codimd
38+
- POSTGRES_PASSWORD=codimd
39+
- POSTGRES_DB=codimd
40+
41+
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
42+
# (Adding the "ports" property to this file will not forward from a Codespace.)
43+
44+
volumes:
45+
node_modules:
46+
postgres-data:

.github/workflows/build.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: 'Test and Build'
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
test-and-build:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
node-version: [10.x, 12.x]
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- uses: actions/cache@v2
19+
with:
20+
path: ~/.npm
21+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
22+
restore-keys: |
23+
${{ runner.os }}-node-
24+
25+
- uses: actions/setup-node@v2
26+
name: Use Node.js ${{ matrix.node-version }}
27+
with:
28+
node-version: ${{ matrix.node-version }}
29+
check-latest: true
30+
31+
- run: npm ci
32+
- run: npm run test:ci
33+
- run: npm run build
34+
35+
doctoc:
36+
runs-on: ubuntu-latest
37+
if: github.ref == 'refs/heads/master' || github.event.pull_request
38+
39+
steps:
40+
- uses: actions/checkout@v2
41+
- uses: actions/setup-node@v2
42+
name: Use Node.js 12
43+
with:
44+
node-version: 12
45+
check-latest: true
46+
- name: Install doctoc-check
47+
run: |
48+
npm install -g doctoc
49+
cp README.md README.md.orig
50+
npm run doctoc
51+
diff -q README.md README.md.orig

.travis.yml

-32
This file was deleted.

deployments/Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
ARG RUNTIME
2+
ARG BUILDPACK
23

3-
FROM hackmdio/buildpack:node-10-0baafb79 as BUILD
4+
FROM $BUILDPACK as BUILD
45

56
COPY --chown=hackmd:hackmd . .
7+
ENV QT_QPA_PLATFORM=offscreen
68

79
RUN set -xe && \
810
git reset --hard && \
@@ -18,6 +20,7 @@ RUN set -xe && \
1820

1921
FROM $RUNTIME
2022
USER hackmd
23+
ENV QT_QPA_PLATFORM=offscreen
2124
WORKDIR /home/hackmd/app
2225
COPY --chown=1500:1500 --from=BUILD /home/hackmd/app .
2326
RUN npm install --production && npm cache clean --force && rm -rf /tmp/{core-js-banners,phantomjs}

deployments/build.sh

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#!/usr/bin/env bash
22

3-
set -euo pipefail
3+
set -eo pipefail
44
set -x
55

6+
if [[ -z $1 || -z $2 ]];then
7+
echo "build.sh [runtime image] [buildpack image]"
8+
exit 1
9+
fi
10+
611
CURRENT_DIR=$(dirname "$BASH_SOURCE")
712

813
GIT_SHA1="$(git rev-parse HEAD)"
@@ -11,6 +16,6 @@ GIT_TAG=$(git describe --exact-match --tags $(git log -n1 --pretty='%h') 2>/dev/
1116

1217
DOCKER_TAG="${GIT_TAG:-$GIT_SHORT_ID}"
1318

14-
docker build --build-arg RUNTIME=hackmdio/runtime:node-10-d27854ef -t "hackmdio/hackmd:$DOCKER_TAG" -f "$CURRENT_DIR/Dockerfile" "$CURRENT_DIR/.."
19+
docker build --build-arg RUNTIME=$1 --build-arg BUILDPACK=$2 -t "hackmdio/hackmd:$DOCKER_TAG" -f "$CURRENT_DIR/Dockerfile" "$CURRENT_DIR/.."
1520

16-
docker build --build-arg RUNTIME=hackmdio/runtime:node-10-cjk-d27854ef -t "hackmdio/hackmd:$DOCKER_TAG-cjk" -f "$CURRENT_DIR/Dockerfile" "$CURRENT_DIR/.."
21+
docker build --build-arg RUNTIME=$1 --build-arg BUILDPACK=$2 -t "hackmdio/hackmd:$DOCKER_TAG-cjk" -f "$CURRENT_DIR/Dockerfile" "$CURRENT_DIR/.."

lib/auth/utils.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ exports.setReturnToFromReferer = function setReturnToFromReferer (req) {
88
if (!req.session) req.session = {}
99

1010
var referer = req.get('referer')
11-
var refererSearchParams = new URLSearchParams(new URL(referer).search)
12-
var nextURL = refererSearchParams.get('next')
11+
var nextURL
12+
if (referer) {
13+
try {
14+
var refererSearchParams = new URLSearchParams(new URL(referer).search)
15+
nextURL = refererSearchParams.get('next')
16+
} catch (err) {
17+
logger.warn(err)
18+
}
19+
}
1320

1421
if (nextURL) {
1522
var isRelativeNextURL = nextURL.indexOf('://') === -1 && !nextURL.startsWith('//')

lib/homepage/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ exports.showIndex = async (req, res) => {
1616
errorMessage: req.flash('error'),
1717
privacyStatement: fs.existsSync(path.join(config.docsPath, 'privacy.md')),
1818
termsOfUse: fs.existsSync(path.join(config.docsPath, 'terms-of-use.md')),
19-
deleteToken: deleteToken
19+
deleteToken: deleteToken,
20+
csrfToken: req.csrfToken()
2021
}
2122

2223
if (!isLogin) {

lib/imageRouter/s3.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ const logger = require('../logger')
99
const { S3Client } = require('@aws-sdk/client-s3-node/S3Client')
1010
const { PutObjectCommand } = require('@aws-sdk/client-s3-node/commands/PutObjectCommand')
1111

12-
const s3 = new S3Client(config.s3)
12+
const credentials = {
13+
accessKeyId: config.s3.accessKeyId,
14+
secretAccessKey: config.s3.secretAccessKey
15+
}
16+
17+
const s3 = new S3Client({
18+
credentials,
19+
region: config.s3.region,
20+
endpoint: config.s3.endpoint
21+
})
1322

1423
exports.uploadImage = function (imagePath, callback) {
1524
if (!imagePath || typeof imagePath !== 'string') {

lib/models/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fs.readdirSync(__dirname)
4545
return (file.indexOf('.') !== 0) && (file !== 'index.js')
4646
})
4747
.forEach(function (file) {
48-
var model = sequelize.import(path.join(__dirname, file))
48+
var model = require(path.join(__dirname, file))(sequelize, Sequelize)
4949
db[model.name] = model
5050
})
5151

lib/routes.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ const appRouter = Router()
1717

1818
// register route
1919

20+
const csurf = require('csurf')
21+
const csurfMiddleware = csurf({ cookie: true })
22+
2023
// get index
21-
appRouter.get('/', wrap(indexController.showIndex))
24+
appRouter.get('/', csurfMiddleware, wrap(indexController.showIndex))
2225

2326
// ----- error page -----
2427
// get 403 forbidden
@@ -52,7 +55,7 @@ appRouter.get('/me', wrap(userController.getMe))
5255
appRouter.get('/me/delete/:token?', wrap(userController.deleteUser))
5356

5457
// export the data of the authenticated user
55-
appRouter.get('/me/export', userController.exportMyData)
58+
appRouter.post('/me/export', urlencodedParser, csurfMiddleware, userController.exportMyData)
5659

5760
appRouter.get('/user/:username/avatar.svg', userController.getMyAvatar)
5861

lib/user/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ exports.exportMyData = (req, res) => {
9191
let filename
9292
let suffix = 0
9393
do {
94-
const separator = suffix === 0 ? '' : '-'
95-
filename = basename + separator + suffix + '.md'
94+
if (suffix === 0) {
95+
filename = basename + '.md'
96+
} else {
97+
filename = basename + '-' + suffix + '.md'
98+
}
9699
suffix++
97100
} while (filenames[filename])
98101
filenames[filename] = true

0 commit comments

Comments
 (0)